Skip to content

Commit

Permalink
update db_handler test
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 committed Oct 25, 2023
1 parent 8cec022 commit 0663fa0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ defmodule Supavisor.ClientHandler do
end

def handle_event(_, {proto, _, bin}, :busy, data) when proto in [:tcp, :ssl] do
case Db.call(data.db_pid, bin, self()) do
case Db.call(data.db_pid, self(), bin) do
:ok ->
Logger.debug("DB call success")
:keep_state_and_data
Expand Down
10 changes: 5 additions & 5 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ defmodule Supavisor.DbHandler do
:gen_statem.start_link(__MODULE__, config, hibernate_after: 5_000)
end

@spec call(pid(), binary(), pid()) :: :ok | {:error, any()} | {:buffering, non_neg_integer()}
def call(pid, msg, caller) do
:gen_statem.call(pid, {:db_call, msg, caller}, 15_000)
@spec call(pid(), pid(), binary()) :: :ok | {:error, any()} | {:buffering, non_neg_integer()}
def call(pid, caller, msg) do
:gen_statem.call(pid, {:db_call, caller, msg}, 15_000)
end

@impl true
Expand Down Expand Up @@ -254,12 +254,12 @@ defmodule Supavisor.DbHandler do
end
end

def handle_event({:call, from}, {:db_call, bin, caller}, :idle, %{sock: sock} = data) do
def handle_event({:call, from}, {:db_call, caller, bin}, :idle, %{sock: sock} = data) do
reply = {:reply, from, sock_send(sock, bin)}
{:keep_state, %{data | caller: caller}, reply}
end

def handle_event({:call, from}, {:db_call, bin, caller}, state, %{buffer: buff} = data) do
def handle_event({:call, from}, {:db_call, caller, bin}, state, %{buffer: buff} = data) do
Logger.debug(
"state #{state} <-- <-- bin #{inspect(byte_size(bin))} bytes, caller: #{inspect(caller)}"
)
Expand Down
6 changes: 3 additions & 3 deletions test/supavisor/db_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ defmodule Supavisor.DbHandlerTest do
data = %{sock: {:gen_tcp, sock}, caller: nil, buffer: []}
from = {self(), :test_ref}
event = {:call, from}
payload = {:db_call, "test_data"}
payload = {:db_call, self(), "test_data"}

{:keep_state, new_data, reply} = Db.handle_event(event, payload, :idle, data)

Expand All @@ -107,10 +107,10 @@ defmodule Supavisor.DbHandlerTest do
end

test "handle_event/4 with non-idle state" do
data = %{sock: nil, caller: nil, buffer: []}
data = %{sock: nil, caller: self(), buffer: []}
from = {self(), :test_ref}
event = {:call, from}
payload = {:db_call, "test_data"}
payload = {:db_call, self(), "test_data"}
state = :non_idle

{:keep_state, new_data, reply} = Db.handle_event(event, payload, state, data)
Expand Down

0 comments on commit 0663fa0

Please sign in to comment.