Skip to content

Commit

Permalink
remove_exactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuku committed Dec 9, 2024
1 parent 9cf5dfa commit 92f26cb
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 46 deletions.
128 changes: 109 additions & 19 deletions lib/exvcr/actor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,26 +82,74 @@ 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
@moduledoc """
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
42 changes: 37 additions & 5 deletions lib/exvcr/checker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
52 changes: 32 additions & 20 deletions lib/exvcr/mock_lock.ex
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 92f26cb

Please sign in to comment.