Skip to content

Commit

Permalink
Update small tests with module opts in maps
Browse files Browse the repository at this point in the history
Do not maintain tests for lists and maps as this state is temporary.
  • Loading branch information
chrzaszcz committed Feb 22, 2022
1 parent 0bdb19f commit 2ba28b5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 76 deletions.
95 changes: 49 additions & 46 deletions test/gen_mod_deps_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ all() ->
fails_on_dependency_cycle,
succeeds_on_cycle_with_soft_dep_in_path,
succeeds_on_adding_soft_dependency_cycle_edge,
forces_dependency_args,
appends_dependencies_args,
forces_dependency_opts,
appends_dependencies_opts,
optional_dependencies_are_not_started_by_themselves,
optional_dependencies_add_arguments,
overrides_dependency_args,
merges_dependency_args
optional_dependencies_add_opts,
overrides_dependency_opts,
merges_dependency_opts
].

%% Fixtures
Expand All @@ -34,64 +34,67 @@ end_per_testcase(_, _C) ->
%% Tests

no_dependencies(_Config) ->
Modules = #{mod_a => [], mod_b => []},
Modules = #{mod_a => #{}, mod_b => #{}},
?assertEqual(Modules, maps:from_list(add_deps(Modules))).

dependency(_Config) ->
set_deps(#{mod_a => [{mod_b, hard}]}),
?assertMatch([{mod_b, _}, {mod_a, _}], add_deps(#{mod_a => []})).
set_deps(#{mod_a => [{mod_b, #{}, hard}]}),
?assertMatch([{mod_b, _}, {mod_a, _}], add_deps(#{mod_a => #{}})).

dependency_chain(_Config) ->
set_deps(#{mod_a => [{mod_b, hard}], mod_b => [{mod_c, hard}]}),
?assertMatch([{mod_c, _}, {mod_b, _}, {mod_a, _}], add_deps(#{mod_a => []})).
set_deps(#{mod_a => [{mod_b, #{}, hard}], mod_b => [{mod_c, #{}, hard}]}),
?assertMatch([{mod_c, _}, {mod_b, _}, {mod_a, _}], add_deps(#{mod_a => #{}})).

dependency_dag(_Config) ->
set_deps(#{mod_a => [{mod_b, hard}, {mod_c, hard}],
mod_b => [{mod_c, hard}]}),
?assertMatch([{mod_c, _}, {mod_b, _}, {mod_a, _}], add_deps(#{mod_a => []})).
set_deps(#{mod_a => [{mod_b, #{}, hard}, {mod_c, #{}, hard}],
mod_b => [{mod_c, #{}, hard}]}),
?assertMatch([{mod_c, _}, {mod_b, _}, {mod_a, _}], add_deps(#{mod_a => #{}})).

fails_on_dependency_cycle(_Config) ->
set_deps(#{mod_a => [{mod_b, hard}],
mod_b => [{mod_c, hard}],
mod_c => [{mod_a, hard}]}),
?assertError(_, add_deps(#{mod_a => []})).
set_deps(#{mod_a => [{mod_b, #{}, hard}],
mod_b => [{mod_c, #{}, hard}],
mod_c => [{mod_a, #{}, hard}]}),
?assertError(_, add_deps(#{mod_a => #{}})).

succeeds_on_cycle_with_soft_dep_in_path(_Config) ->
set_deps(#{mod_a => [{mod_b, hard}],
mod_b => [{mod_c, soft}],
mod_c => [{mod_a, hard}]}),
?assertMatch([{mod_b, _}, {mod_a, _}, {mod_c, _}], add_deps(#{mod_a => []})).
set_deps(#{mod_a => [{mod_b, #{}, hard}],
mod_b => [{mod_c, #{}, soft}],
mod_c => [{mod_a, #{}, hard}]}),
?assertMatch([{mod_b, _}, {mod_a, _}, {mod_c, _}], add_deps(#{mod_a => #{}})).

succeeds_on_adding_soft_dependency_cycle_edge(_Config) ->
set_deps(#{mod_a => [{mod_b, hard}], mod_b => [{mod_a, soft}]}),
?assertMatch([{mod_b, _}, {mod_a, _}], add_deps(#{mod_a => []})).
set_deps(#{mod_a => [{mod_b, #{}, hard}], mod_b => [{mod_a, #{}, soft}]}),
?assertMatch([{mod_b, _}, {mod_a, _}], add_deps(#{mod_a => #{}})).

forces_dependency_args(_Config) ->
set_deps(#{mod_a => [{mod_b, [arg1, arg2], hard}]}),
?assertEqual([{mod_b, [arg1, arg2]}, {mod_a, []}], add_deps(#{mod_a => []})).
forces_dependency_opts(_Config) ->
set_deps(#{mod_a => [{mod_b, #{opt1 => 1, opt2 => 2}, hard}]}),
?assertEqual([{mod_b, #{opt1 => 1, opt2 => 2}}, {mod_a, #{}}], add_deps(#{mod_a => #{}})).

appends_dependencies_args(_Config) ->
set_deps(#{mod_a => [{mod_b, hard}, {mod_c, [arg1], hard}],
mod_b => [{mod_c, [arg2], hard}]}),
?assertEqual([{mod_c, [arg1, arg2]}, {mod_b, []}, {mod_a, []}], add_deps(#{mod_a => []})).
appends_dependencies_opts(_Config) ->
set_deps(#{mod_a => [{mod_b, #{}, hard}, {mod_c, #{opt1 => 1}, hard}],
mod_b => [{mod_c, #{opt2 => 2}, hard}]}),
?assertEqual([{mod_c, #{opt1 => 1, opt2 => 2}}, {mod_b, #{}}, {mod_a, #{}}],
add_deps(#{mod_a => #{}})).

optional_dependencies_are_not_started_by_themselves(_Config) ->
set_deps(#{mod_a => [{mod_b, optional}]}),
?assertMatch([{mod_a, _}], add_deps(#{mod_a => []})).

optional_dependencies_add_arguments(_Config) ->
set_deps(#{mod_a => [{mod_b, [arg1], optional}], mod_c => [{mod_b, hard}]}),
?assertMatch([{mod_b, [arg1]}, {mod_a, _}, {mod_c, _}], add_deps(#{mod_a => [], mod_c => []})).

overrides_dependency_args(_Config) ->
set_deps(#{mod_a => [{mod_b, hard}, {mod_c, [{arg, a}], hard}],
mod_b => [{mod_c, [{arg, b}], hard}]}),
?assertMatch([{mod_c, [{arg, a}]}, {mod_b, _}, {mod_a, _}], add_deps(#{mod_a => []})).

merges_dependency_args(_Config) ->
set_deps(#{mod_a => [{mod_b, hard}, {mod_c, [{arg, a}], hard}],
mod_b => [{mod_c, [{arg, a}, arg2], hard}]}),
?assertMatch([{mod_c, [{arg, a}, arg2]}, {mod_b, _}, {mod_a, _}], add_deps(#{mod_a => []})).
set_deps(#{mod_a => [{mod_b, #{}, optional}]}),
?assertMatch([{mod_a, _}], add_deps(#{mod_a => #{}})).

optional_dependencies_add_opts(_Config) ->
set_deps(#{mod_a => [{mod_b, #{opt1 => 1}, optional}], mod_c => [{mod_b, #{}, hard}]}),
?assertMatch([{mod_b, #{opt1 := 1}}, {mod_a, _}, {mod_c, _}],
add_deps(#{mod_a => #{}, mod_c => #{}})).

overrides_dependency_opts(_Config) ->
set_deps(#{mod_a => [{mod_b, #{}, hard}, {mod_c, #{opt1 => 2}, hard}],
mod_b => [{mod_c, #{opt1 => 1}, hard}]}),
?assertMatch([{mod_c, #{opt1 := 2}}, {mod_b, _}, {mod_a, _}], add_deps(#{mod_a => #{}})).

merges_dependency_opts(_Config) ->
set_deps(#{mod_a => [{mod_b, #{}, hard}, {mod_c, #{opt1 => 1}, hard}],
mod_b => [{mod_c, #{opt1 => 1, opt2 => 2}, hard}]}),
?assertMatch([{mod_c, #{opt1 := 1, opt2 := 2}}, {mod_b, _}, {mod_a, _}],
add_deps(#{mod_a => #{}})).

%% Helpers

Expand Down
60 changes: 30 additions & 30 deletions test/mongoose_modules_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end_per_testcase(_, _C) ->
meck:unload(?MODS).

starts_and_stops_modules(_Config) ->
set_modules(Modules = #{mod_a => [], mod_b => [{opt, val}]}),
set_modules(Modules = #{mod_a => #{}, mod_b => [{opt, val}]}),
ok = mongoose_modules:start(),
check_started(maps:to_list(Modules)),

Expand All @@ -47,17 +47,17 @@ starts_and_stops_modules(_Config) ->

ensures_module(_Config) ->
set_modules(#{}),
?assertEqual({started, start_result}, mongoose_modules:ensure_started(?HOST, mod_a, [])),
?assertEqual(#{mod_a => []}, get_modules()),
?assertEqual({started, start_result}, mongoose_modules:ensure_started(?HOST, mod_a, #{})),
?assertEqual(#{mod_a => #{}}, get_modules()),

?assertEqual(already_started, mongoose_modules:ensure_started(?HOST, mod_a, [])),
?assertEqual(#{mod_a => []}, get_modules()),
?assertEqual(already_started, mongoose_modules:ensure_started(?HOST, mod_a, #{})),
?assertEqual(#{mod_a => #{}}, get_modules()),

?assertEqual({restarted, [], start_result},
mongoose_modules:ensure_started(?HOST, mod_a, [{opt, val}])),
?assertEqual(#{mod_a => [{opt, val}]}, get_modules()),
?assertEqual({restarted, #{}, start_result},
mongoose_modules:ensure_started(?HOST, mod_a, #{opt => val})),
?assertEqual(#{mod_a => #{opt => val}}, get_modules()),

?assertEqual({stopped, [{opt, val}]}, mongoose_modules:ensure_stopped(?HOST, mod_a)),
?assertEqual({stopped, #{opt => val}}, mongoose_modules:ensure_stopped(?HOST, mod_a)),
?assertEqual(#{}, get_modules()),

?assertEqual(already_stopped, mongoose_modules:ensure_stopped(?HOST, mod_a)),
Expand All @@ -66,73 +66,73 @@ ensures_module(_Config) ->
reverts_config_when_module_fails_to_start(_Config) ->
set_modules(#{}),
meck:expect(gen_mod, start_module, fun(_, _, _) -> error(something_awful) end),
?assertError(something_awful, mongoose_modules:ensure_started(?HOST, mod_a, [])),
?assertError(something_awful, mongoose_modules:ensure_started(?HOST, mod_a, #{})),
?assertEqual(#{}, get_modules()).

does_not_change_config_when_module_fails_to_stop(_Config) ->
set_modules(#{mod_a => []}),
set_modules(#{mod_a => #{}}),
meck:expect(gen_mod, stop_module, fun(_, _) -> error(something_awful) end),
?assertError(something_awful, mongoose_modules:ensure_stopped(?HOST, mod_a)),
?assertEqual(#{mod_a => []}, get_modules()).
?assertEqual(#{mod_a => #{}}, get_modules()).

replaces_modules(_Config) ->
set_modules(Modules = #{mod_a => [], mod_b => [{opt, val}], mod_c => []}),
set_modules(Modules = #{mod_a => #{}, mod_b => #{opt => val}, mod_c => #{}}),
ok = mongoose_modules:start(),
check_started(maps:to_list(Modules)),

%% Stop mod_a, change opts for mod_b, do not change mod_c, start mod_d
NewModules = #{mod_b => [{new_opt, new_val}], mod_c => [], mod_d => []},
NewModules = #{mod_b => #{new_opt => new_val}, mod_c => #{}, mod_d => #{}},
ok = mongoose_modules:replace_modules(?HOST, [mod_a], NewModules),
check_stopped([mod_a, mod_b]),
check_not_stopped([mod_c]),
check_started([{mod_b, [{new_opt, new_val}]}, {mod_d, []}]),
check_started([{mod_b, #{new_opt => new_val}}, {mod_d, #{}}]),
?assertEqual(NewModules, get_modules()),

ok = mongoose_modules:stop(),
check_stopped([mod_b, mod_c, mod_d]).

replaces_modules_with_new_deps(_Config) ->
set_deps(#{mod_b => [{mod_c, hard}]}),
set_modules(Modules = #{mod_a => []}),
set_deps(#{mod_b => [{mod_c, #{}, hard}]}),
set_modules(Modules = #{mod_a => #{}}),
ok = mongoose_modules:start(),
check_started(maps:to_list(Modules)),

%% Start mod_b, which depends on mod_c
ok = mongoose_modules:replace_modules(?HOST, [], #{mod_b => []}),
ok = mongoose_modules:replace_modules(?HOST, [], #{mod_b => #{}}),
check_not_stopped([mod_a]),
check_started([{mod_b, []}, {mod_c, []}]),
?assertEqual(Modules#{mod_b => [], mod_c => []}, get_modules()),
check_started([{mod_b, #{}}, {mod_c, #{}}]),
?assertEqual(Modules#{mod_b => #{}, mod_c => #{}}, get_modules()),

ok = mongoose_modules:stop(),
check_stopped([mod_a, mod_b, mod_c]).

replaces_modules_with_old_deps(_Config) ->
set_deps(#{mod_a => [{mod_c, hard}]}),
set_modules(Modules = #{mod_a => [], mod_c => []}),
set_deps(#{mod_a => [{mod_c, #{}, hard}]}),
set_modules(Modules = #{mod_a => #{}, mod_c => #{}}),
ok = mongoose_modules:start(),
check_started(maps:to_list(Modules)),

%% Stop mod_a, which depends on mod_c, and start mod_b
ok = mongoose_modules:replace_modules(?HOST, [mod_a], #{mod_b => []}),
ok = mongoose_modules:replace_modules(?HOST, [mod_a], #{mod_b => #{}}),
check_stopped([mod_a, mod_c]),
check_started([{mod_b, []}]),
?assertEqual(#{mod_b => []}, get_modules()),
check_started([{mod_b, #{}}]),
?assertEqual(#{mod_b => #{}}, get_modules()),

ok = mongoose_modules:stop(),
check_stopped([mod_b]).

replaces_modules_with_same_deps(_Config) ->
set_deps(#{mod_a => [{mod_c, hard}], mod_b => [{mod_c, hard}]}),
set_modules(Modules = #{mod_a => [], mod_c => []}),
set_deps(#{mod_a => [{mod_c, #{}, hard}], mod_b => [{mod_c, #{}, hard}]}),
set_modules(Modules = #{mod_a => #{}, mod_c => #{}}),
ok = mongoose_modules:start(),
check_started(maps:to_list(Modules)),

%% Stop mod_a, and start mod_b, both depending on mod_c
ok = mongoose_modules:replace_modules(?HOST, [mod_a], #{mod_b => []}),
ok = mongoose_modules:replace_modules(?HOST, [mod_a], #{mod_b => #{}}),
check_stopped([mod_a]),
check_not_stopped([mod_c]),
check_started([{mod_b, []}]),
?assertEqual(#{mod_b => [], mod_c => []}, get_modules()),
check_started([{mod_b, #{}}]),
?assertEqual(#{mod_b => #{}, mod_c => #{}}, get_modules()),

ok = mongoose_modules:stop(),
check_stopped([mod_b]).
Expand Down

0 comments on commit 2ba28b5

Please sign in to comment.