Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding offline_stub_SUITE #3142

Merged
merged 6 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions big_tests/default.spec
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
{suites, "tests", muc_light_http_api_SUITE}.
{suites, "tests", muc_light_legacy_SUITE}.
{suites, "tests", oauth_SUITE}.
{suites, "tests", offline_stub_SUITE}.
{suites, "tests", offline_SUITE}.
{suites, "tests", pep_SUITE}.
{suites, "tests", persistent_cluster_id_SUITE}.
Expand Down
2 changes: 1 addition & 1 deletion big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{skip_cases, "tests", carboncopy_SUITE, [discovering_support],
"at the moment mod_disco doesn't support dynamic domains"}.

{suites, "tests", domain_isolation_SUITE}.
{suites, "tests", offline_stub_SUITE}.

{suites, "tests", inbox_SUITE}.
{skip_cases, "tests", inbox_SUITE, [disco_service],
Expand Down
8 changes: 7 additions & 1 deletion big_tests/tests/dynamic_modules.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
-include_lib("common_test/include/ct.hrl").

-export([save_modules_for_host_types/2]).
-export([save_modules/2, ensure_modules/2, restore_modules/2, restore_modules/1]).
-export([save_modules/2, ensure_modules/2, ensure_stopped/2,
restore_modules/2, restore_modules/1]).
-export([stop/2, stop/3, start/3, start/4, restart/3, stop_running/2, start_running/1]).

-import(distributed_helper, [mim/0,
Expand All @@ -23,6 +24,11 @@ ensure_modules(HostType, RequiredModules) ->
{ToReplace, ReplaceWith} = to_replace(RequiredModules, CurrentModules, [], []),
ok = rpc(mim(), gen_mod_deps, replace_modules, [HostType, ToReplace, ReplaceWith]).

ensure_stopped(HostType, ModulesToStop) ->
CurrentModules = get_current_modules(HostType),
[stop(HostType, Mod) || {Mod, _Opts} <- CurrentModules,
lists:member(Mod, ModulesToStop)].

to_replace([], _CurrentModules, ReplaceAcc, ReplaceWithAcc) ->
{lists:usort(ReplaceAcc), ReplaceWithAcc};
to_replace([RequiredModule | Rest], CurrentModules, ReplaceAcc, ReplaceWithAcc) ->
Expand Down
69 changes: 69 additions & 0 deletions big_tests/tests/offline_stub_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
-module(offline_stub_SUITE).
-compile(export_all).

%%%===================================================================
%%% Suite configuration
%%%===================================================================

all() ->
[with_mod_offline_stub,
without_mod_offline_stub].

suite() ->
escalus:suite().

%%%===================================================================
%%% Init & teardown
%%%===================================================================

init_per_suite(Config) ->
escalus:init_per_suite(Config).

end_per_suite(Config) ->
escalus_fresh:clean(),
escalus:end_per_suite(Config).

init_per_testcase(Name, Config0) ->
HostType = domain_helper:host_type(),
Config1 = dynamic_modules:save_modules(HostType, Config0),
dynamic_modules:ensure_stopped(HostType, [mod_offline]),
case Name of
with_mod_offline_stub ->
dynamic_modules:ensure_modules(HostType, [{mod_offline_stub, []}]);
_ -> ok
end,
escalus:init_per_testcase(Name, Config1).

end_per_testcase(Name, Config) ->
HostType = domain_helper:host_type(),
dynamic_modules:restore_modules(HostType, Config),
escalus:end_per_testcase(Name, Config).

%%%===================================================================
%%% offline tests
%%%===================================================================

with_mod_offline_stub(Config) ->
Story =
fun(FreshConfig, Alice, Bob) ->
logout(FreshConfig, Bob),
escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"msgtxt">>)),
[] = escalus:wait_for_stanzas(Alice, 1, 1000)
end,
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}], Story).

