Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 407 lines (381 sloc) 15.695 kb
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
-module(poolboy_tests).

-include_lib("eunit/include/eunit.hrl").

-define(sync(Pid, Event),
    gen_fsm:sync_send_all_state_event(Pid, Event)).

pool_test_() ->
    {foreach,
        fun() ->
            error_logger:tty(false)
        end,
        fun(_) ->
            case whereis(poolboy_test) of
                undefined -> ok;
                Pid -> ?sync(Pid, stop)
            end,
            error_logger:tty(true)
        end,
        [
            {<<"Basic pool operations">>,
                fun pool_startup/0
            },
            {<<"Pool overflow should work">>,
                fun pool_overflow/0
            },
            {<<"Pool behaves when empty">>,
                fun pool_empty/0
            },
            {<<"Pool behaves when empty and oveflow is disabled">>,
                fun pool_empty_no_overflow/0
            },
            {<<"Pool behaves on worker death">>,
                fun worker_death/0
            },
            {<<"Pool behaves when full and a worker dies">>,
                fun worker_death_while_full/0
            },
            {<<"Pool behaves when full, a worker dies and overflow disabled">>,
                fun worker_death_while_full_no_overflow/0
            },
            {<<"Non-blocking pool behaves when full and overflow disabled">>,
                fun pool_full_nonblocking_no_overflow/0
            },
            {<<"Non-blocking pool behaves when full">>,
                fun pool_full_nonblocking/0
            },
            {<<"Pool behaves on owner death">>,
                fun owner_death/0
            },
            {<<"Pool worker init function called when workers when created">>,
                fun worker_init_fun/0
            },
            {<<"Pool worker stop function called on workers when destroyed">>,
                fun worker_stop_fun/0
            }
        ]
    }.

%% Tell a worker to exit and await its impending doom.
kill_worker(Pid) ->
    erlang:monitor(process, Pid),
    gen_server:call(Pid, die),
    receive
        {'DOWN', _, process, Pid, _} ->
            ok
    end.

checkin_worker(Pid, Worker) ->
    %% There's no easy way to wait for a checkin to complete, because it's
    %% async and the supervisor may kill the process if it was an overflow
    %% worker. The only solution seems to be a nasty hardcoded sleep.
    poolboy:checkin(Pid, Worker),
    timer:sleep(500).

