-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathwpool_pool_SUITE.erl
633 lines (507 loc) · 19.5 KB
/
wpool_pool_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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
% 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_pool_SUITE).
-behaviour(ct_suite).
-type config() :: [{atom(), term()}].
-export_type([config/0]).
-define(WORKERS, 6).
-export([all/0]).
-export([init_per_suite/1, end_per_suite/1, init_per_testcase/2, end_per_testcase/2]).
-export([stop_worker/1, best_worker/1, next_worker/1, random_worker/1, available_worker/1,
hash_worker/1, custom_worker/1, next_available_worker/1, wpool_record/1,
queue_type_fifo/1, queue_type_lifo/1, get_workers/1]).
-export([manager_crash/1, super_fast/1, mess_up_with_store/1]).
-elvis([{elvis_style, no_block_expressions, disable}]).
-elvis([{elvis_style, no_catch_expressions, disable}]).
-spec all() -> [atom()].
all() ->
[Fun
|| {Fun, 1} <- module_info(exports),
not lists:member(Fun, [init_per_suite, end_per_suite, module_info])].
-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 init_per_testcase(atom(), config()) -> config().
init_per_testcase(queue_type_lifo = TestCase, Config) ->
{ok, _} = wpool:start_pool(TestCase, [{workers, 1}, {queue_type, lifo}]),
Config;
init_per_testcase(queue_type_fifo = TestCase, Config) ->
{ok, _} = wpool:start_pool(TestCase, [{workers, 1}, {queue_type, fifo}]),
Config;
init_per_testcase(TestCase, Config) ->
{ok, _} = wpool:start_pool(TestCase, [{workers, ?WORKERS}]),
Config.
-spec end_per_testcase(atom(), config()) -> config().
end_per_testcase(TestCase, Config) ->
catch wpool:stop_sup_pool(TestCase),
Config.
-spec stop_worker(config()) -> {comment, []}.
stop_worker(_Config) ->
true = undefined /= wpool_pool:find_wpool(stop_worker),
true = wpool:stop_pool(stop_worker),
undefined = ktn_task:wait_for(fun() -> wpool_pool:find_wpool(stop_worker) end, undefined),
true = wpool:stop_pool(stop_worker),
undefined = wpool_pool:find_wpool(stop_worker),
{comment, ""}.
-spec available_worker(config()) -> {comment, []}.
available_worker(_Config) ->
Pool = available_worker,
Run = fun(Worker, Timeout) -> gen_server:call(Worker, {erlang, self, []}, Timeout) end,
try wpool:call(not_a_pool, x) of
no_result ->
no_result
catch
_:no_workers ->
ok
end,
try wpool:run(not_a_pool, Run) of
no_result ->
no_result
catch
_:no_workers ->
ok
end,
{ok, _} = wpool:run(Pool, Run, available_worker),
ct:log("Put them all to work, each request should go to a different worker"),
[wpool:cast(Pool, {timer, sleep, [5000]}) || _ <- lists:seq(1, ?WORKERS)],
[0] = ktn_task:wait_for(fun() -> worker_msg_queue_lengths(Pool) end, [0]),
ct:log("Now send another round of messages,
the workers queues should still be empty"),
[wpool:cast(Pool, {timer, sleep, [100 * I]}) || I <- lists:seq(1, ?WORKERS)],
% Check that we have ?WORKERS pending tasks
?WORKERS =
ktn_task:wait_for(fun() ->
Stats1 = wpool:stats(Pool),
[0] =
lists:usort([proplists:get_value(message_queue_len, WS)
|| {_, WS} <- proplists:get_value(workers, Stats1)]),
proplists:get_value(total_message_queue_len, Stats1)
end,
?WORKERS),
ct:log("If we can't wait we get no workers"),
try wpool:call(Pool, {erlang, self, []}, available_worker, 100) of
should_fail ->
should_fail
catch
_:timeout ->
timeout
end,
try wpool:run(Pool, Run, available_worker, 100) of
should_fail ->
should_fail
catch
_:timeout ->
timeout
end,
ct:log("Let's wait until all workers are free"),
wpool:call(Pool, {erlang, self, []}, available_worker, infinity),
% Check we have no pending tasks
Stats2 = wpool:stats(Pool),
0 = proplists:get_value(total_message_queue_len, Stats2),
ct:log("Now they all should be free"),
ct:log("We get half of them working for a while"),
[wpool:cast(Pool, {timer, sleep, [60000]}) || _ <- lists:seq(1, ?WORKERS, 2)],
% Check we have no pending tasks
0 =
ktn_task:wait_for(fun() -> proplists:get_value(total_message_queue_len, wpool:stats(Pool))
end,
0),
ct:log("We run tons of calls, and none is blocked,
because all of them are handled by different workers"),
Workers =
[wpool:call(Pool, {erlang, self, []}, available_worker, 5000)
|| _ <- lists:seq(1, 20 * ?WORKERS)],
UniqueWorkers =
sets:to_list(
sets:from_list(Workers)),
{?WORKERS, UniqueWorkers, true} =
{?WORKERS, UniqueWorkers, ?WORKERS / 2 >= length(UniqueWorkers)},
{comment, []}.
-spec best_worker(config()) -> {comment, []}.
best_worker(_Config) ->
Pool = best_worker,
try wpool:call(not_a_pool, x, best_worker) of
Result ->
no_result = Result
catch
_:no_workers ->
ok
end,
Run = fun(Worker, Timeout) -> gen_server:call(Worker, {erlang, self, []}, Timeout) end,
{ok, _} = wpool:run(Pool, Run, best_worker),
Req = wpool:send_request(Pool, {erlang, self, []}, best_worker),
{reply, {ok, _}} = gen_server:wait_response(Req, 5000),
%% Fill up their message queues...
[wpool:cast(Pool, {timer, sleep, [60000]}, next_worker) || _ <- lists:seq(1, ?WORKERS)],
[0] = ktn_task:wait_for(fun() -> worker_msg_queue_lengths(Pool) end, [0]),
[wpool:cast(Pool, {timer, sleep, [60000]}, best_worker) || _ <- lists:seq(1, ?WORKERS)],
[1] = ktn_task:wait_for(fun() -> worker_msg_queue_lengths(Pool) end, [1]),
%% Now try best worker once per worker
[wpool:cast(Pool, {timer, sleep, [60000]}, best_worker) || _ <- lists:seq(1, ?WORKERS)],
%% The load should be evenly distributed...
[2] = ktn_task:wait_for(fun() -> worker_msg_queue_lengths(Pool) end, [2]),
{comment, []}.
-spec next_available_worker(config()) -> {comment, []}.
next_available_worker(_Config) ->
Pool = next_available_worker,
ct:log("not_a_pool is not a pool"),
try wpool:call(not_a_pool, x, next_available_worker) of
Result ->
no_result = Result
catch
_:no_workers ->
ok
end,
Run = fun(Worker, Timeout) -> gen_server:call(Worker, {erlang, self, []}, Timeout) end,
{ok, _} = wpool:run(Pool, Run, next_available_worker),
ct:log("Put them all to work..."),
[wpool:cast(Pool, {timer, sleep, [1500 + I]}, next_available_worker)
|| I <- lists:seq(0, (?WORKERS - 1) * 60000, 60000)],
AvailableWorkers =
fun() ->
length([a_worker
|| {_, WS} <- proplists:get_value(workers, wpool:stats(Pool)),
proplists:get_value(task, WS) == undefined])
end,
ct:log("All busy..."),
0 = ktn_task:wait_for(AvailableWorkers, 0),
ct:log("No available workers..."),
try wpool:cast(Pool, {timer, sleep, [60000]}, next_available_worker) of
ok ->
ct:fail("Exception expected")
catch
_:no_available_workers ->
ok
end,
ct:log("Wait until the first frees up..."),
1 = ktn_task:wait_for(AvailableWorkers, 1),
Req = wpool:send_request(Pool, {erlang, self, []}, next_available_worker),
{reply, {ok, _}} = gen_server:wait_response(Req, 5000),
ok = wpool:cast(Pool, {timer, sleep, [60000]}, next_available_worker),
ct:log("No more available workers..."),
try wpool:cast(Pool, {timer, sleep, [60000]}, next_available_worker) of
ok ->
ct:fail("Exception expected")
catch
_:no_available_workers ->
ok
end,
{comment, []}.
-spec next_worker(config()) -> {comment, []}.
next_worker(_Config) ->
Pool = next_worker,
try wpool:call(not_a_pool, x, next_worker) of
Result ->
no_result = Result
catch
_:no_workers ->
ok
end,
Res0 =
[begin
Stats = wpool:stats(Pool),
I = proplists:get_value(next_worker, Stats),
wpool:call(Pool, {erlang, self, []}, next_worker, infinity)
end
|| I <- lists:seq(1, ?WORKERS)],
?WORKERS =
sets:size(
sets:from_list(Res0)),
Res0 =
[begin
Stats = wpool:stats(Pool),
I = proplists:get_value(next_worker, Stats),
wpool:call(Pool, {erlang, self, []}, next_worker)
end
|| I <- lists:seq(1, ?WORKERS)],
Req = wpool:send_request(Pool, {erlang, self, []}, next_worker),
{reply, {ok, _}} = gen_server:wait_response(Req, 5000),
Run = fun(Worker, Timeout) -> gen_server:call(Worker, {erlang, self, []}, Timeout) end,
{ok, _} = wpool:run(Pool, Run, next_worker),
{comment, []}.
-spec random_worker(config()) -> {comment, []}.
random_worker(_Config) ->
Pool = random_worker,
try wpool:call(not_a_pool, x, random_worker) of
Result ->
no_result = Result
catch
_:no_workers ->
ok
end,
Run = fun(Worker, Timeout) -> gen_server:call(Worker, {erlang, self, []}, Timeout) end,
{ok, _} = wpool:run(Pool, Run, random_worker),
%% Ask for a random worker's identity 20x more than the number of workers
%% and expect to get an answer from every worker at least once.
Serial =
[wpool:call(Pool, {erlang, self, []}, random_worker) || _ <- lists:seq(1, 20 * ?WORKERS)],
?WORKERS =
sets:size(
sets:from_list(Serial)),
%% Randomly ask a lot of workers to send ourselves the atom true
[wpool:cast(Pool, {erlang, send, [self(), true]}, random_worker)
|| _ <- lists:seq(1, 20 * ?WORKERS)],
Results =
[receive
true ->
true
end
|| _ <- lists:seq(1, 20 * ?WORKERS)],
true = lists:all(fun(Value) -> true =:= Value end, Results),
%% do a gen_server:send_request/3
Req = wpool:send_request(Pool, {erlang, self, []}, random_worker),
{reply, {ok, _}} = gen_server:wait_response(Req, 5000),
%% Now do the same with a freshly spawned process for each request to ensure
%% randomness isn't reset with each spawn of the process_dictionary
Self = self(),
_ = [spawn(fun() ->
WorkerId = wpool:call(Pool, {erlang, self, []}, random_worker),
Self ! {worker, WorkerId}
end)
|| _ <- lists:seq(1, 20 * ?WORKERS)],
Concurrent = collect_results(20 * ?WORKERS, []),
?WORKERS =
sets:size(
sets:from_list(Concurrent)),
{comment, []}.
-spec hash_worker(config()) -> {comment, []}.
hash_worker(_Config) ->
Pool = hash_worker,
try wpool:call(not_a_pool, x, {hash_worker, 1}) of
Result ->
no_result = Result
catch
_:no_workers ->
ok
end,
%% Use two hash keys that have different values (0, 1) to target only
%% two workers. Other workers should be missing.
Targeted =
[wpool:call(Pool, {erlang, self, []}, {hash_worker, I rem 2})
|| I <- lists:seq(1, 20 * ?WORKERS)],
2 =
sets:size(
sets:from_list(Targeted)),
%% Now use many different hash keys. All workers should be hit.
Spread =
[wpool:call(Pool, {erlang, self, []}, {hash_worker, I})
|| I <- lists:seq(1, 20 * ?WORKERS)],
?WORKERS =
sets:size(
sets:from_list(Spread)),
Run = fun(Worker, Timeout) -> gen_server:call(Worker, {erlang, self, []}, Timeout) end,
[{ok, _} = wpool:run(Pool, Run, {hash_worker, I}) || I <- lists:seq(1, 20 * ?WORKERS)],
%% Fill up their message queues...
[wpool:cast(Pool, {timer, sleep, [60000]}, {hash_worker, I})
|| I <- lists:seq(1, 20 * ?WORKERS)],
false =
ktn_task:wait_for(fun() -> lists:member(0, worker_msg_queue_lengths(Pool)) end, false),
{comment, []}.
-spec custom_worker(config()) -> {comment, []}.
custom_worker(_Config) ->
Pool = custom_worker,
Strategy = fun wpool_pool:next_worker/1,
try wpool:call(not_a_pool, x, Strategy) of
Result ->
no_result = Result
catch
_:no_workers ->
ok
end,
_ = [begin
Stats = wpool:stats(Pool),
I = proplists:get_value(next_worker, Stats),
wpool:cast(Pool, {io, format, ["ok!"]}, Strategy)
end
|| I <- lists:seq(1, ?WORKERS)],
Res0 =
[begin
Stats = wpool:stats(Pool),
I = proplists:get_value(next_worker, Stats),
wpool:call(Pool, {erlang, self, []}, Strategy, infinity)
end
|| I <- lists:seq(1, ?WORKERS)],
?WORKERS =
sets:size(
sets:from_list(Res0)),
Res0 =
[begin
Stats = wpool:stats(Pool),
I = proplists:get_value(next_worker, Stats),
wpool:call(Pool, {erlang, self, []}, Strategy)
end
|| I <- lists:seq(1, ?WORKERS)],
Req = wpool:send_request(Pool, {erlang, self, []}, Strategy),
{reply, {ok, _}} = gen_server:wait_response(Req, 5000),
Run = fun(Worker, Timeout) -> gen_server:call(Worker, {erlang, self, []}, Timeout) end,
{ok, _} = wpool:run(Pool, Run, Strategy),
{comment, []}.
-spec manager_crash(config()) -> {comment, []}.
manager_crash(_Config) ->
Pool = manager_crash,
QueueManager = 'wpool_pool-manager_crash-queue-manager',
ct:log("Check that the pool is working"),
{ok, ok} = send_io_format(Pool),
OldPid = whereis(QueueManager),
ct:log("Crash the pool manager"),
exit(whereis(QueueManager), kill),
false =
ktn_task:wait_for(fun() -> lists:member(whereis(QueueManager), [OldPid, undefined]) end,
false),
ct:log("Check that the pool is working again"),
{ok, ok} = send_io_format(Pool),
{comment, []}.
-spec super_fast(config()) -> {comment, []}.
super_fast(_Config) ->
Pool = super_fast,
ct:log("Check that the pool is working"),
{ok, ok} = send_io_format(Pool),
ct:log("Impossible task"),
Self = self(),
try wpool:call(Pool, {erlang, send, [Self, something]}, available_worker, 0) of
R ->
ct:fail("Unexpected ~p", [R])
catch
_:timeout ->
ok
end,
ct:log("Wait a second and nothing gets here"),
receive
X ->
ct:fail("Unexpected ~p", [X])
after 1000 ->
ok
end,
{comment, []}.
-spec queue_type_fifo(config()) -> {comment, []}.
queue_type_fifo(_Config) ->
Pool = queue_type_fifo,
Self = self(),
TasksNumber = 10,
Tasks = lists:seq(1, TasksNumber),
ct:log("Pretend worker is busy"),
wpool:cast(Pool, {timer, sleep, [timer:seconds(2)]}),
ct:log("Cast 10 enumerated tasks. Tasks should be queued because worker is busy."),
cast_tasks(Pool, TasksNumber, Self),
ct:log("Collect task results"),
Result = collect_tasks(TasksNumber),
ct:log("Check if tasks were performd in FIFO order."),
Result = Tasks,
{comment, []}.
-spec queue_type_lifo(config()) -> {comment, []}.
queue_type_lifo(_Config) ->
Pool = queue_type_lifo,
Self = self(),
TasksNumber = 10,
Tasks = lists:seq(1, TasksNumber),
ct:log("Pretend worker is busy"),
wpool:cast(Pool, {timer, sleep, [timer:seconds(4)]}),
ct:log("Cast 10 enumerated tasks. Tasks should be queued because worker is busy."),
cast_tasks(Pool, TasksNumber, Self),
ct:log("Collect task results"),
Result = collect_tasks(TasksNumber),
ct:log("Check if tasks were performd in LIFO order."),
Result = lists:reverse(Tasks),
{comment, []}.
-spec get_workers(config()) -> {comment, []}.
get_workers(_Config) ->
Pool = get_workers,
ct:log("Verify that there's the correct number of workers"),
Workers = wpool:get_workers(Pool),
?WORKERS = length(Workers),
ct:log("All workers are alive"),
true = lists:all(fun(Whereis) -> Whereis =/= undefined end, Workers),
{comment, []}.
-spec wpool_record(config()) -> {comment, []}.
wpool_record(_Config) ->
WPool = wpool_pool:find_wpool(wpool_record),
wpool_record = wpool_pool:wpool_get(name, WPool),
6 = wpool_pool:wpool_get(size, WPool),
[_, _, _, _] = wpool_pool:wpool_get([next, opts, qmanager, born], WPool),
WPool2 = wpool_pool:next(3, WPool),
3 = wpool_pool:wpool_get(next, WPool2),
{comment, []}.
-spec mess_up_with_store(config()) -> {comment, []}.
mess_up_with_store(_Config) ->
Pool = mess_up_with_store,
ct:comment("Mess up with ets table..."),
store_mess_up(Pool),
ct:comment("Rebuild stats"),
1 = proplists:get_value(next_worker, wpool:stats(Pool)),
ct:comment("Mess up with ets table again..."),
store_mess_up(Pool),
{ok, ok} = wpool:call(Pool, {io, format, ["1!~n"]}, random_worker),
ct:comment("Mess up with ets table once more..."),
{ok, ok} = wpool:call(Pool, {io, format, ["2!~n"]}, next_worker),
2 = proplists:get_value(next_worker, wpool:stats(Pool)),
store_mess_up(Pool),
{ok, ok} = wpool:call(Pool, {io, format, ["3!~n"]}, next_worker),
2 = proplists:get_value(next_worker, wpool:stats(Pool)),
ct:comment("Mess up with ets table one final time..."),
store_mess_up(Pool),
_ = wpool_pool:find_wpool(Pool),
ct:comment("Now, delete the pool"),
Flag = process_flag(trap_exit, true),
exit(whereis(Pool), kill),
ok =
ktn_task:wait_for(fun() ->
try wpool:call(Pool, {io, format, ["1!~n"]}, random_worker) of
X ->
{unexpected, X}
catch
_:no_workers ->
ok
end
end,
ok),
true = process_flag(trap_exit, Flag),
ct:comment("And now delete the ets table altogether"),
store_mess_up(Pool),
_ = wpool_pool:find_wpool(Pool),
wpool:stop(),
ok = wpool:start(),
{comment, []}.
cast_tasks(Pool, TasksNumber, ReplyTo) ->
lists:foreach(fun(N) -> wpool:cast(Pool, {erlang, send, [ReplyTo, {task, N}]}) end,
lists:seq(1, TasksNumber)).
collect_tasks(TasksNumber) ->
lists:map(fun(_) ->
receive
{task, N} ->
N
end
end,
lists:seq(1, TasksNumber)).
collect_results(0, Results) ->
Results;
collect_results(N, Results) ->
receive
{worker, WorkerId} ->
collect_results(N - 1, [WorkerId | Results])
after 100 ->
timeout
end.
send_io_format(Pool) ->
{ok, ok} = wpool:call(Pool, {io, format, ["ok!~n"]}, available_worker).
worker_msg_queue_lengths(Pool) ->
lists:usort([proplists:get_value(message_queue_len, WS)
|| {_, WS} <- proplists:get_value(workers, wpool:stats(Pool))]).
store_mess_up(Pool) ->
true = persistent_term:erase({wpool_pool, Pool}).