Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 405 lines (379 sloc) 15.641 kb
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
1 -module(poolboy_tests).
2
3 -include_lib("eunit/include/eunit.hrl").
4
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
5 -define(sync(Pid, Event),
6 gen_fsm:sync_send_all_state_event(Pid, Event)).
7
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
8 pool_test_() ->
9 {foreach,
10 fun() ->
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
11 error_logger:tty(false)
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
12 end,
13 fun(_) ->
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
14 case whereis(poolboy_test) of
15 undefined -> ok;
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
16 Pid -> ?sync(Pid, stop)
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
17 end,
18 error_logger:tty(true)
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
19 end,
20 [
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
21 {<<"Basic pool operations">>,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
22 fun pool_startup/0
23 },
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
24 {<<"Pool overflow should work">>,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
25 fun pool_overflow/0
26 },
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
27 {<<"Pool behaves when empty">>,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
28 fun pool_empty/0
29 },
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
30 {<<"Pool behaves when empty and oveflow is disabled">>,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
31 fun pool_empty_no_overflow/0
32 },
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
33 {<<"Pool behaves on worker death">>,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
34 fun worker_death/0
35 },
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
36 {<<"Pool behaves when full and a worker dies">>,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
37 fun worker_death_while_full/0
38 },
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
39 {<<"Pool behaves when full, a worker dies and overflow disabled">>,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
40 fun worker_death_while_full_no_overflow/0
41 },
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
42 {<<"Non-blocking pool behaves when full and overflow disabled">>,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
43 fun pool_full_nonblocking_no_overflow/0
44 },
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
45 {<<"Non-blocking pool behaves when full">>,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
46 fun pool_full_nonblocking/0
d27c012 @skeltoac Fix demonitored pids being left in state space and check monitor list le...
skeltoac authored
47 },
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
48 {<<"Pool behaves on owner death">>,
49 fun owner_death/0
e6aeeff Add typespecs and test Kevin Smith's worker init/stop functionality
Devin Torres authored
50 },
51 {<<"Pool worker init function called when workers when created">>,
52 fun worker_init_fun/0
53 },
54 {<<"Pool worker stop function called on workers when destroyed">>,
55 fun worker_stop_fun/0
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
56 }
57 ]
58 }.
59
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
60 %% Tell a worker to exit and await its impending doom.
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
61 kill_worker(Pid) ->
62 erlang:monitor(process, Pid),
63 gen_server:call(Pid, die),
64 receive
65 {'DOWN', _, process, Pid, _} ->
66 ok
67 end.
68
69 checkin_worker(Pid, Worker) ->
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
70 %% There's no easy way to wait for a checkin to complete, because it's
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
71 %% async and the supervisor may kill the process if it was an overflow
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
72 %% worker. The only solution seems to be a nasty hardcoded sleep.
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
73 poolboy:checkin(Pid, Worker),
74 timer:sleep(500).
75
76 pool_startup() ->
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
77 %% Check basic pool operation.
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
78 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
79 {worker_module, poolboy_test_worker},
80 {size, 10}, {max_overflow, 5}]),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
81 ?assertEqual(10, length(?sync(Pid, get_avail_workers))),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
82 poolboy:checkout(Pid),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
83 ?assertEqual(9, length(?sync(Pid, get_avail_workers))),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
84 Worker = poolboy:checkout(Pid),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
85 ?assertEqual(8, length(?sync(Pid, get_avail_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
86 checkin_worker(Pid, Worker),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
87 ?assertEqual(9, length(?sync(Pid, get_avail_workers))),
88 ?assertEqual(1, length(?sync(Pid, get_all_monitors))),
89 ok = ?sync(Pid, stop).
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
90
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
91 pool_overflow() ->
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
92 %% Check that the pool overflows properly.
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
93 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
94 {worker_module, poolboy_test_worker},
95 {size, 5}, {max_overflow, 5}]),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
96 Workers = [poolboy:checkout(Pid) || _ <- lists:seq(0, 6)],
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
97 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
98 ?assertEqual(7, length(?sync(Pid, get_all_workers))),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
99 [A, B, C, D, E, F, G] = Workers,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
100 checkin_worker(Pid, A),
101 checkin_worker(Pid, B),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
102 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
103 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
104 checkin_worker(Pid, C),
105 checkin_worker(Pid, D),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
106 ?assertEqual(2, length(?sync(Pid, get_avail_workers))),
107 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
108 checkin_worker(Pid, E),
109 checkin_worker(Pid, F),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
110 ?assertEqual(4, length(?sync(Pid, get_avail_workers))),
111 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
112 checkin_worker(Pid, G),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
113 ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
114 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
115 ?assertEqual(0, length(?sync(Pid, get_all_monitors))),
116 ok = ?sync(Pid, stop).
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
117
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
118 pool_empty() ->
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
119 %% Checks that the the pool handles the empty condition correctly when
120 %% overflow is enabled.
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
121 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
122 {worker_module, poolboy_test_worker},
123 {size, 5}, {max_overflow, 2}]),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
124 Workers = [poolboy:checkout(Pid) || _ <- lists:seq(0, 6)],
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
125 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
126 ?assertEqual(7, length(?sync(Pid, get_all_workers))),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
127 [A, B, C, D, E, F, G] = Workers,
128 Self = self(),
129 spawn(fun() ->
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
130 Worker = poolboy:checkout(Pid),
131 Self ! got_worker,
132 checkin_worker(Pid, Worker)
133 end),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
134
135 %% Spawned process should block waiting for worker to be available.
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
136 receive
137 got_worker -> ?assert(false)
138 after
139 500 -> ?assert(true)
140 end,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
141 checkin_worker(Pid, A),
142 checkin_worker(Pid, B),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
143
144 %% Spawned process should have been able to obtain a worker.
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
145 receive
146 got_worker -> ?assert(true)
147 after
148 500 -> ?assert(false)
149 end,
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
150 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
151 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
152 checkin_worker(Pid, C),
153 checkin_worker(Pid, D),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
154 ?assertEqual(2, length(?sync(Pid, get_avail_workers))),
155 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
156 checkin_worker(Pid, E),
157 checkin_worker(Pid, F),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
158 ?assertEqual(4, length(?sync(Pid, get_avail_workers))),
159 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
160 checkin_worker(Pid, G),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
161 ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
162 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
163 ?assertEqual(0, length(?sync(Pid, get_all_monitors))),
164 ok = ?sync(Pid, stop).
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
165
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
166 pool_empty_no_overflow() ->
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
167 %% Checks the pool handles the empty condition properly when overflow is
168 %% disabled.
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
169 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
170 {worker_module, poolboy_test_worker},
171 {size, 5}, {max_overflow, 0}]),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
172 Workers = [poolboy:checkout(Pid) || _ <- lists:seq(0, 4)],
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
173 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
174 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
175 [A, B, C, D, E] = Workers,
176 Self = self(),
177 spawn(fun() ->
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
178 Worker = poolboy:checkout(Pid),
179 Self ! got_worker,
180 checkin_worker(Pid, Worker)
181 end),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
182
183 %% Spawned process should block waiting for worker to be available.
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
184 receive
185 got_worker -> ?assert(false)
186 after
187 500 -> ?assert(true)
188 end,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
189 checkin_worker(Pid, A),
190 checkin_worker(Pid, B),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
191
192 %% Spawned process should have been able to obtain a worker.
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
193 receive
194 got_worker -> ?assert(true)
195 after
196 500 -> ?assert(false)
197 end,
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
198 ?assertEqual(2, length(?sync(Pid, get_avail_workers))),
199 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
200 checkin_worker(Pid, C),
201 checkin_worker(Pid, D),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
202 ?assertEqual(4, length(?sync(Pid, get_avail_workers))),
203 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
204 checkin_worker(Pid, E),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
205 ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
206 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
207 ?assertEqual(0, length(?sync(Pid, get_all_monitors))),
208 ok = ?sync(Pid, stop).
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
209
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
210 worker_death() ->
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
211 %% Check that dead workers are only restarted when the pool is not full
212 %% and the overflow count is 0. Meaning, don't restart overflow workers.
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
213 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
214 {worker_module, poolboy_test_worker},
215 {size, 5}, {max_overflow, 2}]),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
216 Worker = poolboy:checkout(Pid),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
217 kill_worker(Worker),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
218 ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
219 [A, B, C|_Workers] = [poolboy:checkout(Pid) || _ <- lists:seq(0, 6)],
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
220 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
221 ?assertEqual(7, length(?sync(Pid, get_all_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
222 kill_worker(A),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
223 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
224 ?assertEqual(6, length(?sync(Pid, get_all_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
225 kill_worker(B),
226 kill_worker(C),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
227 ?assertEqual(1, length(?sync(Pid, get_avail_workers))),
228 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
229 ?assertEqual(4, length(?sync(Pid, get_all_monitors))),
230 ok = ?sync(Pid, stop).
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
231
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
232 worker_death_while_full() ->
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
233 %% Check that if a worker dies while the pool is full and there is a
234 %% queued checkout, a new worker is started and the checkout serviced.
235 %% If there are no queued checkouts, a new worker is not started.
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
236 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
237 {worker_module, poolboy_test_worker},
238 {size, 5}, {max_overflow, 2}]),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
239 Worker = poolboy:checkout(Pid),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
240 kill_worker(Worker),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
241 ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
242 [A, B|_Workers] = [poolboy:checkout(Pid) || _ <- lists:seq(0, 6)],
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
243 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
244 ?assertEqual(7, length(?sync(Pid, get_all_workers))),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
245 Self = self(),
246 spawn(fun() ->
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
247 poolboy:checkout(Pid),
248 Self ! got_worker
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
249 %% XXX: Don't release the worker. We want to also test what happens
250 %% when the worker pool is full and a worker dies with no queued
251 %% checkouts.
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
252 end),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
253
254 %% Spawned process should block waiting for worker to be available.
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
255 receive
256 got_worker -> ?assert(false)
257 after
258 500 -> ?assert(true)
259 end,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
260 kill_worker(A),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
261
262 %% Spawned process should have been able to obtain a worker.
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
263 receive
264 got_worker -> ?assert(true)
265 after
266 1000 -> ?assert(false)
267 end,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
268 kill_worker(B),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
269 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
270 ?assertEqual(6, length(?sync(Pid, get_all_workers))),
271 ?assertEqual(6, length(?sync(Pid, get_all_monitors))),
272 ok = ?sync(Pid, stop).
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
273
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
274 worker_death_while_full_no_overflow() ->
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
275 %% Check that if a worker dies while the pool is full and there's no
276 %% overflow, a new worker is started unconditionally and any queued
277 %% checkouts are serviced.
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
278 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
279 {worker_module, poolboy_test_worker},
280 {size, 5}, {max_overflow, 0}]),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
281 Worker = poolboy:checkout(Pid),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
282 kill_worker(Worker),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
283 ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
284 [A, B, C|_Workers] = [poolboy:checkout(Pid) || _ <- lists:seq(0, 4)],
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
285 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
286 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
287 Self = self(),
288 spawn(fun() ->
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
289 poolboy:checkout(Pid),
290 Self ! got_worker
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
291 %% XXX: Do not release, need to also test when worker dies and no
292 %% checkouts queued.
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
293 end),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
294
295 %% Spawned process should block waiting for worker to be available.
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
296 receive
297 got_worker -> ?assert(false)
298 after
299 500 -> ?assert(true)
300 end,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
301 kill_worker(A),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
302
303 %% Spawned process should have been able to obtain a worker.
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
304 receive
305 got_worker -> ?assert(true)
306 after
307 1000 -> ?assert(false)
308 end,
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
309 kill_worker(B),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
310 ?assertEqual(1, length(?sync(Pid, get_avail_workers))),
311 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
312 kill_worker(C),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
313 ?assertEqual(2, length(?sync(Pid, get_avail_workers))),
314 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
315 ?assertEqual(3, length(?sync(Pid, get_all_monitors))),
316 ok = ?sync(Pid, stop).
2f712e4 @Vagabond Add tests & some hooks to poolboy to let the tests check the pool state
Vagabond authored
317
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
318 pool_full_nonblocking_no_overflow() ->
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
319 %% Check that when the pool is full, checkouts return 'full' when the
320 %% option to use non-blocking checkouts is used.
3d70538 @Vagabond Add tests for nonblocking checkout, fix a bug found while testing
Vagabond authored
321 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
322 {worker_module, poolboy_test_worker},
323 {size, 5}, {max_overflow, 0}]),
3d70538 @Vagabond Add tests for nonblocking checkout, fix a bug found while testing
Vagabond authored
324 Workers = [poolboy:checkout(Pid) || _ <- lists:seq(0, 4)],
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
325 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
326 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
327 ?assertEqual(full, poolboy:checkout(Pid, false)),
328 ?assertEqual(full, poolboy:checkout(Pid, false)),
3d70538 @Vagabond Add tests for nonblocking checkout, fix a bug found while testing
Vagabond authored
329 A = hd(Workers),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
330 checkin_worker(Pid, A),
3d70538 @Vagabond Add tests for nonblocking checkout, fix a bug found while testing
Vagabond authored
331 ?assertEqual(A, poolboy:checkout(Pid)),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
332 ?assertEqual(5, length(?sync(Pid, get_all_monitors))),
333 ok = ?sync(Pid, stop).
3d70538 @Vagabond Add tests for nonblocking checkout, fix a bug found while testing
Vagabond authored
334
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
335 pool_full_nonblocking() ->
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
336 %% Check that when the pool is full, checkouts return 'full' when the
337 %% option to use non-blocking checkouts is used.
3d70538 @Vagabond Add tests for nonblocking checkout, fix a bug found while testing
Vagabond authored
338 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
339 {worker_module, poolboy_test_worker},
340 {size, 5}, {max_overflow, 5}]),
3d70538 @Vagabond Add tests for nonblocking checkout, fix a bug found while testing
Vagabond authored
341 Workers = [poolboy:checkout(Pid) || _ <- lists:seq(0, 9)],
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
342 ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
343 ?assertEqual(10, length(?sync(Pid, get_all_workers))),
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
344 ?assertEqual(full, poolboy:checkout(Pid, false)),
3d70538 @Vagabond Add tests for nonblocking checkout, fix a bug found while testing
Vagabond authored
345 A = hd(Workers),
e038af9 @Vagabond Refactor tests to be less prone to random failure & be quieter
Vagabond authored
346 checkin_worker(Pid, A),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
347 NewWorker = poolboy:checkout(Pid, false),
348 ?assertEqual(false, is_process_alive(A)), %% Overflow workers get shutdown
3d70538 @Vagabond Add tests for nonblocking checkout, fix a bug found while testing
Vagabond authored
349 ?assert(is_pid(NewWorker)),
9733a39 Change blocking checkout interface to checkout(PoolName, Block)
Devin Torres authored
350 ?assertEqual(full, poolboy:checkout(Pid, false)),
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
351 ?assertEqual(10, length(?sync(Pid, get_all_monitors))),
352 ok = ?sync(Pid, stop).
d27c012 @skeltoac Fix demonitored pids being left in state space and check monitor list le...
skeltoac authored
353
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
354 owner_death() ->
355 %% Check that a dead owner (a process that dies with a worker checked out)
d27c012 @skeltoac Fix demonitored pids being left in state space and check monitor list le...
skeltoac authored
356 %% causes the pool to dismiss the worker and prune the state space.
357 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
358 {worker_module, poolboy_test_worker},
359 {size, 5}, {max_overflow, 5}]),
d27c012 @skeltoac Fix demonitored pids being left in state space and check monitor list le...
skeltoac authored
360 spawn(fun() ->
7a376a4 Merge cleanup and try to cleanup the tests
Devin Torres authored
361 poolboy:checkout(Pid),
362 receive after 500 -> exit(normal) end
363 end),
364 timer:sleep(1000),
365 ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
366 ?assertEqual(5, length(?sync(Pid, get_all_workers))),
367 ?assertEqual(0, length(?sync(Pid, get_all_monitors))),
368 ok = ?sync(Pid, stop).
e6aeeff Add typespecs and test Kevin Smith's worker init/stop functionality
Devin Torres authored
369
370 worker_init_fun() ->
371 Self = self(),
372 InitFun = fun(Worker) ->
373 Self ! worked,
374 {ok, Worker}
375 end,
376 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
377 {worker_module, poolboy_test_worker},
378 {init_fun, InitFun}]),
379 poolboy:checkout(Pid),
380 receive
381 worked -> ?assert(true)
382 after
383 1000 -> ?assert(false)
384 end,
385 ok = ?sync(Pid, stop).
386
387 worker_stop_fun() ->
388 Self = self(),
389 StopFun = fun(Worker) ->
390 Self ! worked,
391 Worker ! stop
392 end,
393 {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
394 {worker_module, poolboy_test_worker},
395 {stop_fun, StopFun},
396 {size, 0}, {max_overflow, 1}]),
397 Worker = poolboy:checkout(Pid),
398 checkin_worker(Pid, Worker),
399 receive
400 worked -> ?assert(true)
401 after
402 1000 -> ?assert(false)
403 end,
404 ok = ?sync(Pid, stop).
Something went wrong with that request. Please try again.