Skip to content

Commit

Permalink
fix test to check exec of tasks are called.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkotze committed Feb 25, 2017
1 parent d9faa3c commit 32e498b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
12 changes: 6 additions & 6 deletions lib/eye_drops/eye_ball/eye_ball.ex
Expand Up @@ -8,18 +8,18 @@ defmodule EyeDrops.EyeBall do
GenServer.start_link(__MODULE__, tasks, name: __MODULE__)
end

# @spec look(pid, atom) :: none
@spec look(pid, atom) :: none
def look(server, key) do
GenServer.call(server, {:lookup, key})
end

# @spec run_on_start(pid) :: none
@spec run_on_start(pid) :: none
def run_on_start(server) do
GenServer.call(server, :run_on_start)
end

# GenServer implementation
# @spec init(map) :: {:ok, map}
@spec init(map) :: {:ok, map}
def init(tasks) do
:ok = :fs.subscribe
include_list = Map.get(tasks, :include_tasks, [])
Expand All @@ -31,20 +31,20 @@ defmodule EyeDrops.EyeBall do
{:ok, %{tasks: eye_tasks}}
end

# @spec handle_info({pid, tuple, tuple}, any) :: {:noreply, any}
@spec handle_info({pid, tuple, tuple}, any) :: {:noreply, any}
def handle_info({_pid, {:fs, :file_event}, {path, _event}}, state) do
:ok = Tasks.to_run(state.tasks, to_string(path))
|> Tasks.exec

{:noreply, state}
end

# @spec handle_call(tuple, any, any) :: {:reply, any, any}
@spec handle_call(tuple, any, any) :: {:reply, any, any}
def handle_call({:lookup, name}, _from, state) do
{:reply, Map.fetch(state, name), state}
end

# @spec handle_call(atom, any, any) :: {:reply, any, any}
@spec handle_call(atom, any, any) :: {:reply, any, any}
def handle_call(:run_on_start, _from, state) do
tasks = Tasks.run_on_start(state.tasks)
:ok = Tasks.exec(tasks)
Expand Down
30 changes: 14 additions & 16 deletions lib/eye_drops/eye_ball/eye_ball_test.exs
Expand Up @@ -17,24 +17,22 @@ defmodule EyeDrops.EyeBallTest do
assert Enum.count(tasks) == 2
end

test "Eye ball handle_info is run with no tasks" do
with_mock EyeDrops.Tasks, [:passthrough], [
exec: fn ([]) -> :ok end]
do
{:ok, pid} = EyeDrops.EyeBall.open(%{})
send(pid, {pid, {:fs, :file_event}, {"path/does/not/exist.ex", "event"}})
assert called EyeDrops.Tasks.exec([])
end
test_with_mock "Eye ball handle_info is run with no tasks", EyeDrops.Tasks, [:passthrough], [
exec: fn ([]) -> :ok end]
do
{:ok, pid} = EyeDrops.EyeBall.open(%{})
message = {pid, {:fs, :file_event}, {"path/does/not/exist.ex", "event"}}
send(pid, message)
:timer.sleep(50) # hack to wait for send message
assert called EyeDrops.Tasks.exec([])
end

test "Eye ball run tasks on start of eye drops" do
with_mock EyeDrops.Tasks, [:passthrough], [
exec: fn (_tasks) -> :ok end]
do
{:ok, all_tasks} = EyeDrops.EyeBall.open(%{})
EyeDrops.EyeBall.run_on_start(all_tasks)
test_with_mock "Eye ball run tasks on start of eye drops", EyeDrops.Tasks, [:passthrough], [
exec: fn (_tasks) -> :ok end]
do
{:ok, all_tasks} = EyeDrops.EyeBall.open(%{})
EyeDrops.EyeBall.run_on_start(all_tasks)

assert called EyeDrops.Tasks.exec(:_)
end
assert called EyeDrops.Tasks.exec(:_)
end
end

0 comments on commit 32e498b

Please sign in to comment.