-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
wpool_process_callbacks_SUITE.erl
182 lines (152 loc) · 6.8 KB
/
wpool_process_callbacks_SUITE.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
-module(wpool_process_callbacks_SUITE).
-behaviour(ct_suite).
-type config() :: [{atom(), term()}].
-export_type([config/0]).
-export([all/0]).
-export([init_per_suite/1, end_per_suite/1]).
-export([complete_callback_passed_when_starting_pool/1,
partial_callback_passed_when_starting_pool/1,
callback_can_be_added_and_removed_after_pool_is_started/1,
crashing_callback_does_not_affect_others/1, non_existsing_module_does_not_affect_others/1,
complete_coverage/1]).
-dialyzer({no_underspecs, all/0}).
-spec all() -> [atom()].
all() ->
[complete_callback_passed_when_starting_pool,
partial_callback_passed_when_starting_pool,
callback_can_be_added_and_removed_after_pool_is_started,
crashing_callback_does_not_affect_others,
non_existsing_module_does_not_affect_others].
-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
ok = wpool:start(),
Config.
-spec end_per_suite(config()) -> config().
end_per_suite(Config) ->
wpool:stop(),
Config.
-spec complete_callback_passed_when_starting_pool(config()) -> ok.
complete_callback_passed_when_starting_pool(_Config) ->
Pool = callbacks_test,
WorkersCount = 13,
meck:new(callbacks, [non_strict]),
meck:expect(callbacks, handle_init_start, fun(_AWorkerName) -> ok end),
meck:expect(callbacks, handle_worker_creation, fun(_AWorkerName) -> ok end),
meck:expect(callbacks, handle_worker_death, fun(_AWName, _Reason) -> ok end),
{ok, _Pid} =
wpool:start_pool(Pool,
[{workers, WorkersCount},
{enable_callbacks, true},
{worker, {crashy_server, []}},
{callbacks, [callbacks]}]),
WorkersCount =
ktn_task:wait_for(function_calls(callbacks, handle_init_start, ['_']), WorkersCount),
WorkersCount =
ktn_task:wait_for(function_calls(callbacks, handle_worker_creation, ['_']), WorkersCount),
Worker = wpool_pool:random_worker(Pool),
Worker ! crash,
1 = ktn_task:wait_for(function_calls(callbacks, handle_worker_death, ['_', '_']), 1),
wpool:stop_pool(Pool),
meck:unload(callbacks),
ok.
-spec partial_callback_passed_when_starting_pool(config) -> ok.
partial_callback_passed_when_starting_pool(_Config) ->
Pool = partial_callbacks_test,
WorkersCount = 7,
meck:new(callbacks, [non_strict]),
meck:expect(callbacks, handle_worker_creation, fun(_AWorkerName) -> ok end),
meck:expect(callbacks, handle_worker_death, fun(_AWName, _Reason) -> ok end),
{ok, _Pid} =
wpool:start_pool(Pool,
[{workers, WorkersCount},
{enable_callbacks, true},
{callbacks, [callbacks]}]),
WorkersCount =
ktn_task:wait_for(function_calls(callbacks, handle_worker_creation, ['_']), WorkersCount),
wpool:stop_pool(Pool),
meck:unload(callbacks),
ok.
-spec callback_can_be_added_and_removed_after_pool_is_started(config()) -> ok.
callback_can_be_added_and_removed_after_pool_is_started(_Config) ->
Pool = after_start_callbacks_test,
WorkersCount = 3,
meck:new(callbacks, [non_strict]),
meck:expect(callbacks, handle_worker_death, fun(_AWName, _Reason) -> ok end),
meck:new(callbacks2, [non_strict]),
meck:expect(callbacks2, handle_worker_death, fun(_AWName, _Reason) -> ok end),
{ok, _Pid} =
wpool:start_pool(Pool,
[{workers, WorkersCount},
{worker, {crashy_server, []}},
{enable_callbacks, true}]),
%% Now we are adding 2 callback modules
_ = wpool_pool:add_callback_module(Pool, callbacks),
_ = wpool_pool:add_callback_module(Pool, callbacks2),
Worker = wpool_pool:random_worker(Pool),
Worker ! crash,
%% they both are called
1 = ktn_task:wait_for(function_calls(callbacks, handle_worker_death, ['_', '_']), 1),
1 = ktn_task:wait_for(function_calls(callbacks2, handle_worker_death, ['_', '_']), 1),
%% then the first module is removed
_ = wpool_pool:remove_callback_module(Pool, callbacks),
Worker2 = wpool_pool:random_worker(Pool),
Worker2 ! crash,
%% and only the scond one is called
1 = ktn_task:wait_for(function_calls(callbacks, handle_worker_death, ['_', '_']), 1),
2 = ktn_task:wait_for(function_calls(callbacks2, handle_worker_death, ['_', '_']), 2),
wpool:stop_pool(Pool),
meck:unload(callbacks),
meck:unload(callbacks2),
ok.
-spec crashing_callback_does_not_affect_others(config()) -> ok.
crashing_callback_does_not_affect_others(_Config) ->
Pool = crashing_callbacks_test,
WorkersCount = 3,
meck:new(callbacks, [non_strict]),
meck:expect(callbacks, handle_worker_creation, fun(_AWorkerName) -> ok end),
meck:new(callbacks2, [non_strict]),
meck:expect(callbacks2,
handle_worker_creation,
fun(AWorkerName) -> {not_going_to_work} = AWorkerName end),
{ok, _Pid} =
wpool:start_pool(Pool,
[{workers, WorkersCount},
{worker, {crashy_server, []}},
{enable_callbacks, true},
{callbacks, [callbacks, callbacks2]}]),
WorkersCount =
ktn_task:wait_for(function_calls(callbacks, handle_worker_creation, ['_']), WorkersCount),
WorkersCount =
ktn_task:wait_for(function_calls(callbacks2, handle_worker_creation, ['_']),
WorkersCount),
wpool:stop_pool(Pool),
meck:unload(callbacks),
meck:unload(callbacks2),
ok.
-spec non_existsing_module_does_not_affect_others(config()) -> ok.
non_existsing_module_does_not_affect_others(_Config) ->
Pool = non_existing_callbacks_test,
WorkersCount = 4,
meck:new(callbacks, [non_strict]),
meck:expect(callbacks, handle_worker_creation, fun(_AWorkerName) -> ok end),
{ok, _Pid} =
wpool:start_pool(Pool,
[{workers, WorkersCount},
{worker, {crashy_server, []}},
{enable_callbacks, true},
{callbacks, [callbacks, non_existing_m]}]),
{error, nofile} = wpool_pool:add_callback_module(Pool, non_existing_m2),
WorkersCount =
ktn_task:wait_for(function_calls(callbacks, handle_worker_creation, ['_']), WorkersCount),
wpool:stop_pool(Pool),
meck:unload(callbacks),
ok.
function_calls(Module, Function, MeckMatchSpec) ->
fun() -> meck:num_calls(Module, Function, MeckMatchSpec) end.
-spec complete_coverage(config()) -> ok.
complete_coverage(_Config) ->
{ok, EventManager} = gen_event:start_link(),
gen_event:add_handler(EventManager, {wpool_process_callbacks, ?MODULE}, ?MODULE),
{error, {unexpected_call, call}} =
gen_event:call(EventManager, {wpool_process_callbacks, ?MODULE}, call),
ok.