Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shapovalovts committed Jul 13, 2024
1 parent 007cc3d commit ffb0e4b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.PHONY: run-ghead run-chead
.PHONY: test ftest
.PHONY: cb cr
.PHONY: dialyzer format lint
.PHONY: dialyzer format static_checks

COG = cog.py
REBAR = ./rebar3
Expand Down Expand Up @@ -99,8 +99,9 @@ dialyzer: ##@DEV Run dialyzer
format: ##@DEV Format erlang code
$(REBAR) format

lint: ##@DEV Run erlang linter to validate the code
static_checks: ##@DEV Run erlang static checks to validate the code
$(REBAR) lint
$(REBAR) hunk

tmux: ##@DEV Run tmux with Sky Port development layout
SOURCES_PARENT_DIR=~/projects tmuxinator
Expand Down
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
}
]}.

{plugins, [rebar3_format, rebar3_depup, rebar3_lint]}.
{plugins, [rebar3_format, rebar3_depup, rebar3_lint, rebar3_hank]}.

{format, [
{files, ["src/**/*.?rl", "include/*.hrl", "test/**/*.?rl"]},
Expand Down
14 changes: 14 additions & 0 deletions src/srv/cloud/wm_virtres.erl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ terminate(State, StateName, #mstate{job_id = JobId}) ->
%% ============================================================================

-spec sleeping({call, pid()} | cast | info, term(), #mstate{}) -> {atom(), atom(), #mstate{}}.
sleeping({call, From}, get_current_state, MState) ->
{keep_state, MState, [{reply, From, ?FUNCTION_NAME}]};
sleeping(cast,
activate,
#mstate{task_id = ID,
Expand All @@ -102,6 +104,8 @@ sleeping(cast, Msg, MState) ->
handle_event(Msg, ?FUNCTION_NAME, MState).

-spec validating({call, pid()} | cast | info, term(), #mstate{}) -> {atom(), atom(), #mstate{}}.
validating({call, From}, get_current_state, MState) ->
{keep_state, MState, [{reply, From, ?FUNCTION_NAME}]};
validating(cast,
{partition_exists, Ref, false},
#mstate{action = create,
Expand Down Expand Up @@ -161,6 +165,8 @@ validating(cast, Msg, MState) ->
handle_event(Msg, ?FUNCTION_NAME, MState).

-spec creating({call, pid()} | cast | info, term(), #mstate{}) -> {atom(), atom(), #mstate{}}.
creating({call, From}, get_current_state, MState) ->
{keep_state, MState, [{reply, From, ?FUNCTION_NAME}]};
creating(cast, {partition_spawned, Ref, NewPartExtId}, #mstate{job_id = JobId, wait_ref = Ref} = MState) ->
?LOG_INFO("Partition spawned => check status: ~p, job ~p", [NewPartExtId, JobId]),
Timer = wm_virtres_handler:wait_for_partition_fetch(),
Expand Down Expand Up @@ -236,6 +242,8 @@ creating(cast, Msg, MState) ->
handle_event(Msg, ?FUNCTION_NAME, MState).

-spec uploading({call, pid()} | cast | info, term(), #mstate{}) -> {atom(), atom(), #mstate{}}.
uploading({call, From}, get_current_state, MState) ->
{keep_state, MState, [{reply, From, ?FUNCTION_NAME}]};
uploading(cast, {Ref, ok}, #mstate{upload_ref = Ref, job_id = JobId} = MState) ->
?LOG_INFO("Uploading has finished (~p)", [Ref]),
?LOG_DEBUG("Let the job be scheduled again with preset nodes request (~p)", [JobId]),
Expand All @@ -253,6 +261,8 @@ uploading(cast, Msg, MState) ->
handle_event(Msg, ?FUNCTION_NAME, MState).

-spec running({call, pid()} | cast | info, term(), #mstate{}) -> {atom(), atom(), #mstate{}}.
running({call, From}, get_current_state, MState) ->
{keep_state, MState, [{reply, From, ?FUNCTION_NAME}]};
running(cast, {Ref, Status}, #mstate{} = MState) ->
?LOG_INFO("Got orphaned message with reference ~p [running, ~p]", [Ref, Status]),
{next_state, running, MState};
Expand All @@ -262,6 +272,8 @@ running(cast, Msg, MState) ->
handle_event(Msg, ?FUNCTION_NAME, MState).

-spec downloading({call, pid()} | cast | info, term(), #mstate{}) -> {atom(), atom(), #mstate{}}.
downloading({call, From}, get_current_state, MState) ->
{keep_state, MState, [{reply, From, ?FUNCTION_NAME}]};
downloading(cast, {Ref, ok}, #mstate{download_ref = Ref, job_id = JobId} = MState) ->
?LOG_INFO("Downloading has finished => delete entities [~p, ~p]", [Ref, JobId]),
stop_port_forwarding(JobId),
Expand All @@ -284,6 +296,8 @@ downloading(cast, Msg, MState) ->
handle_event(Msg, ?FUNCTION_NAME, MState).

-spec destroying({call, pid()} | cast | info, term(), #mstate{}) -> {atom(), atom(), #mstate{}}.
destroying({call, From}, get_current_state, MState) ->
{keep_state, MState, [{reply, From, ?FUNCTION_NAME}]};
destroying(cast,
start_destroying,
#mstate{task_id = TaskId,
Expand Down
6 changes: 3 additions & 3 deletions test/wm_ct_helpers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ kill_gate_system_process() ->

-spec get_fsm_state_name(pid()) -> {ok, atom()}.
get_fsm_state_name(Pid) ->
{status, _, {module, gen_fsm}, [_, running, _, _, InfoList]} = sys:get_status(Pid),
{status, _, {module, gen_statem}, [_, running, _, _, InfoList]} = sys:get_status(Pid),
GetStatus =
fun ({"StateName", _}) ->
fun ({"Status", _}) ->
true;
(_) ->
false
Expand All @@ -67,5 +67,5 @@ get_fsm_state_name(Pid) ->
false
end,
{value, {data, DataList}} = lists:search(GetResult, InfoList),
{value, {"StateName", StateName}} = lists:search(GetStatus, DataList),
{value, {"Status", StateName}} = lists:search(GetStatus, DataList),
StateName.
50 changes: 25 additions & 25 deletions test/wm_virtres_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ activate(Config) ->
RequestPartitionExistence = fun(_, _) -> {ok, WaitRef} end,
meck:expect(wm_virtres_handler, request_partition_existence, RequestPartitionExistence),

?assertEqual(sleeping, wm_ct_helpers:get_fsm_state_name(Pid)),
ok = gen_fsm:send_event(Pid, activate),
?assertEqual(validating, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
?assertEqual(sleeping, gen_statem:call(Pid, get_current_state)),
ok = gen_statem:cast(Pid, activate),
?assertEqual(validating, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

Expand All @@ -133,8 +133,8 @@ part_absent_create(Config) ->
SpawnPartition = fun(_, _) -> {ok, WaitRef} end,
meck:expect(wm_virtres_handler, spawn_partition, SpawnPartition),

ok = gen_fsm:send_event(Pid, {partition_exists, WaitRef, false}),
?assertEqual(creating, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
ok = gen_statem:cast(Pid, {partition_exists, WaitRef, false}),
?assertEqual(creating, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

Expand All @@ -149,8 +149,8 @@ part_spawned(Config) ->

meck:expect(wm_virtres_handler, request_partition, fun(X, _) when X == JobId -> {ok, WaitRef} end),

ok = gen_fsm:send_event(Pid, {partition_spawned, WaitRef, PartExtId}),
?assertEqual(creating, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
ok = gen_statem:cast(Pid, {partition_spawned, WaitRef, PartExtId}),
?assertEqual(creating, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

Expand All @@ -164,8 +164,8 @@ part_fetched_not_up(Config) ->

meck:expect(wm_virtres_handler, wait_for_partition_fetch, fun() -> erlang:make_ref() end),

ok = gen_fsm:send_event(Pid, {partition_fetched, WaitRef, Part}),
?assertEqual(creating, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
ok = gen_statem:cast(Pid, {partition_fetched, WaitRef, Part}),
?assertEqual(creating, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

Expand All @@ -181,8 +181,8 @@ part_fetched_up(Config) ->
meck:expect(wm_virtres_handler, ensure_entities_created, fun(_, X, _) when X == Part -> {ok, PartMgrNodeId} end),
meck:expect(wm_virtres_handler, wait_for_wm_resources_readiness, fun() -> erlang:make_ref() end),

ok = gen_fsm:send_event(Pid, {partition_fetched, WaitRef, Part}),
?assertEqual(creating, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
ok = gen_statem:cast(Pid, {partition_fetched, WaitRef, Part}),
?assertEqual(creating, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

Expand All @@ -195,10 +195,10 @@ uploading_is_about_to_start(Config) ->
meck:expect(wm_virtres_handler, is_job_partition_ready, fun(X) when X == JobId -> false end),

erlang:send(Pid, readiness_check),
?assertEqual(creating, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
?assertEqual(creating, gen_statem:call(Pid, get_current_state)),

erlang:send(Pid, readiness_check),
?assertEqual(creating, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
?assertEqual(creating, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

Expand All @@ -215,7 +215,7 @@ uploading_started(Config) ->
meck:expect(wm_virtres_handler, update_job, fun(_, X) when X == JobId -> 1 end),

erlang:send(Pid, part_check),
?assertEqual(uploading, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
?assertEqual(uploading, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

Expand All @@ -229,8 +229,8 @@ uploading_done(Config) ->

meck:expect(wm_virtres_handler, update_job, fun(_, X) when X == JobId -> 1 end),

ok = gen_fsm:send_event(Pid, {WaitRef, ok}),
?assertEqual(running, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
ok = gen_statem:cast(Pid, {WaitRef, ok}),
?assertEqual(running, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

Expand All @@ -247,8 +247,8 @@ downloading_started(Config) ->
start_job_data_downloading,
fun(X, Y, _) when X == PartMgrNodeId andalso Y == JobId -> {ok, WaitRef, ["/tmp/f1", "/tmp/f2"]} end),

ok = gen_fsm:send_all_state_event(Pid, job_finished),
?assertEqual(downloading, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
ok = gen_statem:cast(Pid, job_finished),
?assertEqual(downloading, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

Expand All @@ -262,8 +262,8 @@ downloading_done(Config) ->

meck:expect(wm_virtres_handler, remove_relocation_entities, fun(X) when X == JobId -> ok end),

ok = gen_fsm:send_event(Pid, {WaitRef, ok}),
?assertEqual(destroying, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
ok = gen_statem:cast(Pid, {WaitRef, ok}),
?assertEqual(destroying, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

Expand All @@ -275,8 +275,8 @@ part_destraction_in_progress(Config) ->
WaitRef = proplists:get_value(wait_ref, Config),
PartExtId = proplists:get_value(part_ext_id, Config),

ok = gen_fsm:send_event(Pid, {delete_in_progress, WaitRef, PartExtId}),
?assertEqual(destroying, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
ok = gen_statem:cast(Pid, {delete_in_progress, WaitRef, PartExtId}),
?assertEqual(destroying, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

Expand All @@ -288,7 +288,7 @@ deactivate(Config) ->
WaitRef = proplists:get_value(wait_ref, Config),
PartExtId = proplists:get_value(part_ext_id, Config),

ok = gen_fsm:send_event(Pid, {partition_deleted, WaitRef, PartExtId}),
ok = gen_statem:cast(Pid, {partition_deleted, WaitRef, PartExtId}),
?assert(lists:any(fun(_) ->
timer:sleep(500),
not is_process_alive(Pid)
Expand All @@ -302,7 +302,7 @@ part_exists_detete(Config) ->

WaitRef = proplists:get_value(wait_ref, Config),

ok = gen_fsm:send_event(Pid, {partition_exists, WaitRef, true}),
?assertEqual(destroying, gen_fsm:sync_send_all_state_event(Pid, get_current_state)),
ok = gen_statem:cast(Pid, {partition_exists, WaitRef, true}),
?assertEqual(destroying, gen_statem:call(Pid, get_current_state)),
timer:sleep(1000),
?assert(is_process_alive(Pid)).

0 comments on commit ffb0e4b

Please sign in to comment.