Skip to content

Commit

Permalink
Simplified WorkList API
Browse files Browse the repository at this point in the history
info/0 replaces count/1, count_pending/1 and empty?/1
  • Loading branch information
jfcloutier committed Jan 31, 2022
1 parent 308b259 commit d55aca9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 46 deletions.
24 changes: 8 additions & 16 deletions lib/jackalope/persistent_work_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 2 additions & 12 deletions lib/jackalope/transient_work_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions lib/jackalope/work_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,9 @@ 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()) :: map()
def info(work_list)
@doc "Remove all work items"
@spec remove_all(work_list()) :: work_list()
def remove_all(work_list)
Expand Down
25 changes: 16 additions & 9 deletions test/jackalope_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -103,7 +103,7 @@ defmodule JackalopeTest do
)
end)

assert WorkList.count(work_list) == 5
assert count(work_list) == 5

ref = make_ref()

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d55aca9

Please sign in to comment.