From 32e498bde2388848a84b337c99939de81848a566 Mon Sep 17 00:00:00 2001 From: Richard Kotze Date: Sat, 25 Feb 2017 20:49:38 +0000 Subject: [PATCH] fix test to check exec of tasks are called. --- lib/eye_drops/eye_ball/eye_ball.ex | 12 +++++----- lib/eye_drops/eye_ball/eye_ball_test.exs | 30 +++++++++++------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/eye_drops/eye_ball/eye_ball.ex b/lib/eye_drops/eye_ball/eye_ball.ex index 27097e8..e8308df 100644 --- a/lib/eye_drops/eye_ball/eye_ball.ex +++ b/lib/eye_drops/eye_ball/eye_ball.ex @@ -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, []) @@ -31,7 +31,7 @@ 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 @@ -39,12 +39,12 @@ defmodule EyeDrops.EyeBall do {: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) diff --git a/lib/eye_drops/eye_ball/eye_ball_test.exs b/lib/eye_drops/eye_ball/eye_ball_test.exs index e33c2ff..cc626be 100644 --- a/lib/eye_drops/eye_ball/eye_ball_test.exs +++ b/lib/eye_drops/eye_ball/eye_ball_test.exs @@ -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 \ No newline at end of file