Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vagabond committed Jan 9, 2020
1 parent 2433cc5 commit 703b034
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
19 changes: 16 additions & 3 deletions src/blockchain.erl
Expand Up @@ -1491,6 +1491,11 @@ blocks_test() ->
meck:expect(blockchain_election, has_new_group, fun(_) ->
false
end),
meck:new(blockchain_swarm, [passthrough]),
meck:expect(blockchain_swarm, pubkey_bin, fun() ->
crypto:strong_rand_bytes(33)
end),

{ok, Pid} = blockchain_lock:start_link(),

#{secret := Priv, public := Pub} = libp2p_crypto:generate_keys(ecc_compact),
Expand Down Expand Up @@ -1530,7 +1535,10 @@ blocks_test() ->
?assert(meck:validate(blockchain_worker)),
meck:unload(blockchain_worker),
?assert(meck:validate(blockchain_election)),
meck:unload(blockchain_election).
meck:unload(blockchain_election),
?assert(meck:validate(blockchain_swarm)),
meck:unload(blockchain_swarm).



get_block_test() ->
Expand All @@ -1553,7 +1561,10 @@ get_block_test() ->
meck:expect(blockchain_election, has_new_group, fun(_) ->
false
end),

meck:new(blockchain_swarm, [passthrough]),
meck:expect(blockchain_swarm, pubkey_bin, fun() ->
crypto:strong_rand_bytes(33)
end),

{ok, Pid} = blockchain_lock:start_link(),

Expand Down Expand Up @@ -1590,7 +1601,9 @@ get_block_test() ->
?assert(meck:validate(blockchain_worker)),
meck:unload(blockchain_worker),
?assert(meck:validate(blockchain_election)),
meck:unload(blockchain_election).
meck:unload(blockchain_election),
?assert(meck:validate(blockchain_swarm)),
meck:unload(blockchain_swarm).


-endif.
3 changes: 2 additions & 1 deletion test/blockchain_simple_SUITE.erl
Expand Up @@ -77,7 +77,8 @@ init_per_testcase(TestCase, Config) ->
BaseDir = "data/test_SUITE/" ++ erlang:atom_to_list(TestCase),
Balance = 5000,
{ok, Sup, {PrivKey, PubKey}, Opts} = test_utils:init(BaseDir),
{ok, GenesisMembers, ConsensusMembers, Keys} = test_utils:init_chain(Balance, {PrivKey, PubKey}),
%% two tests rely on the swarm not being in the consensus group, so exclude them here
{ok, GenesisMembers, ConsensusMembers, Keys} = test_utils:init_chain(Balance, {PrivKey, PubKey}, not lists:member(TestCase, [bogus_coinbase_test, bogus_coinbase_with_good_payment_test])),

Chain = blockchain_worker:blockchain(),
Swarm = blockchain_swarm:swarm(),
Expand Down
22 changes: 15 additions & 7 deletions test/test_utils.erl
Expand Up @@ -5,7 +5,7 @@
-include("blockchain_vars.hrl").

-export([
init/1, init_chain/2,
init/1, init_chain/2, init_chain/3,
generate_keys/1, generate_keys/2,
wait_until/1, wait_until/3,
create_block/2,
Expand All @@ -30,13 +30,21 @@ init(BaseDir) ->
?assert(erlang:is_pid(blockchain_swarm:swarm())),
{ok, Sup, {PrivKey, PubKey}, Opts}.

init_chain(Balance, {PrivKey, PubKey}) ->
init_chain(Balance, Keys) ->
init_chain(Balance, Keys, true).

init_chain(Balance, {PrivKey, PubKey}, InConsensus) ->
% Generate fake blockchains (just the keys)
RandomKeys = test_utils:generate_keys(10),
Address = blockchain_swarm:pubkey_bin(),
GenesisMembers = [
{Address, {PubKey, PrivKey, libp2p_crypto:mk_sig_fun(PrivKey)}}
] ++ RandomKeys,
GenesisMembers = case InConsensus of
true ->
RandomKeys = test_utils:generate_keys(10),
Address = blockchain_swarm:pubkey_bin(),
[
{Address, {PubKey, PrivKey, libp2p_crypto:mk_sig_fun(PrivKey)}}
] ++ RandomKeys;
false ->
test_utils:generate_keys(11)
end,

% Create genesis block
{InitialVars, Keys} = blockchain_ct_utils:create_vars(#{}),
Expand Down

0 comments on commit 703b034

Please sign in to comment.