pool_startup() ->
    %% Check basic pool operation.
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {size, 10}, {max_overflow, 5}]),
    ?assertEqual(10, length(?sync(Pid, get_avail_workers))),
    poolboy:checkout(Pid),
    ?assertEqual(9, length(?sync(Pid, get_avail_workers))),
    Worker = poolboy:checkout(Pid),
    ?assertEqual(8, length(?sync(Pid, get_avail_workers))),
    checkin_worker(Pid, Worker),
    ?assertEqual(9, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(1, length(?sync(Pid, get_all_monitors))),
    ok = ?sync(Pid, stop).

pool_overflow() ->
    %% Check that the pool overflows properly.
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {size, 5}, {max_overflow, 5}]),
    Workers = [poolboy:checkout(Pid) || _ <- lists:seq(0, 6)],
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(7, length(?sync(Pid, get_all_workers))),
    [A, B, C, D, E, F, G] = Workers,
    checkin_worker(Pid, A),
    checkin_worker(Pid, B),
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    checkin_worker(Pid, C),
    checkin_worker(Pid, D),
    ?assertEqual(2, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    checkin_worker(Pid, E),
    checkin_worker(Pid, F),
    ?assertEqual(4, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    checkin_worker(Pid, G),
    ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    ?assertEqual(0, length(?sync(Pid, get_all_monitors))),
    ok = ?sync(Pid, stop).

pool_empty() ->
    %% Checks that the the pool handles the empty condition correctly when
    %% overflow is enabled.
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {size, 5}, {max_overflow, 2}]),
    Workers = [poolboy:checkout(Pid) || _ <- lists:seq(0, 6)],
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(7, length(?sync(Pid, get_all_workers))),
    [A, B, C, D, E, F, G] = Workers,
    Self = self(),
    spawn(fun() ->
        Worker = poolboy:checkout(Pid),
        Self ! got_worker,
        checkin_worker(Pid, Worker)
    end),

    %% Spawned process should block waiting for worker to be available.
    receive
        got_worker -> ?assert(false)
    after
        500 -> ?assert(true)
    end,
    checkin_worker(Pid, A),
    checkin_worker(Pid, B),

    %% Spawned process should have been able to obtain a worker.
    receive
        got_worker -> ?assert(true)
    after
        500 -> ?assert(false)
    end,
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    checkin_worker(Pid, C),
    checkin_worker(Pid, D),
    ?assertEqual(2, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    checkin_worker(Pid, E),
    checkin_worker(Pid, F),
    ?assertEqual(4, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    checkin_worker(Pid, G),
    ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    ?assertEqual(0, length(?sync(Pid, get_all_monitors))),
    ok = ?sync(Pid, stop).

pool_empty_no_overflow() ->
    %% Checks the pool handles the empty condition properly when overflow is
    %% disabled.
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {size, 5}, {max_overflow, 0}]),
    Workers = [poolboy:checkout(Pid) || _ <- lists:seq(0, 4)],
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    [A, B, C, D, E] = Workers,
    Self = self(),
    spawn(fun() ->
        Worker = poolboy:checkout(Pid),
        Self ! got_worker,
        checkin_worker(Pid, Worker)
    end),

    %% Spawned process should block waiting for worker to be available.
    receive
        got_worker -> ?assert(false)
    after
        500 -> ?assert(true)
    end,
    checkin_worker(Pid, A),
    checkin_worker(Pid, B),

    %% Spawned process should have been able to obtain a worker.
    receive
        got_worker -> ?assert(true)
    after
        500 -> ?assert(false)
    end,
    ?assertEqual(2, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    checkin_worker(Pid, C),
    checkin_worker(Pid, D),
    ?assertEqual(4, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    checkin_worker(Pid, E),
    ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    ?assertEqual(0, length(?sync(Pid, get_all_monitors))),
    ok = ?sync(Pid, stop).

worker_death() ->
    %% Check that dead workers are only restarted when the pool is not full
    %% and the overflow count is 0. Meaning, don't restart overflow workers.
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {size, 5}, {max_overflow, 2}]),
    Worker = poolboy:checkout(Pid),
    kill_worker(Worker),
    ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
    [A, B, C|_Workers] = [poolboy:checkout(Pid) || _ <- lists:seq(0, 6)],
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(7, length(?sync(Pid, get_all_workers))),
    kill_worker(A),
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(6, length(?sync(Pid, get_all_workers))),
    kill_worker(B),
    kill_worker(C),
    ?assertEqual(1, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    ?assertEqual(4, length(?sync(Pid, get_all_monitors))),
    ok = ?sync(Pid, stop).

worker_death_while_full() ->
    %% Check that if a worker dies while the pool is full and there is a
    %% queued checkout, a new worker is started and the checkout serviced.
    %% If there are no queued checkouts, a new worker is not started.
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {size, 5}, {max_overflow, 2}]),
    Worker = poolboy:checkout(Pid),
    kill_worker(Worker),
    ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
    [A, B|_Workers] = [poolboy:checkout(Pid) || _ <- lists:seq(0, 6)],
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(7, length(?sync(Pid, get_all_workers))),
    Self = self(),
    spawn(fun() ->
        poolboy:checkout(Pid),
        Self ! got_worker,
        %% XXX: Don't release the worker. We want to also test what happens
        %% when the worker pool is full and a worker dies with no queued
        %% checkouts.
        timer:sleep(5000)
    end),

    %% Spawned process should block waiting for worker to be available.
    receive
        got_worker -> ?assert(false)
    after
        500 -> ?assert(true)
    end,
    kill_worker(A),

    %% Spawned process should have been able to obtain a worker.
    receive
        got_worker -> ?assert(true)
    after
        1000 -> ?assert(false)
    end,
    kill_worker(B),
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(6, length(?sync(Pid, get_all_workers))),
    ?assertEqual(6, length(?sync(Pid, get_all_monitors))),
    ok = ?sync(Pid, stop).

