From 0d28c28a85582d2b7104f2caefe0c56f5062c578 Mon Sep 17 00:00:00 2001 From: kballou Date: Tue, 21 Oct 2014 18:05:37 -0600 Subject: Add some basic tests for pool scheduler --- test/poolparty_test.exs | 60 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/test/poolparty_test.exs b/test/poolparty_test.exs index 761c433..6e66bda 100644 --- a/test/poolparty_test.exs +++ b/test/poolparty_test.exs @@ -1,3 +1,59 @@ -defmodule PoolpartyTest do - use ExUnit.Case +defmodule PoolPartyTest do + use ExUnit.Case, aysnc: false + + setup do + {:ok, pool} = PoolParty.start(nil, nil) + on_exit(pool, fn -> Application.stop(pool) end) + {:ok, pool: pool} + end + + test "pool can perform work", %{pool: _} do + :timer.sleep(100) + [:ok, :ok, :ok, :ok, :ok, :ok, :ok, :ok] = + (1..8) |> + Enum.map(fn(x) -> PoolParty.Scheduler.process(&(&1*&1), x, self()) end) + :timer.sleep(100) + state = :sys.get_state(PoolParty.Scheduler) + assert_receive {:result, 1} + assert_receive {:result, 4} + assert_receive {:result, 9} + assert_receive {:result, 16} + assert_receive {:result, 25} + assert_receive {:result, 36} + assert_receive {:result, 49} + assert_receive {:result, 64} + assert(state.max_pool_size == Application.get_env(:poolparty, :pool_size)) + assert(length(state.queue) == 0) + end + + test "pool queues work when full", %{pool: _} do + :timer.sleep(100) + (1..16) |> + Enum.map(fn(x) -> + PoolParty.Scheduler.process( + fn(x) -> + :timer.sleep(100) + x + end, + x, self()) end) + state = :sys.get_state(PoolParty.Scheduler) + assert(length(state.queue) == 8) + assert(HashDict.size(state.processing) == 8) + end + + test "pool can handle worker leaving", %{pool: _} do + :timer.sleep(100) + PoolParty.Scheduler.process(fn(_)-> exit(:kill) end, [], self()) + assert(HashDict.size(:sys.get_state(PoolParty.Scheduler).processing) == 1) + :timer.sleep(100) + state = :sys.get_state(PoolParty.Scheduler) + assert_receive {:result, :failed} + assert(length(state.workers) == 8) + assert(HashDict.size(state.processing) == 0) + end + + test "pool handles leaving process without fail", %{pool: _} do + :timer.sleep(100) + PoolParty.Scheduler.leave(self()) + end end -- cgit v1.2.1