Skip to content

Commit

Permalink
test: better mock load/unload handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPiechota committed Dec 26, 2024
1 parent 54a9bfc commit a66d921
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
43 changes: 35 additions & 8 deletions apps/arweave/test/ar_test_node.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
wait_until_height/1, wait_until_height/2, wait_until_height/3,
assert_wait_until_height/2, http_get_block/2, get_blocks/1,
mock_to_force_invalid_h1/0, get_difficulty_for_invalid_hash/0, invalid_solution/0,
valid_solution/0, remote_call/4, load_fixture/1,
valid_solution/0, new_mock/2, mock_function/3, unmock_module/1, remote_call/4,
load_fixture/1,
get_default_storage_module_packing/2]).

%% The "legacy" interface.
Expand Down Expand Up @@ -1044,30 +1045,55 @@ get_tx_confirmations(Node, TXID) ->
-1
end.

new_mock(Module, Options) ->
try
meck:new(Module, Options)
catch
error:E ->
?LOG_ERROR("Error creating mock for ~p: ~p", [Module, E])
end.

mock_function(Module, Fun, Mock) ->
try
meck:expect(Module, Fun, Mock)
catch
error:E ->
?LOG_ERROR("Error setting mock for ~p: ~p", [Module, E])
end.

unmock_module(Module) ->
try
meck:unload(Module)
catch
error:E ->
?LOG_ERROR("Error unloading mock for ~p: ~p", [Module, E])
end.

mock_functions(Functions) ->
{
fun() ->
lists:foldl(
fun({Module, Fun, Mock}, Mocked) ->
NewMocked = case maps:get(Module, Mocked, false) of
false ->
meck:new(Module, [passthrough]),
new_mock(Module, [passthrough]),
lists:foreach(
fun(Node) ->
remote_call(Node, meck, new,
remote_call(Node, ar_test_node, new_mock,
[Module, [no_link, passthrough]])
end,
all_peers()),
maps:put(Module, true, Mocked);
true ->
Mocked
end,
mock_function(Module, Fun, Mock),
lists:foreach(
fun(Node) ->
meck:expect(Module, Fun, Mock),
remote_call(Node, meck, expect, [Module, Fun, Mock])
remote_call(Node, ar_test_node, mock_function,
[Module, Fun, Mock])
end,
[main | all_peers()]),
all_peers()),
NewMocked
end,
maps:new(),
Expand All @@ -1077,11 +1103,12 @@ mock_functions(Functions) ->
fun(Mocked) ->
maps:fold(
fun(Module, _, _) ->
unmock_module(Module),
lists:foreach(
fun(Node) ->
remote_call(Node, meck, unload, [Module])
remote_call(Node, ar_test_node, unmock_module, [Module])
end,
[main | all_peers()])
all_peers())
end,
noop,
Mocked
Expand Down
2 changes: 1 addition & 1 deletion apps/arweave/test/ar_vdf_server_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ test_vdf_client_fast_block() ->
%% to the VDF client allowing it to validate teh block.
send_new_block(ar_test_node:peer_ip(main), B1),
%% If all is right, the VDF server should push the old and new VDF sessions allowing
%% the VDF clietn to finally validate the block.
%% the VDF client to finally validate the block.
BI = assert_wait_until_height(peer1, 1).

test_vdf_client_fast_block_pull_interface() ->
Expand Down

0 comments on commit a66d921

Please sign in to comment.