worker_death_while_full_no_overflow() ->
    %% Check that if a worker dies while the pool is full and there's no
    %% overflow, a new worker is started unconditionally and any queued
    %% checkouts are serviced.
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {size, 5}, {max_overflow, 0}]),
    Worker = poolboy:checkout(Pid),
    kill_worker(Worker),
    ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
    [A, B, C|_Workers] = [poolboy:checkout(Pid) || _ <- lists:seq(0, 4)],
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    Self = self(),
    spawn(fun() ->
        poolboy:checkout(Pid),
        Self ! got_worker,
        %% XXX: Do not release, need to also test when worker dies and no
        %% checkouts queued.
        timer:sleep(5000)
    end),

    %% Spawned process should block waiting for worker to be available.
    receive
        got_worker -> ?assert(false)
    after
        500 -> ?assert(true)
    end,
    kill_worker(A),

    %% Spawned process should have been able to obtain a worker.
    receive
        got_worker -> ?assert(true)
    after
        1000 -> ?assert(false)
    end,
    kill_worker(B),
    ?assertEqual(1, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    kill_worker(C),
    ?assertEqual(2, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    ?assertEqual(3, length(?sync(Pid, get_all_monitors))),
    ok = ?sync(Pid, stop).

pool_full_nonblocking_no_overflow() ->
    %% Check that when the pool is full, checkouts return 'full' when the
    %% option to use non-blocking checkouts is used.
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {size, 5}, {max_overflow, 0}]),
    Workers = [poolboy:checkout(Pid) || _ <- lists:seq(0, 4)],
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    ?assertEqual(full, poolboy:checkout(Pid, false)),
    ?assertEqual(full, poolboy:checkout(Pid, false)),
    A = hd(Workers),
    checkin_worker(Pid, A),
    ?assertEqual(A, poolboy:checkout(Pid)),
    ?assertEqual(5, length(?sync(Pid, get_all_monitors))),
    ok = ?sync(Pid, stop).

pool_full_nonblocking() ->
    %% Check that when the pool is full, checkouts return 'full' when the
    %% option to use non-blocking checkouts is used.
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {size, 5}, {max_overflow, 5}]),
    Workers = [poolboy:checkout(Pid) || _ <- lists:seq(0, 9)],
    ?assertEqual(0, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(10, length(?sync(Pid, get_all_workers))),
    ?assertEqual(full, poolboy:checkout(Pid, false)),
    A = hd(Workers),
    checkin_worker(Pid, A),
    NewWorker = poolboy:checkout(Pid, false),
    ?assertEqual(false, is_process_alive(A)), %% Overflow workers get shutdown
    ?assert(is_pid(NewWorker)),
    ?assertEqual(full, poolboy:checkout(Pid, false)),
    ?assertEqual(10, length(?sync(Pid, get_all_monitors))),
    ok = ?sync(Pid, stop).

owner_death() ->
    %% Check that a dead owner (a process that dies with a worker checked out)
    %% causes the pool to dismiss the worker and prune the state space.
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {size, 5}, {max_overflow, 5}]),
    spawn(fun() ->
        poolboy:checkout(Pid),
        receive after 500 -> exit(normal) end
    end),
    timer:sleep(1000),
    ?assertEqual(5, length(?sync(Pid, get_avail_workers))),
    ?assertEqual(5, length(?sync(Pid, get_all_workers))),
    ?assertEqual(0, length(?sync(Pid, get_all_monitors))),
    ok = ?sync(Pid, stop).

worker_init_fun() ->
    Self = self(),
    InitFun = fun(Worker) ->
        Self ! worked,
        {ok, Worker}
    end,
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {init_fun, InitFun}]),
    poolboy:checkout(Pid),
    receive
        worked -> ?assert(true)
    after
        1000 -> ?assert(false)
    end,
    ok = ?sync(Pid, stop).

worker_stop_fun() ->
    Self = self(),
    StopFun = fun(Worker) ->
        Self ! worked,
        Worker ! stop
    end,
    {ok, Pid} = poolboy:start_link([{name, {local, poolboy_test}},
                                    {worker_module, poolboy_test_worker},
                                    {stop_fun, StopFun},
                                    {size, 0}, {max_overflow, 1}]),
    Worker = poolboy:checkout(Pid),
    checkin_worker(Pid, Worker),
    receive
        worked -> ?assert(true)
    after
        1000 -> ?assert(false)
    end,
    ok = ?sync(Pid, stop).
Something went wrong with that request. Please try again.