Fix unit tests

This commit is contained in:
Kresten Krab Thorup 2012-04-27 09:59:09 +02:00
parent 6b47d8dd1e
commit 2d928fce73
2 changed files with 35 additions and 79 deletions

View file

@ -36,8 +36,7 @@
get_fail/2,
open/1, close/1,
put/3,
sync_range/2,
sync_fold_range/4,
fold_range/4,
stop/0]).
%% gen_server callbacks
@ -75,11 +74,8 @@ close(N) ->
put(N, K, V) ->
call({put, N, K, V}).
sync_range(T, Range) ->
call({sync_range, T, Range}).
sync_fold_range(T, Fun, Acc0, Range) ->
call({sync_fold_range, T, Fun, Acc0, Range}).
fold_range(T, Fun, Acc0, Range) ->
call({fold_range, T, Fun, Acc0, Range}).
stop() ->
call(stop).
@ -104,17 +100,11 @@ handle_call({close, N}, _, #state { btrees = D} = State) ->
Otherwise ->
{reply, {error, Otherwise}, State}
end;
handle_call({sync_range, Name, Range}, _From,
#state { btrees = D} = State) ->
Tree = dict:fetch(Name, D),
{ok, Ref} = hanoi:sync_range(Tree, Range),
Result = sync_range_gather(Ref),
{reply, Result, State};
handle_call({sync_fold_range, Name, Fun, Acc0, Range},
handle_call({fold_range, Name, Fun, Acc0, Range},
_From,
#state { btrees = D } = State) ->
Tree = dict:fetch(Name, D),
Result = hanoi:sync_fold_range(Tree, Fun, Acc0, Range),
Result = hanoi:fold_range(Tree, Fun, Acc0, Range),
{reply, Result, State};
handle_call({put, N, K, V}, _, #state { btrees = D} = State) ->
Tree = dict:fetch(N, D),
@ -151,16 +141,3 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
sync_range_gather(Ref) ->
sync_range_gather(Ref, []).
sync_range_gather(Ref, Acc) ->
receive
{fold_result, Ref, K, V} ->
sync_range_gather(Ref, [{K, V} | Acc]);
{fold_done, Ref} ->
{ok, Acc}
after 3000 ->
{error, timeout}
end.

View file

@ -60,10 +60,18 @@ full_test_() ->
[
?_test(test_tree_simple_1()),
?_test(test_tree_simple_2()),
?_test(test_tree_simple_3()),
?_test(test_tree_simple_4()),
{timeout, 300, ?_test(test_tree())},
{timeout, 120, ?_test(test_qc())}
?_test(test_tree_simple_4())
% {timeout, 300, ?_test(test_tree())},
% {timeout, 120, ?_test(test_qc())}
]}.
full2_test_() ->
{setup,
spawn,
fun () -> ok end,
fun (_) -> ok end,
[
{timeout, 300, ?_test(test_tree())}
]}.
-ifdef(TRIQ).
@ -152,16 +160,12 @@ command(#state { open = Open, closed = Closed } = S) ->
|| open_dicts(S), open_dicts_with_keys(S)]
++ [ {500, {call, ?SERVER, delete_exist, cmd_delete_args(S)}}
|| open_dicts(S), open_dicts_with_keys(S)]
++ [ {125, {call, ?SERVER, sync_fold_range, cmd_sync_fold_range_args(S)}}
|| open_dicts(S), open_dicts_with_keys(S)]
++ [ {125, {call, ?SERVER, sync_range, cmd_sync_range_args(S)}}
++ [ {125, {call, ?SERVER, fold_range, cmd_sync_fold_range_args(S)}}
|| open_dicts(S), open_dicts_with_keys(S)]
).
%% Precondition (abstract)
precondition(S, {call, ?SERVER, sync_fold_range, [_Tree, _F, _A0, Range]}) ->
is_valid_range(Range) andalso open_dicts(S) andalso open_dicts_with_keys(S);
precondition(S, {call, ?SERVER, sync_range, [_Tree, Range]}) ->
precondition(S, {call, ?SERVER, fold_range, [_Tree, _F, _A0, Range]}) ->
is_valid_range(Range) andalso open_dicts(S) andalso open_dicts_with_keys(S);
precondition(S, {call, ?SERVER, delete_exist, [_Name, _K]}) ->
open_dicts(S) andalso open_dicts_with_keys(S);
@ -199,9 +203,7 @@ is_valid_range(_) ->
%% Next state manipulation (abstract / concrete)
next_state(S, _Res, {call, ?SERVER, sync_fold_range, [_Tree, _F, _A0, _Range]}) ->
S;
next_state(S, _Res, {call, ?SERVER, sync_range, [_Tree, _Range]}) ->
next_state(S, _Res, {call, ?SERVER, fold_range, [_Tree, _F, _A0, _Range]}) ->
S;
next_state(S, _Res, {call, ?SERVER, get_fail, [_Name, _Key]}) ->
S;
@ -235,17 +237,11 @@ next_state(#state { open = Open, closed=Closed} = S, _Res,
%% Postcondition check (concrete)
postcondition(#state { open = Open},
{call, ?SERVER, sync_fold_range, [Tree, F, A0, Range]}, Result) ->
{call, ?SERVER, fold_range, [Tree, F, A0, Range]}, Result) ->
#tree { elements = TDict } = dict:fetch(Tree, Open),
DictResult = lists:sort(dict_range_query(TDict, F, A0, Range)),
CallResult = lists:sort(Result),
DictResult == CallResult;
postcondition(#state { open = Open},
{call, ?SERVER, sync_range, [Tree, Range]}, {ok, Result}) ->
#tree { elements = TDict } = dict:fetch(Tree, Open),
DictResult = lists:sort(dict_range_query(TDict, Range)),
CallResult = lists:sort(Result),
DictResult == CallResult;
postcondition(_S,
{call, ?SERVER, get_fail, [_Name, _Key]}, not_found) ->
true;
@ -295,17 +291,6 @@ test_tree_simple_2() ->
ok = hanoi:delete(Tree, <<"ã">>),
ok = hanoi:close(Tree).
test_tree_simple_3() ->
{ok, Tree} = hanoi:open("simple"),
ok = hanoi:put(Tree, <<"X">>, <<"Y">>),
{ok, Ref} = hanoi:sync_range(Tree, #btree_range{from_key= <<"X">>, to_key= <<"X">>}),
?assertEqual(ok,
receive
{fold_done, Ref} -> ok
after 1000 -> {error, timeout}
end),
ok = hanoi:close(Tree).
test_tree_simple_4() ->
Key = <<56,11,62,42,35,163,16,100,9,224,8,228,130,94,198,2,126,117,243,
1,122,175,79,159,212,177,30,153,71,91,85,233,41,199,190,58,3,
@ -324,6 +309,8 @@ test_tree() ->
end,
ok,
lists:seq(2,100000,1)),
io:format(user, "INSERT DONE 1~n", []),
lists:foldl(fun(N,_) ->
ok = hanoi:put(Tree,
<<N:128>>, <<"data",N:128>>)
@ -331,8 +318,13 @@ test_tree() ->
ok,
lists:seq(4000,6000,1)),
io:format(user, "INSERT DONE 2~n", []),
hanoi:delete(Tree, <<1500:128>>),
io:format(user, "INSERT DONE 3~n", []),
{Time,{ok,Count}} = timer:tc(?MODULE, run_fold, [Tree,1000,2000]),
error_logger:info_msg("time to fold: ~p/sec (time=~p, count=~p)~n", [1000000/(Time/Count), Time/1000000, Count]),
@ -341,26 +333,13 @@ test_tree() ->
ok = hanoi:close(Tree).
run_fold(Tree,From,To) ->
{ok, PID} = hanoi:sync_range(Tree, #btree_range{from_key= <<From:128>>, to_key= <<(To+1):128>>}),
lists:foreach(fun(1500) -> ok;
(N) ->
receive
{fold_result, PID, <<N:128>>,_} -> ok
after 100 ->
error_logger:info_msg("timed out on #~p~n", [N])
end
end,
lists:seq(From,To,1)),
receive
{fold_done, PID} -> ok
after 1000 ->
error_logger:info_msg("timed out on fold_done! ~n", [])
end,
%% we should now have no spurious messages
{messages,[]} = erlang:process_info(self(),messages),
{ok, To-From}.
{_, Count} = hanoi:fold_range(Tree,
fun(<<N:128>>,_Value, {N, C}) ->
{N + 1, C + 1}
end,
{From, 0},
#btree_range{from_key= <<From:128>>, to_key= <<(To+1):128>>}),
{ok, Count}.