without_mod_offline_stub(Config) ->
Story =
fun(FreshConfig, Alice, Bob) ->
logout(FreshConfig, Bob),
escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"msgtxt">>)),
Err = escalus:wait_for_stanza(Alice),
escalus:assert(is_error, [<<"cancel">>, <<"service-unavailable">>], Err)
end,
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}], Story).

%%%===================================================================
%%% Helpers
%%%===================================================================
logout(Config, User) ->
mongoose_helper:logout_user(Config, User).
13 changes: 7 additions & 6 deletions src/ejabberd_sm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -482,20 +482,21 @@ init([]) ->
ets:new(sm_iqtable, [named_table]),

ejabberd_hooks:add(node_cleanup, global, ?MODULE, node_cleanup, 50),
lists:foreach(fun(Host) -> ejabberd_hooks:add(hooks(Host)) end, ?MYHOSTS),
lists:foreach(fun(HostType) -> ejabberd_hooks:add(hooks(HostType)) end,
?ALL_HOST_TYPES),

ejabberd_commands:register_commands(commands()),

ejabberd_gen_sm:start(sm_backend(), Opts),

{ok, #state{}}.

hooks(Host) ->
hooks(HostType) ->
[
{roster_in_subscription, Host, ejabberd_sm, check_in_subscription, 20},
{offline_message_hook, Host, ejabberd_sm, bounce_offline_message, 100},
{offline_groupchat_message_hook, Host, ejabberd_sm, bounce_offline_message, 100},
{remove_user, Host, ejabberd_sm, disconnect_removed_user, 100}
{roster_in_subscription, HostType, ejabberd_sm, check_in_subscription, 20},
{offline_message_hook, HostType, ejabberd_sm, bounce_offline_message, 100},
{offline_groupchat_message_hook, HostType, ejabberd_sm, bounce_offline_message, 100},
{remove_user, HostType, ejabberd_sm, disconnect_removed_user, 100}
].

%%--------------------------------------------------------------------
Expand Down
13 changes: 8 additions & 5 deletions src/offline/mod_offline_stub.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,26 @@

%% gen_mod callbacks
-export([start/2,
stop/1]).
stop/1,
supported_features/0]).

%% Hook handlers
-export([stop_hook_processing/4]).

-spec start(any(), any()) -> 'ok'.
start(Host, _Opts) ->
[ejabberd_hooks:add(Hook, Host, M, F, Prio)
start(HostType, _Opts) ->
[ejabberd_hooks:add(Hook, HostType, M, F, Prio)
|| {Hook, M, F, Prio} <- handlers()],
ok.

-spec stop(any()) -> 'ok'.
stop(Host) ->
[ejabberd_hooks:delete(Hook, Host, M, F, Prio)
stop(HostType) ->
[ejabberd_hooks:delete(Hook, HostType, M, F, Prio)
|| {Hook, M, F, Prio} <- handlers()],
ok.

supported_features() -> [dynamic_domains].

%% Don't repeat yourself.
handlers() ->
[{offline_message_hook, ?MODULE, stop_hook_processing, 75}].
Expand Down
9 changes: 6 additions & 3 deletions test/ejabberd_sm_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,12 @@ terminate_sm() ->
gen_server:stop(ejabberd_sm).

set_meck(SMBackend) ->
meck:expect(ejabberd_config, get_global_option,
fun(sm_backend) -> SMBackend;
(hosts) -> [<<"localhost">>] end),
meck:expect(ejabberd_config, get_global_option, fun(sm_backend) -> SMBackend end),
meck:expect(ejabberd_config, get_global_option_or_default,
fun
(hosts, _Default) -> [<<"localhost">>];
(host_types, Default) -> Default
end),
meck:expect(ejabberd_config, get_local_option, fun(_) -> undefined end),
meck:expect(ejabberd_hooks, add, fun(_) -> ok end),
meck:expect(ejabberd_hooks, add, fun(_, _, _, _, _) -> ok end),
Expand Down