diff --git a/apps/emqx/src/emqx_pool.erl b/apps/emqx/src/emqx_pool.erl index 8b9508768..c34ae61ae 100644 --- a/apps/emqx/src/emqx_pool.erl +++ b/apps/emqx/src/emqx_pool.erl @@ -32,7 +32,7 @@ ]). -ifdef(TEST). --export([worker/0]). +-export([worker/0, flush_async_tasks/0]). -endif. %% gen_server callbacks @@ -139,3 +139,15 @@ run({F, A}) when is_function(F), is_list(A) -> run(Fun) when is_function(Fun) -> Fun(). +-ifdef(TEST). +%% This help function creates a large enough number of async tasks +%% to force flush the pool workers. +%% The number of tasks should be large enough to ensure all workers have +%% the chance to work on at least one of the tasks. +flush_async_tasks() -> + Ref = make_ref(), + Self = self(), + L = lists:seq(1, 997), + lists:foreach(fun(I) -> emqx_pool:async_submit(fun() -> Self ! {done, Ref, I} end, []) end, L), + lists:foreach(fun(I) -> receive {done, Ref, I} -> ok end end, L). +-endif. diff --git a/apps/emqx_gateway/test/emqx_stomp_SUITE.erl b/apps/emqx_gateway/test/emqx_stomp_SUITE.erl index 8436b7312..13fced1c2 100644 --- a/apps/emqx_gateway/test/emqx_stomp_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_stomp_SUITE.erl @@ -403,6 +403,7 @@ t_rest_clienit_info(_) -> %% kickout {204, _} = request(delete, ClientPath), + ok = emqx_pool:flush_async_tasks(), {200, Clients2} = request(get, "/gateway/stomp/clients"), ?assertEqual(0, length(maps:get(data, Clients2))) end).