-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
wpool_SUITE.erl
590 lines (494 loc) · 20.9 KB
/
wpool_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
% This file is licensed to you under the Apache License,
% Version 2.0 (the "License"); you may not use this file
% except in compliance with the License. You may obtain
% a copy of the License at
%
% https://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing,
% software distributed under the License is distributed on an
% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
% KIND, either express or implied. See the License for the
% specific language governing permissions and limitations
% under the License.
%% @hidden
-module(wpool_SUITE).
-behaviour(ct_suite).
-elvis([{elvis_style, atom_naming_convention, disable}]).
-type config() :: [{atom(), term()}].
-export_type([config/0]).
-export([all/0]).
-export([init_per_suite/1, end_per_suite/1]).
-export([stats/1, stop_pool/1, non_brutal_shutdown/1, brutal_worker_shutdown/1, overrun/1,
kill_on_overrun/1, too_much_overrun/1, default_strategy/1, overrun_handler1/1,
overrun_handler2/1, default_options/1, complete_coverage/1, child_spec/1, broadcall/1,
broadcast/1, send_request/1, worker_killed_stats/1, accepts_maps_and_lists_as_opts/1,
pool_of_supervisors/1]).
-elvis([{elvis_style, no_block_expressions, disable}]).
-dialyzer({no_underspecs, all/0}).
-spec all() -> [atom()].
all() ->
[too_much_overrun,
overrun,
stop_pool,
non_brutal_shutdown,
brutal_worker_shutdown,
stats,
default_strategy,
default_options,
complete_coverage,
child_spec,
broadcast,
broadcall,
send_request,
kill_on_overrun,
worker_killed_stats,
accepts_maps_and_lists_as_opts,
pool_of_supervisors].
-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 overrun_handler1(M) -> {overrun1, M}.
overrun_handler1(M) ->
overrun_handler ! {overrun1, M}.
-spec overrun_handler2(M) -> {overrun2, M}.
overrun_handler2(M) ->
overrun_handler ! {overrun2, M}.
-spec too_much_overrun(config()) -> {comment, []}.
too_much_overrun(_Config) ->
ct:comment("Receiving overruns here..."),
true = register(overrun_handler, self()),
{ok, PoolPid} =
wpool:start_sup_pool(wpool_SUITE_too_much_overrun,
[{workers, 1},
{overrun_warning, 999},
{overrun_handler, {?MODULE, overrun_handler1}}]),
%% Sadly, the function that autogenerates this name is private.
CheckerName = 'wpool_pool-wpool_SUITE_too_much_overrun-time-checker',
ok = wpool_time_checker:add_handler(CheckerName, {?MODULE, overrun_handler2}),
ct:comment("Find the worker and the time checker..."),
{ok, Worker} = wpool:call(wpool_SUITE_too_much_overrun, {erlang, self, []}),
TCPid = get_time_checker(PoolPid),
ct:comment("Start a long running task..."),
ok = wpool:cast(wpool_SUITE_too_much_overrun, {timer, sleep, [5000]}),
TaskId =
ktn_task:wait_for_success(fun() ->
{dictionary, Dict} = erlang:process_info(Worker, dictionary),
{TId, _, _} = proplists:get_value(wpool_task, Dict),
TId
end),
ct:comment("Simulate overrun warning..."),
% huge runtime => no more overruns
TCPid ! {check, Worker, TaskId, 9999999999, infinity},
ct:comment("Get overrun message..."),
_ = receive
{overrun1, Message1} ->
overrun = proplists:get_value(alert, Message1),
wpool_SUITE_too_much_overrun = proplists:get_value(pool, Message1),
Worker = proplists:get_value(worker, Message1),
{cast, {timer, sleep, [5000]}} = proplists:get_value(task, Message1),
9999999999 = proplists:get_value(runtime, Message1)
after 100 ->
ct:fail(no_overrun)
end,
ct:comment("Get overrun message..."),
_ = receive
{overrun2, Message2} ->
overrun = proplists:get_value(alert, Message2),
wpool_SUITE_too_much_overrun = proplists:get_value(pool, Message2),
Worker = proplists:get_value(worker, Message2),
{cast, {timer, sleep, [5000]}} = proplists:get_value(task, Message2),
9999999999 = proplists:get_value(runtime, Message2)
after 100 ->
ct:fail(no_overrun)
end,
ct:comment("No more overruns..."),
_ = case get_messages(100) of
[] ->
ok;
Msgs1 ->
ct:fail({unexpected_messages, Msgs1})
end,
ct:comment("Kill the worker..."),
exit(Worker, kill),
ct:comment("Simulate overrun warning..."),
TCPid ! {check, Worker, TaskId, 100}, % tiny runtime, to check
ct:comment("Nothing happens..."),
ok = no_messages(),
ct:comment("Stop pool..."),
ok = wpool:stop_sup_pool(wpool_SUITE_too_much_overrun),
{comment, []}.
-spec overrun(config()) -> {comment, []}.
overrun(_Config) ->
true = register(overrun_handler, self()),
{ok, _Pid} =
wpool:start_sup_pool(wpool_SUITE_overrun_pool,
[{workers, 1},
{overrun_warning, 1000},
{overrun_handler, {?MODULE, overrun_handler1}}]),
ok = wpool:cast(wpool_SUITE_overrun_pool, {timer, sleep, [1500]}),
_ = receive
{overrun1, Message} ->
overrun = proplists:get_value(alert, Message),
wpool_SUITE_overrun_pool = proplists:get_value(pool, Message),
WPid = proplists:get_value(worker, Message),
true = is_pid(WPid),
{cast, {timer, sleep, [1500]}} = proplists:get_value(task, Message),
Runtime = proplists:get_value(runtime, Message),
true = Runtime >= 1000
after 1500 ->
ct:fail(no_overrun)
end,
ok = no_messages(),
ok = wpool:stop_sup_pool(wpool_SUITE_overrun_pool),
{comment, []}.
-spec kill_on_overrun(config()) -> {comment, []}.
kill_on_overrun(_Config) ->
true = register(overrun_handler, self()),
{ok, _Pid} =
wpool:start_sup_pool(wpool_SUITE_kill_on_overrun_pool,
[{workers, 1},
{overrun_warning, 500},
{max_overrun_warnings,
2}, %% The worker must be killed after 2 overrun
%% warnings, which is after 1 secs with this
%% configuration
{overrun_handler, {?MODULE, overrun_handler1}}]),
ok = wpool:cast(wpool_SUITE_kill_on_overrun_pool, {timer, sleep, [2000]}),
_ = receive
{overrun1, Message} ->
overrun = proplists:get_value(alert, Message),
wpool_SUITE_kill_on_overrun_pool = proplists:get_value(pool, Message),
WPid = proplists:get_value(worker, Message),
true = is_pid(WPid),
true = erlang:is_process_alive(WPid)
after 2000 ->
ct:fail(no_overrun)
end,
_ = receive
{overrun1, Message2} ->
max_overrun_limit = proplists:get_value(alert, Message2),
wpool_SUITE_kill_on_overrun_pool = proplists:get_value(pool, Message2),
WPid2 = proplists:get_value(worker, Message2),
true = is_pid(WPid2),
false = erlang:is_process_alive(WPid2)
after 2000 ->
ct:fail(no_overrun)
end,
ok = no_messages(),
ok = wpool:stop_sup_pool(wpool_SUITE_overrun_pool),
{comment, []}.
-spec stop_pool(config()) -> {comment, []}.
stop_pool(_Config) ->
{ok, PoolPid} = wpool:start_sup_pool(wpool_SUITE_stop_pool, [{workers, 1}]),
true = erlang:is_process_alive(PoolPid),
ok = wpool:stop_sup_pool(wpool_SUITE_stop_pool),
false = erlang:is_process_alive(PoolPid),
ok = wpool:stop_sup_pool(wpool_SUITE_stop_pool),
{comment, []}.
-spec non_brutal_shutdown(config()) -> {comment, []}.
non_brutal_shutdown(_Config) ->
{ok, PoolPid} =
wpool:start_sup_pool(wpool_SUITE_non_brutal_shutdown,
[{workers, 1}, {pool_sup_shutdown, 100}]),
true = erlang:is_process_alive(PoolPid),
Stats = wpool:stats(wpool_SUITE_non_brutal_shutdown),
{workers, [{WorkerId, _}]} = lists:keyfind(workers, 1, Stats),
Worker = wpool_pool:worker_name(wpool_SUITE_non_brutal_shutdown, WorkerId),
monitor(process, Worker),
ok = wpool:stop_sup_pool(wpool_SUITE_non_brutal_shutdown),
receive
{'DOWN', _, process, {Worker, _}, Reason} ->
shutdown = Reason
after 200 ->
ct:fail(worker_not_stopped)
end,
{comment, []}.
-spec brutal_worker_shutdown(config()) -> {comment, []}.
brutal_worker_shutdown(_Config) ->
{ok, PoolPid} =
wpool:start_sup_pool(wpool_SUITE_non_brutal_shutdown,
[{workers, 1},
{pool_sup_shutdown, 100},
{worker_shutdown, brutal_kill}]),
true = erlang:is_process_alive(PoolPid),
Stats = wpool:stats(wpool_SUITE_non_brutal_shutdown),
{workers, [{WorkerId, _}]} = lists:keyfind(workers, 1, Stats),
Worker = wpool_pool:worker_name(wpool_SUITE_non_brutal_shutdown, WorkerId),
monitor(process, Worker),
ok = wpool:stop_sup_pool(wpool_SUITE_non_brutal_shutdown),
receive
{'DOWN', _, process, {Worker, _}, Reason} ->
killed = Reason
after 200 ->
ct:fail(worker_not_stopped)
end,
{comment, []}.
-spec stats(config()) -> {comment, []}.
stats(_Config) ->
Get = fun proplists:get_value/2,
ok =
try
_ = wpool:stats(?MODULE),
ok
catch
_:no_workers ->
ok
end,
{ok, PoolPid} = wpool:start_sup_pool(wpool_SUITE_stats_pool, [{workers, 10}]),
true = is_process_alive(PoolPid),
PoolPid = erlang:whereis(wpool_SUITE_stats_pool),
% Checks ...
[InitStats] = wpool:stats(),
wpool_SUITE_stats_pool = Get(pool, InitStats),
PoolPid = Get(supervisor, InitStats),
Options = Get(options, InitStats),
infinity = Get(overrun_warning, Options),
{logger, warning} = Get(overrun_handler, Options),
10 = Get(workers, Options),
10 = Get(size, InitStats),
1 = Get(next_worker, InitStats),
InitWorkers = Get(workers, InitStats),
10 = length(InitWorkers),
_ = [begin
WorkerStats = Get(I, InitWorkers),
0 = Get(message_queue_len, WorkerStats),
[] = lists:keydelete(message_queue_len, 1, lists:keydelete(memory, 1, WorkerStats))
end
|| I <- lists:seq(1, 10)],
% Start a long task on every worker
Sleep = {timer, sleep, [2000]},
[wpool:cast(wpool_SUITE_stats_pool, Sleep, next_worker) || _ <- lists:seq(1, 10)],
ok =
ktn_task:wait_for_success(fun() ->
WorkingStats = wpool:stats(wpool_SUITE_stats_pool),
wpool_SUITE_stats_pool = Get(pool, WorkingStats),
PoolPid = Get(supervisor, WorkingStats),
Options = Get(options, WorkingStats),
10 = Get(size, WorkingStats),
1 = Get(next_worker, WorkingStats),
WorkingWorkers = Get(workers, WorkingStats),
10 = length(WorkingWorkers),
[begin
WorkerStats = Get(I, WorkingWorkers),
0 = Get(message_queue_len, WorkerStats),
{timer, sleep, 1} = Get(current_function, WorkerStats),
{timer, sleep, 1, _} = Get(current_location, WorkerStats),
{cast, Sleep} = Get(task, WorkerStats),
true = is_number(Get(runtime, WorkerStats))
end
|| I <- lists:seq(1, 10)],
ok
end),
wpool:stop_sup_pool(wpool_SUITE_stats_pool),
no_workers =
ktn_task:wait_for(fun() ->
try
wpool:stats(wpool_SUITE_stats_pool)
catch
_:E ->
E
end
end,
no_workers,
100,
50),
{comment, []}.
-spec default_strategy(config()) -> {comment, []}.
default_strategy(_Config) ->
application:unset_env(worker_pool, default_strategy),
available_worker = wpool:default_strategy(),
application:set_env(worker_pool, default_strategy, best_worker),
best_worker = wpool:default_strategy(),
application:unset_env(worker_pool, default_strategy),
available_worker = wpool:default_strategy(),
{comment, []}.
-spec default_options(config()) -> {comment, []}.
default_options(_Config) ->
ct:comment("Starts a pool with default options"),
{ok, PoolPid} = wpool:start_pool(default_pool),
true = is_pid(PoolPid),
ct:comment("Starts a supervised pool with default options"),
{ok, SupPoolPid} = wpool:start_sup_pool(default_sup_pool),
true = is_pid(SupPoolPid),
{comment, []}.
-spec complete_coverage(config()) -> {comment, []}.
complete_coverage(_Config) ->
ct:comment("Time checker"),
{ok, _} = wpool_time_checker:init({pool, [{x, y}]}),
{ok, PoolPid} = wpool:start_pool(coverage, []),
TCPid = get_time_checker(PoolPid),
TCPid ! info,
ok = gen_server:cast(TCPid, cast),
ct:comment("Queue Manager"),
QMPid = get_queue_manager(PoolPid),
QMPid ! info,
{ok, _} = wpool_queue_manager:start_link(pool, pool_queue_manager),
{ok, _} = wpool_queue_manager:init([{pool, pool}]),
{comment, []}.
-spec child_spec(config()) -> {comment, []}.
child_spec(_Config) ->
ct:comment("Verify child_spec is correct"),
ChildSpec = wpool:child_spec(child_spec, []),
ok = supervisor:check_childspecs([ChildSpec]),
{comment, []}.
-spec broadcall(config()) -> {comment, []}.
broadcall(_Config) ->
Pool = broadcall,
WorkersCount = 19,
{ok, _Pid} = wpool:start_pool(Pool, [{workers, WorkersCount}]),
ct:comment("Check mecked function is called ~p times.", [WorkersCount]),
meck:new(x, [non_strict]),
meck:expect(x, x, fun() -> ok end),
% Broadcall x:x() execution to workers.
{[_ | _], _} = wpool:broadcall(Pool, {x, x, []}, infinity),
% Give some time for the workers to perform the calls.
WorkersCount = ktn_task:wait_for(fun() -> meck:num_calls(x, x, '_') end, WorkersCount),
ct:comment("Check they all are \"working\""),
try
% Make all the workers sleep for 1.5 seconds
wpool:broadcall(Pool, {timer, sleep, [1500]}, 1000),
% check they all are actually busy (executing timer:sleep/1 function).
ct:fail("They finished before the timeout")
catch
_:{timeout, _} ->
ok
end,
ct:comment("Check that failures are delivered as errors"),
meck:expect(x, fail, fun() -> exit(self(), shutdown) end),
{_, [_ | _]} = wpool:broadcall(Pool, {x, fail, []}, infinity),
meck:unload(x),
{comment, []}.
-spec broadcast(config()) -> {comment, []}.
broadcast(_Config) ->
Pool = broadcast,
WorkersCount = 19,
{ok, _Pid} = wpool:start_pool(Pool, [{workers, WorkersCount}]),
ct:comment("Check mecked function is called ~p times.", [WorkersCount]),
meck:new(x, [non_strict]),
meck:expect(x, x, fun() -> ok end),
% Broadcast x:x() execution to workers.
wpool:broadcast(Pool, {x, x, []}),
% Give some time for the workers to perform the calls.
WorkersCount = ktn_task:wait_for(fun() -> meck:num_calls(x, x, '_') end, WorkersCount),
ct:comment("Check they all are \"working\""),
% Make all the workers sleep for 1.5 seconds
wpool:broadcast(Pool, {timer, sleep, [1500]}),
% check they all are actually busy (executing timer:sleep/1 function).
try
wpool:call(Pool, {io, format, ["I am awake.~n"]}, next_available_worker),
ct:fail("There was at least 1 worker available")
catch
_:no_available_workers ->
ok
end,
meck:unload(x),
{comment, []}.
-spec send_request(config()) -> {comment, []}.
send_request(_Config) ->
Pool = send_request,
{ok, _Pid} = wpool:start_pool(Pool, []),
ct:comment("Check that the request can be placed"),
send_request_to_worker(Pool),
ct:comment("Check the hash_worker strategy"),
send_request_to_worker(Pool, {hash_worker, key}),
ct:comment("Check the random_worker strategy"),
send_request_to_worker(Pool, random_worker),
{comment, []}.
-spec worker_killed_stats(config()) -> {comment, []}.
worker_killed_stats(_Config) ->
%% Each server will take 100ms to start, but the start_sup_pool/2 call is synchronous anyway
{ok, PoolPid} =
wpool:start_sup_pool(wpool_SUITE_worker_killed_stats,
[{workers, 3}, {worker, {sleepy_server, 500}}]),
true = erlang:is_process_alive(PoolPid),
Workers =
fun() -> lists:keyfind(workers, 1, wpool:stats(wpool_SUITE_worker_killed_stats)) end,
WorkerName = wpool_pool:worker_name(wpool_SUITE_worker_killed_stats, 1),
ct:comment("wpool:stats/1 should work normally"),
{workers, [_, _, _]} = Workers(),
ct:comment("wpool:stats/1 should work even if a process just dies and it's not yet back alive"),
exit(whereis(WorkerName), kill),
{workers, [_, _]} = Workers(),
ct:comment("Once the process is alive again, we should see it at the stats"),
true = ktn_task:wait_for(fun() -> is_pid(whereis(WorkerName)) end, true, 10, 75),
{workers, [_, _, _]} = Workers(),
{comment, []}.
-spec accepts_maps_and_lists_as_opts(config()) -> {comment, []}.
accepts_maps_and_lists_as_opts(_Config) ->
%% Each server will take 100ms to start, but the start_sup_pool/2 call is synchronous anyway
{ok, PoolPidList} =
wpool:start_sup_pool(accepts_maps_and_lists_as_opts_list,
[{workers, 3}, {worker, {sleepy_server, 500}}]),
true = erlang:is_process_alive(PoolPidList),
ct:comment("accepts lists as opts"),
{ok, PoolPidMap} =
wpool:start_sup_pool(accepts_maps_and_lists_as_opts_map,
#{workers => 3, worker => {sleepy_server, 500}}),
true = erlang:is_process_alive(PoolPidMap),
ct:comment("accepts lists as opts"),
{comment, []}.
-spec pool_of_supervisors(config()) -> {comment, string()}.
pool_of_supervisors(_Config) ->
Opts =
#{workers => 3,
worker_shutdown => infinity,
worker => {supervisor, {echo_supervisor, echo_supervisor, noargs}}},
{ok, Pid} = wpool:start_sup_pool(pool_of_supervisors, Opts),
true = erlang:is_process_alive(Pid),
Run = fun(Sup, _) -> supervisor:start_child(Sup, [{ok, #{}}]) end,
ForEach =
fun(_) ->
{ok, EchoServer} = wpool:run(pool_of_supervisors, Run, next_worker),
true = erlang:is_process_alive(EchoServer)
end,
lists:foreach(ForEach, lists:seq(1, 9)),
Supervisors = wpool:get_workers(pool_of_supervisors),
[3 = proplists:get_value(active, supervisor:count_children(Supervisor))
|| Supervisor <- Supervisors],
{comment, "Nicely load-balanced childrens across supervisors"}.
%% =============================================================================
%% Helpers
%% =============================================================================
get_time_checker(PoolPid) ->
[TCPid] =
[P || {_, P, worker, [wpool_time_checker]} <- supervisor:which_children(PoolPid)],
TCPid.
get_queue_manager(PoolPid) ->
[QMPid] =
[P || {_, P, worker, [wpool_queue_manager]} <- supervisor:which_children(PoolPid)],
QMPid.
get_messages(MaxTimeout) ->
get_messages(MaxTimeout, []).
get_messages(MaxTimeout, Acc) ->
receive
Any ->
get_messages(MaxTimeout, [Any | Acc])
after MaxTimeout ->
Acc
end.
no_messages() ->
case get_messages(1000) of
[] ->
ok;
Msgs2 ->
ct:fail({unexpected_messages, Msgs2})
end.
send_request_to_worker(Pool) ->
ReqId = wpool:send_request(Pool, {erlang, self, []}),
wait_response(ReqId).
send_request_to_worker(Pool, Strategy) ->
ReqId = wpool:send_request(Pool, {erlang, self, []}, Strategy),
wait_response(ReqId).
wait_response(ReqId) ->
case gen_server:wait_response(ReqId, 5000) of
{reply, {ok, _}} ->
ok;
timeout ->
ct:fail("no response")
end.