From 92f26cb7e6c3f1e18acc207d73a0585ba3c3a576 Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Mon, 9 Dec 2024 23:27:57 +0100 Subject: [PATCH 1/2] remove_exactor --- lib/exvcr/actor.ex | 128 +++++++++++++++++++++++++++++++++++------ lib/exvcr/checker.ex | 42 ++++++++++++-- lib/exvcr/mock_lock.ex | 52 ++++++++++------- mix.exs | 3 +- 4 files changed, 179 insertions(+), 46 deletions(-) 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}, From 7179adab1281342ccceff27f694623fabe6ac380 Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Sun, 15 Dec 2024 13:23:53 +0100 Subject: [PATCH 2/2] remove from mix lock --- mix.lock | 1 - 1 file changed, 1 deletion(-) diff --git a/mix.lock b/mix.lock index 64d5e9c..a91e6e3 100644 --- a/mix.lock +++ b/mix.lock @@ -6,7 +6,6 @@ "earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"}, "earmark_parser": {:hex, :earmark_parser, "1.4.19", "de0d033d5ff9fc396a24eadc2fcf2afa3d120841eb3f1004d138cbf9273210e8", [:mix], [], "hexpm", "527ab6630b5c75c3a3960b75844c314ec305c76d9899bb30f71cb85952a9dc45"}, "ex_doc": {:hex, :ex_doc, "0.28.0", "7eaf526dd8c80ae8c04d52ac8801594426ae322b52a6156cd038f30bafa8226f", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "e55cdadf69a5d1f4cfd8477122ebac5e1fadd433a8c1022dafc5025e48db0131"}, - "exactor": {:hex, :exactor, "2.2.4", "5efb4ddeb2c48d9a1d7c9b465a6fffdd82300eb9618ece5d34c3334d5d7245b1", [:mix], [], "hexpm", "1222419f706e01bfa1095aec9acf6421367dcfab798a6f67c54cf784733cd6b5"}, "excoveralls": {:hex, :excoveralls, "0.18.0", "b92497e69465dc51bc37a6422226ee690ab437e4c06877e836f1c18daeb35da9", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "1109bb911f3cb583401760be49c02cbbd16aed66ea9509fc5479335d284da60b"}, "exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm", "32e95820a97cffea67830e91514a2ad53b888850442d6d395f53a1ac60c82e07"}, "finch": {:hex, :finch, "0.16.0", "40733f02c89f94a112518071c0a91fe86069560f5dbdb39f9150042f44dcfb1a", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f660174c4d519e5fec629016054d60edd822cdfe2b7270836739ac2f97735ec5"},