2012-01-07 16:17:48 +00:00
|
|
|
-module(lsm_btree_tests).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
|
|
|
-ifdef(TEST).
|
2012-01-07 14:44:31 +00:00
|
|
|
-ifdef(TRIQ).
|
|
|
|
-include_lib("triq/include/triq.hrl").
|
|
|
|
-include_lib("triq/include/triq_statem.hrl").
|
|
|
|
-else.
|
2012-01-06 21:56:23 +00:00
|
|
|
-include_lib("proper/include/proper.hrl").
|
2012-01-07 14:44:31 +00:00
|
|
|
-endif.
|
2012-01-04 14:05:31 +00:00
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
-endif.
|
|
|
|
|
2012-01-07 14:44:31 +00:00
|
|
|
-ifdef(PROPER).
|
2012-01-06 21:56:23 +00:00
|
|
|
-behaviour(proper_statem).
|
2012-01-07 14:44:31 +00:00
|
|
|
-endif.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
-compile(export_all).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
-export([command/1, initial_state/0,
|
|
|
|
next_state/3, postcondition/3,
|
|
|
|
precondition/2]).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
-record(state, { open = dict:new(),
|
|
|
|
closed = dict:new() }).
|
2012-01-07 16:17:48 +00:00
|
|
|
-define(SERVER, lsm_btree_drv).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
full_test_() ->
|
|
|
|
{setup,
|
|
|
|
spawn,
|
|
|
|
fun () -> ok end,
|
|
|
|
fun (_) -> ok end,
|
2012-01-07 13:21:38 +00:00
|
|
|
[
|
2012-01-07 14:44:31 +00:00
|
|
|
{timeout, 120, ?_test(test_qc())},
|
2012-01-07 00:02:09 +00:00
|
|
|
?_test(test_tree_simple_1()),
|
2012-01-07 11:43:49 +00:00
|
|
|
?_test(test_tree_simple_2()),
|
2012-01-07 13:21:38 +00:00
|
|
|
?_test(test_tree())
|
|
|
|
]}.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 14:44:31 +00:00
|
|
|
-ifdef(TRIQ).
|
|
|
|
test_qc() ->
|
|
|
|
[?assertEqual(true, triq:module(?MODULE))].
|
|
|
|
-else.
|
2012-01-07 00:02:09 +00:00
|
|
|
qc_opts() -> [{numtests, 800}].
|
2012-01-07 14:44:31 +00:00
|
|
|
test_qc() ->
|
2012-01-06 21:56:23 +00:00
|
|
|
[?assertEqual([], proper:module(?MODULE, qc_opts()))].
|
2012-01-07 14:44:31 +00:00
|
|
|
-endif.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Generators
|
|
|
|
%% ----------------------------------------------------------------------
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 14:16:57 +00:00
|
|
|
-define(NUM_TREES, 10).
|
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Generate a name for a btree
|
2012-01-06 21:56:23 +00:00
|
|
|
g_btree_name() ->
|
2012-01-07 14:44:31 +00:00
|
|
|
?LET(I, choose(1,?NUM_TREES),
|
2012-01-07 14:16:57 +00:00
|
|
|
btree_name(I)).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 21:58:38 +00:00
|
|
|
%% Generate a key for the Tree
|
|
|
|
g_key() ->
|
|
|
|
binary().
|
|
|
|
|
|
|
|
%% Generate a value for the Tree
|
|
|
|
g_value() ->
|
|
|
|
binary().
|
|
|
|
|
|
|
|
g_fail_key() ->
|
|
|
|
?LET(T, choose(1,999999999999),
|
|
|
|
term_to_binary(T)).
|
|
|
|
|
2012-01-07 22:35:27 +00:00
|
|
|
g_open_tree(Open) ->
|
|
|
|
oneof(dict:fetch_keys(Open)).
|
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Pick a name of a non-empty Btree
|
2012-01-07 22:35:27 +00:00
|
|
|
g_non_empty_btree(Open) ->
|
|
|
|
?LET(TreesWithKeys, dict:filter(fun(_K, D) -> dict:size(D) > 0 end, Open),
|
|
|
|
oneof(dict:fetch_keys(TreesWithKeys))).
|
2012-01-07 21:58:38 +00:00
|
|
|
|
|
|
|
g_existing_key(Name, Open) ->
|
|
|
|
oneof(dict:fetch_keys(dict:fetch(Name, Open))).
|
|
|
|
|
|
|
|
g_non_existing_key(Name, Open) ->
|
|
|
|
?SUCHTHAT(Key, g_fail_key(),
|
|
|
|
begin
|
|
|
|
D = dict:fetch(Name, Open),
|
|
|
|
not dict:is_key(Key, D)
|
|
|
|
end).
|
2012-01-07 00:02:09 +00:00
|
|
|
|
2012-01-07 14:16:57 +00:00
|
|
|
btree_name(I) ->
|
|
|
|
"Btree_" ++ integer_to_list(I).
|
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Statem test
|
|
|
|
%% ----------------------------------------------------------------------
|
|
|
|
initial_state() ->
|
2012-01-07 14:16:57 +00:00
|
|
|
ClosedBTrees = lists:foldl(fun(N, Closed) ->
|
|
|
|
dict:store(btree_name(N),
|
|
|
|
dict:new(),
|
|
|
|
Closed)
|
|
|
|
end,
|
|
|
|
dict:new(),
|
|
|
|
lists:seq(1,?NUM_TREES)),
|
|
|
|
#state { closed=ClosedBTrees }.
|
2012-01-07 00:02:09 +00:00
|
|
|
|
|
|
|
|
2012-01-07 14:16:57 +00:00
|
|
|
command(#state { open = Open, closed = Closed } = S) ->
|
2012-01-06 21:56:23 +00:00
|
|
|
frequency(
|
2012-01-07 14:16:57 +00:00
|
|
|
[ {20, {call, ?SERVER, open, [oneof(dict:fetch_keys(Closed))]}}
|
2012-01-07 21:58:38 +00:00
|
|
|
|| closed_dicts(S)] ++
|
2012-01-07 14:16:57 +00:00
|
|
|
[ {20, {call, ?SERVER, close, [oneof(dict:fetch_keys(Open))]}}
|
2012-01-07 21:58:38 +00:00
|
|
|
|| open_dicts(S)] ++
|
2012-01-06 21:56:23 +00:00
|
|
|
[ {2000, {call, ?SERVER, put, cmd_put_args(S)}}
|
2012-01-07 21:58:38 +00:00
|
|
|
|| open_dicts(S)] ++
|
2012-01-07 22:35:27 +00:00
|
|
|
[ {1500, {call, ?SERVER, lookup_fail, cmd_lookup_fail_args(S)}}
|
|
|
|
|| open_dicts(S)] ++
|
2012-01-07 00:02:09 +00:00
|
|
|
[ {1500, {call, ?SERVER, lookup_exist, cmd_lookup_args(S)}}
|
2012-01-07 21:58:38 +00:00
|
|
|
|| open_dicts(S), open_dicts_with_keys(S)] ++
|
2012-01-07 11:43:49 +00:00
|
|
|
[ {500, {call, ?SERVER, delete_exist, cmd_delete_args(S)}}
|
2012-01-07 21:58:38 +00:00
|
|
|
|| open_dicts(S), open_dicts_with_keys(S)]).
|
2012-01-07 11:43:49 +00:00
|
|
|
|
|
|
|
%% Precondition (abstract)
|
|
|
|
precondition(#state { open = _Open}, {call, ?SERVER, delete_exist,
|
|
|
|
[_Name, _K]}) ->
|
|
|
|
%% No need to check since we limit this in the command/1 generator
|
|
|
|
true;
|
2012-01-07 21:58:38 +00:00
|
|
|
precondition(#state { open = _Open }, {call, ?SERVER, lookup_fail,
|
|
|
|
[_Name, _K]}) ->
|
|
|
|
%% We can always try to look up some non-existing key
|
|
|
|
true;
|
2012-01-07 00:02:09 +00:00
|
|
|
precondition(#state { open = _Open }, {call, ?SERVER, lookup_exist,
|
|
|
|
[_Name, _K]}) ->
|
2012-01-07 11:43:49 +00:00
|
|
|
%% No need to check since we limit this in the command/1 generator
|
2012-01-07 00:02:09 +00:00
|
|
|
true;
|
2012-01-07 11:43:49 +00:00
|
|
|
precondition(#state { open = Open }, {call, ?SERVER, put, [Name, _K, _V]}) ->
|
2012-01-06 21:56:23 +00:00
|
|
|
dict:is_key(Name, Open);
|
2012-01-07 14:16:57 +00:00
|
|
|
precondition(#state { open = Open, closed = Closed }, {call, ?SERVER, open, [Name]}) ->
|
|
|
|
(not (dict:is_key(Name, Open))) and (dict:is_key(Name, Closed));
|
|
|
|
precondition(#state { open = Open, closed = Closed }, {call, ?SERVER, close, [Name]}) ->
|
|
|
|
(dict:is_key(Name, Open)) and (not dict:is_key(Name, Closed)).
|
|
|
|
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Next state manipulation (abstract / concrete)
|
2012-01-07 21:58:38 +00:00
|
|
|
next_state(S, _Res, {call, ?SERVER, lookup_fail, [_Name, _Key]}) ->
|
|
|
|
S;
|
2012-01-07 00:02:09 +00:00
|
|
|
next_state(S, _Res, {call, ?SERVER, lookup_exist, [_Name, _Key]}) ->
|
|
|
|
S;
|
2012-01-07 11:43:49 +00:00
|
|
|
next_state(#state { open = Open} = S, _Res,
|
|
|
|
{call, ?SERVER, delete_exist, [Name, Key]}) ->
|
|
|
|
S#state { open = dict:update(Name,
|
|
|
|
fun(Dict) ->
|
|
|
|
dict:erase(Key, Dict)
|
|
|
|
end,
|
|
|
|
Open)};
|
2012-01-06 21:56:23 +00:00
|
|
|
next_state(#state { open = Open} = S, _Res,
|
|
|
|
{call, ?SERVER, put, [Name, Key, Value]}) ->
|
|
|
|
S#state { open = dict:update(Name,
|
|
|
|
fun(Dict) ->
|
|
|
|
dict:store(Key, Value, Dict)
|
|
|
|
end,
|
|
|
|
Open)};
|
2012-01-07 14:16:57 +00:00
|
|
|
next_state(#state { open = Open, closed=Closed} = S, _Res, {call, ?SERVER, open, [Name]}) ->
|
|
|
|
S#state { open = dict:store(Name, dict:fetch(Name, Closed) , Open),
|
|
|
|
closed = dict:erase(Name, Closed) };
|
|
|
|
next_state(#state { open = Open, closed=Closed} = S, _Res, {call, ?SERVER, close, [Name]}) ->
|
|
|
|
S#state { closed = dict:store(Name, dict:fetch(Name, Open) , Closed),
|
|
|
|
open = dict:erase(Name, Open) }.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Postcondition check (concrete)
|
2012-01-07 22:35:27 +00:00
|
|
|
postcondition(_S,
|
2012-01-07 21:58:38 +00:00
|
|
|
{call, ?SERVER, lookup_fail, [_Name, _Key]}, notfound) ->
|
|
|
|
true;
|
2012-01-07 00:02:09 +00:00
|
|
|
postcondition(#state { open = Open },
|
|
|
|
{call, ?SERVER, lookup_exist, [Name, Key]}, {ok, Value}) ->
|
2012-01-07 11:43:49 +00:00
|
|
|
dict:fetch(Key, dict:fetch(Name, Open)) == Value;
|
|
|
|
postcondition(_S, {call, ?SERVER, delete_exist, [_Name, _Key]}, ok) ->
|
|
|
|
true;
|
2012-01-06 21:56:23 +00:00
|
|
|
postcondition(_S, {call, ?SERVER, put, [_Name, _Key, _Value]}, ok) ->
|
|
|
|
true;
|
|
|
|
postcondition(_S, {call, ?SERVER, open, [_Name]}, ok) ->
|
|
|
|
true;
|
2012-01-07 14:16:57 +00:00
|
|
|
postcondition(_S, {call, ?SERVER, close, [_Name]}, ok) ->
|
|
|
|
true;
|
2012-01-06 21:56:23 +00:00
|
|
|
postcondition(_, _, _) ->
|
|
|
|
false.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Main property. Running a random set of commands is in agreement
|
|
|
|
%% with a dict.
|
2012-01-06 21:56:23 +00:00
|
|
|
prop_dict_agree() ->
|
|
|
|
?FORALL(Cmds, commands(?MODULE),
|
|
|
|
?TRAPEXIT(
|
|
|
|
begin
|
2012-01-07 16:17:48 +00:00
|
|
|
lsm_btree_drv:start_link(),
|
2012-01-06 21:56:23 +00:00
|
|
|
{History,State,Result} = run_commands(?MODULE, Cmds),
|
2012-01-07 16:17:48 +00:00
|
|
|
lsm_btree_drv:stop(),
|
2012-01-07 00:02:09 +00:00
|
|
|
cleanup_test_trees(State),
|
2012-01-06 21:56:23 +00:00
|
|
|
?WHENFAIL(io:format("History: ~w\nState: ~w\nResult: ~w\n",
|
|
|
|
[History,State,Result]),
|
2012-01-07 14:44:31 +00:00
|
|
|
Result =:= ok)
|
2012-01-06 21:56:23 +00:00
|
|
|
end)).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% UNIT TESTS
|
2012-01-06 21:56:23 +00:00
|
|
|
%% ----------------------------------------------------------------------
|
2012-01-07 00:02:09 +00:00
|
|
|
test_tree_simple_1() ->
|
2012-01-07 16:17:48 +00:00
|
|
|
{ok, Tree} = lsm_btree:open("simple"),
|
|
|
|
ok = lsm_btree:put(Tree, <<>>, <<"data", 77:128>>),
|
|
|
|
{ok, <<"data", 77:128>>} = lsm_btree:lookup(Tree, <<>>),
|
|
|
|
ok = lsm_btree:close(Tree).
|
2012-01-07 00:02:09 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
test_tree_simple_2() ->
|
2012-01-07 16:17:48 +00:00
|
|
|
{ok, Tree} = lsm_btree:open("simple"),
|
|
|
|
ok = lsm_btree:put(Tree, <<"ã">>, <<"µ">>),
|
|
|
|
ok = lsm_btree:delete(Tree, <<"ã">>),
|
|
|
|
ok = lsm_btree:close(Tree).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
test_tree() ->
|
2012-01-05 10:48:14 +00:00
|
|
|
|
2012-01-06 23:28:46 +00:00
|
|
|
%% application:start(sasl),
|
2012-01-05 10:48:14 +00:00
|
|
|
|
2012-01-07 16:17:48 +00:00
|
|
|
{ok, Tree} = lsm_btree:open("simple"),
|
2012-01-05 10:48:14 +00:00
|
|
|
lists:foldl(fun(N,_) ->
|
2012-01-07 16:17:48 +00:00
|
|
|
ok = lsm_btree:put(Tree,
|
2012-01-07 11:43:49 +00:00
|
|
|
<<N:128>>, <<"data",N:128>>)
|
2012-01-05 10:48:14 +00:00
|
|
|
end,
|
|
|
|
ok,
|
|
|
|
lists:seq(2,10000,1)),
|
|
|
|
|
2012-01-07 16:17:48 +00:00
|
|
|
ok = lsm_btree:close(Tree).
|
2012-01-05 10:48:14 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Command processing
|
|
|
|
%% ----------------------------------------------------------------------
|
|
|
|
cmd_close_args(#state { open = Open }) ->
|
|
|
|
oneof(dict:fetch_keys(Open)).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
cmd_put_args(#state { open = Open }) ->
|
|
|
|
?LET({Name, Key, Value},
|
2012-01-07 21:58:38 +00:00
|
|
|
{oneof(dict:fetch_keys(Open)), g_key(), g_value()},
|
2012-01-07 11:43:49 +00:00
|
|
|
[Name, Key, Value]).
|
|
|
|
|
|
|
|
|
2012-01-07 21:58:38 +00:00
|
|
|
cmd_lookup_fail_args(#state { open = Open}) ->
|
2012-01-07 22:35:27 +00:00
|
|
|
?LET(Name, g_open_tree(Open),
|
2012-01-07 21:58:38 +00:00
|
|
|
?LET(Key, g_non_existing_key(Name, Open),
|
|
|
|
[Name, Key])).
|
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
cmd_lookup_args(#state { open = Open}) ->
|
2012-01-07 22:35:27 +00:00
|
|
|
?LET(Name, g_non_empty_btree(Open),
|
2012-01-07 21:58:38 +00:00
|
|
|
?LET(Key, g_existing_key(Name, Open),
|
2012-01-07 11:43:49 +00:00
|
|
|
[Name, Key])).
|
|
|
|
|
|
|
|
cmd_delete_args(#state { open = Open}) ->
|
2012-01-07 22:35:27 +00:00
|
|
|
?LET(Name, g_non_empty_btree(Open),
|
2012-01-07 21:58:38 +00:00
|
|
|
?LET(Key, g_existing_key(Name, Open),
|
2012-01-07 11:43:49 +00:00
|
|
|
[Name, Key])).
|
|
|
|
|
|
|
|
|
|
|
|
%% Context management
|
|
|
|
%% ----------------------------------------------------------------------
|
|
|
|
cleanup_test_trees(#state { open = Open}) ->
|
|
|
|
[cleanup_tree(N) || N <- dict:fetch_keys(Open)].
|
|
|
|
|
|
|
|
cleanup_tree(Tree) ->
|
|
|
|
{ok, FileNames} = file:list_dir(Tree),
|
|
|
|
[ok = file:delete(filename:join([Tree, Fname])) || Fname <- FileNames],
|
|
|
|
file:del_dir(Tree).
|
|
|
|
|
|
|
|
%% Various Helper routines
|
|
|
|
%% ----------------------------------------------------------------------
|
|
|
|
|
2012-01-07 21:58:38 +00:00
|
|
|
open_dicts_with_keys(#state { open = Open}) ->
|
2012-01-07 23:01:18 +00:00
|
|
|
lists:any(fun({_, D}) -> dict:size(D) > 0 end,
|
|
|
|
dict:to_list(Open)).
|
2012-01-07 21:58:38 +00:00
|
|
|
|
|
|
|
open_dicts(#state { open = Open}) ->
|
|
|
|
dict:size(Open) > 0.
|
|
|
|
|
|
|
|
closed_dicts(#state { closed = Closed}) ->
|
|
|
|
dict:size(Closed) > 0.
|
|
|
|
|
|
|
|
|