Skip to content

Commit

Permalink
Rework integration test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Jun 14, 2024
1 parent 2ad6dfc commit 572f130
Showing 1 changed file with 61 additions and 48 deletions.
109 changes: 61 additions & 48 deletions integration_test/extra_code_paths/path1/dummy_helper.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
-module(dummy_helper).

-required_variable(#{name=>dummy_var, description=>"dummy_var",
default_value=>default_value}).
-include_lib("stdlib/include/assert.hrl").

-required_variable(#{name => dummy_var,
description => "dummy_var",
default_value => default_value}).

-define(comment(U), binary_to_list(iolist_to_binary(io_lib:format("Condition failed with last users distribution ~n~p", [U])))).

%% amoc_dist testing function
-export([test_amoc_dist/0]).
Expand All @@ -11,65 +16,73 @@ test_amoc_dist() ->
Master = amoc_cluster:master_node(),
Slaves = amoc_cluster:slave_nodes(),
%% check the status of the nodes
disabled = rpc:call(Master, amoc_controller, get_status, []),
[{running, #{scenario := dummy_scenario}} = rpc:call(Node, amoc_controller, get_status, [])
|| Node <- Slaves],
%% check user ids
{N1, Nodes1, Ids1, Max1} = get_users_info(Slaves),
true = N1 > 0,
N1 = Max1,
Ids1 = lists:seq(1, N1),
?assertEqual(disabled, get_status(Master)),
[ ?assertMatch({running, #{scenario := dummy_scenario}}, get_status(Node)) || Node <- Slaves],
%% check user ids, users have all been started at the first two nodes
{N1, Max1, Nodes1, Ids1, Users1} = get_users_info(Slaves),
?assert(N1 > 0),
?assertEqual(N1, Max1, ?comment(Users1)),
?assertEqual(Ids1, lists:seq(1, N1), ?comment(Users1)),
[AddedNode] = Slaves -- Nodes1,
%% add 20 users
{ok, _} = rpc:call(Master, amoc_dist, add, [20]),
timer:sleep(3000),
{N2, Nodes2, Ids2, Max2} = get_users_info(Slaves),
N2 = Max2,
Ids2 = lists:seq(1, N2),
[AddedNode] = Nodes2 -- Nodes1,
N2 = N1 + 20,
add_and_wait(Master, 20),
{N2, Max2, Nodes2, Ids2, Users2} = get_users_info(Slaves),
?assertEqual(N2, Max2, ?comment(Users2)),
?assertEqual(Ids2, lists:seq(1, N2), ?comment(Users2)),
?assertEqual([AddedNode], Nodes2 -- Nodes1, ?comment(Users2)),
?assertEqual(N2, N1 + 20, ?comment(Users2)),
%% remove 10 users
{ok, _} = rpc:call(Master, amoc_dist, remove, [10, true]),
timer:sleep(3000),
{N3, Nodes3, _Ids3, Max3} = get_users_info(Slaves),
Nodes2 = Nodes3,
Max3 = Max2,
N2 = N3 + 10,
remove_and_wait(Master, 10),
{N3, Max3, Nodes3, _Ids3, Users3} = get_users_info(Slaves),
?assertEqual(N2 - 10, N3, ?comment(Users3)),
?assertEqual(Max2, Max3, ?comment([Users2, Users3])),
?assertEqual(Nodes2, Nodes3, ?comment(Users3)),
%% try to remove N3 users
{ok, Ret} = rpc:call(Master, amoc_dist, remove, [N3, true]),
Ret = remove_and_wait(Master, N3),
RemovedN = lists:sum([N || {_, N} <- Ret]),
timer:sleep(3000),
{N4, Nodes4, Ids4, _Max4} = get_users_info(Slaves),
Nodes1 = Nodes4,
N3 = N4 + RemovedN,
true = RemovedN < N3,
{N4, _Max4, Nodes4, Ids4, Users4} = get_users_info(Slaves),
?assertEqual(N3, N4 + RemovedN, ?comment(Users4)),
?assertEqual(Nodes1, Nodes4, ?comment(Users4)),
?assert(RemovedN < N3),
%% add 20 users
{ok, _} = rpc:call(Master, amoc_dist, add, [20]),
timer:sleep(3000),
{N5, Nodes5, Ids5, Max5} = get_users_info(Slaves),
Nodes2 = Nodes5,
Max5 = Max2 + 20,
N5 = N4 + 20,
true = Ids5 -- Ids4 =:= lists:seq(Max2 + 1, Max5),
add_and_wait(Master, 20),
{N5, Max5, Nodes5, Ids5, Users5} = get_users_info(Slaves),
?assertEqual(Nodes2, Nodes5, ?comment(Users5)),
?assertEqual(Max5, Max2 + 20, ?comment(Users5)),
?assertEqual(N5, N4 + 20, ?comment(Users5)),
?assertEqual(Ids5 -- Ids4, lists:seq(Max2 + 1, Max5), ?comment(Users5)),
%% terminate scenario
{ok,_} = rpc:call(Master, amoc_dist, stop, []),
timer:sleep(3000),
[{finished, dummy_scenario} = rpc:call(Node, amoc_controller, get_status, [])
|| Node <- Slaves],
stop(Master),
[ ?assertEqual({finished, dummy_scenario}, get_status(Node)) || Node <- Slaves],
%% return expected value
amoc_dist_works_as_expected
catch
C:E:S ->
{error, {C, E, S}}
{C, E, S}
end.

get_users_info(SlaveNodes) ->
Users = [{Node, Id} ||
Node <- SlaveNodes,
{_Pid, Id} <- rpc:call(Node, amoc_users_sup, get_all_children, [])],
Ids = lists:usort([Id || {_, Id} <- Users]),
Nodes = lists:usort([Node || {Node, _} <- Users]),
Distrib = [ {Node, erpc:call(Node, amoc_users_sup, get_all_children, [])} || Node <- SlaveNodes ],
Ids = lists:usort([Id || {_Node, Users} <- Distrib, {_, Id} <- Users]),
Nodes = lists:usort([Node || {Node, Users} <- Distrib, [] =/= Users]),
N = length(Ids),
N = length(Users),
MaxId = lists:max(Ids),
{N, Nodes, Ids, MaxId}.
{N, MaxId, Nodes, Ids, Distrib}.

add_and_wait(Master, Num) ->
{ok, Ret} = erpc:call(Master, amoc_dist, add, [Num]),
timer:sleep(3000),
Ret.

remove_and_wait(Master, Num) ->
{ok, Ret} = erpc:call(Master, amoc_dist, remove, [Num, true]),
timer:sleep(3000),
Ret.

stop(Master) ->
{ok, Ret} = erpc:call(Master, amoc_dist, stop, []),
timer:sleep(3000),
Ret.

get_status(Node) ->
erpc:call(Node, amoc_controller, get_status, []).

0 comments on commit 572f130

Please sign in to comment.