diff --git a/lib/exvcr/actor.ex b/lib/exvcr/actor.ex index cebba86..de9a6dc 100644 --- a/lib/exvcr/actor.ex +++ b/lib/exvcr/actor.ex @@ -8,29 +8,71 @@ defmodule ExVCR.Actor do Stores request/response for the recorder. """ - use ExActor.GenServer + use GenServer - defstart start(arg), do: initial_state(arg) + def start(arg) do + GenServer.start(__MODULE__, arg) + end + + def append(pid, x) do + GenServer.cast(pid, {:append, x}) + end + + def set(pid, x) do + GenServer.cast(pid, {:set, x}) + end + + def get(pid) do + GenServer.call(pid, :get) + end + + def update(pid, finder, updater) do + GenServer.call(pid, {:update, finder, updater}) + end + + def pop(pid) do + GenServer.call(pid, :pop) + end - defcast append(x), state: state, do: new_state([x|state]) - defcast set(x), do: new_state(x) - defcall get, state: state, do: reply(state) + # Callbacks - defcall update(finder, updater), state: state do - state = Enum.map(state, fn(record) -> + @impl true + def init(arg) do + {:ok, arg} + end + + @impl true + def handle_cast({:append, x}, state) do + {:noreply, [x|state]} + end + + @impl true + def handle_cast({:set, x}, _state) do + {:noreply, x} + end + + @impl true + def handle_call(:get, _from, state) do + {:reply, state, state} + end + + @impl true + def handle_call({:update, finder, updater}, _from, state) do + new_state = Enum.map(state, fn(record) -> if finder.(record) do updater.(record) else record end end) - set_and_reply(state, state) + {:reply, new_state, new_state} end - defcall pop(), state: state do + @impl true + def handle_call(:pop, _from, state) do case state do - [] -> reply(state) - [head | tail] -> set_and_reply(tail, head) + [] -> {:reply, state, state} + [head | tail] -> {:reply, head, tail} end end end @@ -40,12 +82,36 @@ defmodule ExVCR.Actor do Stores option parameters for the recorder. """ - use ExActor.GenServer + use GenServer - defstart start(arg), do: initial_state(arg) + def start(arg) do + GenServer.start(__MODULE__, arg) + end - defcast set(x), do: new_state(x) - defcall get, state: state, do: reply(state) + def set(pid, x) do + GenServer.cast(pid, {:set, x}) + end + + def get(pid) do + GenServer.call(pid, :get) + end + + # Callbacks + + @impl true + def init(arg) do + {:ok, arg} + end + + @impl true + def handle_cast({:set, x}, _state) do + {:noreply, x} + end + + @impl true + def handle_call(:get, _from, state) do + {:reply, state, state} + end end defmodule CurrentRecorder do @@ -53,13 +119,37 @@ defmodule ExVCR.Actor do Stores current recorder to be able to fetch it inside of the mocked version of the adapter. """ - use ExActor.GenServer, export: __MODULE__ + use GenServer + + def start_link(_) do + GenServer.start_link(__MODULE__, default_state(), name: __MODULE__) + end - defstart(start_link(_), do: default_state() |> initial_state()) + def set(x) do + GenServer.cast(__MODULE__, {:set, x}) + end - defcast(set(x), do: new_state(x)) - defcall(get, state: state, do: reply(state)) + def get do + GenServer.call(__MODULE__, :get) + end def default_state(), do: nil + + # Callbacks + + @impl true + def init(state) do + {:ok, state} + end + + @impl true + def handle_cast({:set, x}, _state) do + {:noreply, x} + end + + @impl true + def handle_call(:get, _from, state) do + {:reply, state, state} + end end end diff --git a/lib/exvcr/checker.ex b/lib/exvcr/checker.ex index 8539fe0..9c8a879 100644 --- a/lib/exvcr/checker.ex +++ b/lib/exvcr/checker.ex @@ -4,13 +4,23 @@ defmodule ExVCR.Checker do It's for [mix vcr.check] task. """ - use ExActor.GenServer, export: :singleton + use GenServer - defstart start(arg), do: initial_state(arg) + def start(arg) do + GenServer.start(__MODULE__, arg, name: :singleton) + end + + def get do + GenServer.call(:singleton, :get) + end + + def set(x) do + GenServer.cast(:singleton, {:set, x}) + end - defcall get, state: state, do: reply(state) - defcast set(x), do: new_state(x) - defcast append(x), state: state, do: new_state(%{state | files: [x|state.files]}) + def append(x) do + GenServer.cast(:singleton, {:append, x}) + end @doc """ Increment the counter for cache cassettes hit. @@ -26,4 +36,26 @@ defmodule ExVCR.Checker do ExVCR.Checker.append({type, ExVCR.Recorder.get_file_path(recorder)}) end end + + # Callbacks + + @impl true + def init(arg) do + {:ok, arg} + end + + @impl true + def handle_call(:get, _from, state) do + {:reply, state, state} + end + + @impl true + def handle_cast({:set, x}, _state) do + {:noreply, x} + end + + @impl true + def handle_cast({:append, x}, state) do + {:noreply, %{state | files: [x|state.files]}} + end end diff --git a/lib/exvcr/mock_lock.ex b/lib/exvcr/mock_lock.ex index ce9e67f..4d75e06 100644 --- a/lib/exvcr/mock_lock.ex +++ b/lib/exvcr/mock_lock.ex @@ -1,9 +1,9 @@ defmodule ExVCR.MockLock do - use ExActor.GenServer, export: :mock_lock + use GenServer @ten_milliseconds 10 - defstart start() do - initial_state(%{lock_holder: nil}) + def start() do + GenServer.start(__MODULE__, %{lock_holder: nil}, name: :mock_lock) end def ensure_started do @@ -12,38 +12,50 @@ defmodule ExVCR.MockLock do end end - defcast request_lock(caller_pid, test_pid) do + def request_lock(caller_pid, test_pid) do + GenServer.cast(:mock_lock, {:request_lock, caller_pid, test_pid}) + end + + def release_lock() do + GenServer.call(:mock_lock, :release_lock) + end + + # Callbacks + + @impl true + def init(state) do + {:ok, state} + end + + @impl true + def handle_cast({:request_lock, caller_pid, test_pid}, state) do Process.send(self(), {:do_request_lock, caller_pid, test_pid}, []) - noreply() + {:noreply, state} end - defhandleinfo {:do_request_lock, caller_pid, test_pid}, state: state do + @impl true + def handle_info({:do_request_lock, caller_pid, test_pid}, state) do if Map.get(state, :lock_holder) do Process.send_after(self(), {:do_request_lock, caller_pid, test_pid}, @ten_milliseconds) - noreply() + {:noreply, state} else Process.monitor(test_pid) Process.send(caller_pid, :lock_granted, []) - - state - |> Map.put(:lock_holder, caller_pid) - |> new_state + {:noreply, Map.put(state, :lock_holder, caller_pid)} end end - defhandleinfo {:DOWN, _ref, :process, pid, _}, state: state do + @impl true + def handle_info({:DOWN, _ref, :process, pid, _}, state) do if state.lock_holder == pid do - state - |> Map.put(:lock_holder, nil) - |> new_state + {:noreply, Map.put(state, :lock_holder, nil)} else - noreply() + {:noreply, state} end end - defcall release_lock(), state: state do - state - |> Map.put(:lock_holder, nil) - |> set_and_reply(:ok) + @impl true + def handle_call(:release_lock, _from, state) do + {:reply, :ok, Map.put(state, :lock_holder, nil)} end end diff --git a/mix.exs b/mix.exs index ddf5136..b48fad4 100644 --- a/mix.exs +++ b/mix.exs @@ -20,13 +20,12 @@ defmodule ExVCR.Mixfile do end def application do - [applications: [:meck, :exactor, :exjsx], mod: {ExVCR.Application, []}] + [applications: [:meck, :exjsx], mod: {ExVCR.Application, []}] end def deps do [ {:meck, "~> 0.8"}, - {:exactor, "~> 2.2"}, {:exjsx, "~> 4.0"}, {:ibrowse, "4.4.0", optional: true}, {:httpotion, "~> 3.1", optional: true},