diff --git a/lib/jackalope/persistent_work_list.ex b/lib/jackalope/persistent_work_list.ex index e6ef0d5..a2a03c0 100644 --- a/lib/jackalope/persistent_work_list.ex +++ b/lib/jackalope/persistent_work_list.ex @@ -78,12 +78,8 @@ defmodule Jackalope.PersistentWorkList do end @impl GenServer - def handle_call(:count, _from, state) do - {:reply, count(state), state} - end - - def handle_call(:count_pending, _from, state) do - {:reply, count_pending(state), state} + def handle_call(:info, _from, state) do + {:reply, info(state), state} end def handle_call(:peek, _from, state) do @@ -151,6 +147,10 @@ defmodule Jackalope.PersistentWorkList do ## PRIVATE + defp info(state) do + %{count_waiting: count(state), count_pending: count_pending(state)} + end + defp count(state) do expired_count = Enum.count( @@ -504,18 +504,10 @@ defimpl Jackalope.WorkList, for: PID do end @impl Jackalope.WorkList - def count(work_list) do - GenServer.call(work_list, :count) + def info(work_list) do + GenServer.call(work_list, :info) end - @impl Jackalope.WorkList - def count_pending(work_list) do - GenServer.call(work_list, :count_pending) - end - - @impl Jackalope.WorkList - def empty?(work_list), do: peek(work_list) == nil - @impl Jackalope.WorkList def remove_all(work_list) do :ok = GenServer.call(work_list, :remove_all) diff --git a/lib/jackalope/transient_work_list.ex b/lib/jackalope/transient_work_list.ex index 9638ad3..cc1830b 100644 --- a/lib/jackalope/transient_work_list.ex +++ b/lib/jackalope/transient_work_list.ex @@ -169,18 +169,8 @@ defimpl Jackalope.WorkList, for: Jackalope.TransientWorkList do end @impl Jackalope.WorkList - def count(work_list) do - length(work_list.items) - end - - @impl Jackalope.WorkList - def count_pending(work_list) do - Enum.count(work_list.pending) - end - - @impl Jackalope.WorkList - def empty?(work_list) do - work_list.items == [] + def info(work_list) do + %{count_waiting: length(work_list.items), count_pending: Enum.count(work_list.pending)} end @impl Jackalope.WorkList diff --git a/lib/jackalope/work_list.ex b/lib/jackalope/work_list.ex index 45260f5..892d4cc 100644 --- a/lib/jackalope/work_list.ex +++ b/lib/jackalope/work_list.ex @@ -25,15 +25,12 @@ defprotocol Jackalope.WorkList do @doc "Remove an item from the pending set" @spec done(work_list(), reference()) :: {work_list(), item()} def done(work_list, ref) - @doc "Count the number of work items" - @spec count(work_list()) :: non_neg_integer() - def count(work_list) - @doc "Count the number of pending work items" - @spec count_pending(work_list()) :: non_neg_integer() - def count_pending(work_list) - @doc "Are there work items" - @spec empty?(work_list()) :: boolean - def empty?(work_list) + @doc "Info about the work list" + @spec info(work_list()) :: %{ + required(:count_waiting) => non_neg_integer(), + required(:count_pending) => non_neg_integer() + } + def info(work_list) @doc "Remove all work items" @spec remove_all(work_list()) :: work_list() def remove_all(work_list) diff --git a/test/jackalope_test.exs b/test/jackalope_test.exs index 2c54bf2..f1a212f 100644 --- a/test/jackalope_test.exs +++ b/test/jackalope_test.exs @@ -85,7 +85,7 @@ defmodule JackalopeTest do ) end) - assert WorkList.count(work_list) == 10 + assert count(work_list) == 10 end test "pending and done work items #{work_list_mod}", context do @@ -103,7 +103,7 @@ defmodule JackalopeTest do ) end) - assert WorkList.count(work_list) == 5 + assert count(work_list) == 5 ref = make_ref() @@ -112,7 +112,7 @@ defmodule JackalopeTest do |> WorkList.pending(ref) |> WorkList.done(ref) - assert WorkList.count(work_list) == 4 + assert count(work_list) == 4 end test "dropping work items #{work_list_mod}", context do @@ -130,7 +130,7 @@ defmodule JackalopeTest do ) end) - assert WorkList.count(work_list) == 10 + assert count(work_list) == 10 end test "reset_pending work items #{work_list_mod}", context do @@ -151,9 +151,9 @@ defmodule JackalopeTest do ref = make_ref() work_list = WorkList.pending(work_list, ref) - assert WorkList.count(work_list) == 4 + assert count(work_list) == 4 work_list = WorkList.reset_pending(work_list) - assert WorkList.count(work_list) == 5 + assert count(work_list) == 5 end end @@ -190,7 +190,7 @@ defmodule JackalopeTest do ) end) - assert WorkList.count(work_list) == 10 + assert count(work_list) == 10 ref = make_ref() work_list = WorkList.pending(work_list, ref) :ok = GenServer.stop(work_list, :normal) @@ -202,7 +202,12 @@ defmodule JackalopeTest do data_dir: "/tmp/jackalope" ) - assert WorkList.count(work_list) == 5 + assert count(work_list) == 5 + end + + defp count(work_list) do + %{count_waiting: count} = WorkList.info(work_list) + count end defp get_session_work_list() do @@ -255,7 +260,9 @@ defmodule JackalopeTest do if reset? do WorkList.remove_all(work_list) - assert WorkList.empty?(work_list) + info = WorkList.info(work_list) + assert info.count_waiting == 0 + assert info.count_pending == 0 end end