From 4949b78ffe0a71689e51e0646caffdfa90f05b57 Mon Sep 17 00:00:00 2001 From: ruslandoga <67764432+ruslandoga@users.noreply.github.com> Date: Thu, 19 Dec 2024 12:28:37 +0700 Subject: [PATCH] Revert "Proxy info messages to the adapter (#316)" This reverts commit 5d088f7262b43e442c13c9f880c8fff7dfe92277. --- .formatter.exs | 2 +- examples/tcp_connection/lib/tcp_connection.ex | 17 --- integration_test/cases/info_test.exs | 100 ------------------ lib/db_connection/connection.ex | 20 +--- test/test_support.exs | 4 - 5 files changed, 6 insertions(+), 137 deletions(-) delete mode 100644 integration_test/cases/info_test.exs diff --git a/.formatter.exs b/.formatter.exs index ab786a03..be68757e 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -1,4 +1,4 @@ # Used by "mix format" [ - inputs: ["{mix,.formatter}.exs", "{config,lib,test,examples,integration_test}/**/*.{ex,exs}"] + inputs: ["{mix,.formatter}.exs", "{config,lib,test,integration_test}/**/*.{ex,exs}"] ] diff --git a/examples/tcp_connection/lib/tcp_connection.ex b/examples/tcp_connection/lib/tcp_connection.ex index ebc1b7b3..efb68352 100644 --- a/examples/tcp_connection/lib/tcp_connection.ex +++ b/examples/tcp_connection/lib/tcp_connection.ex @@ -64,8 +64,6 @@ defmodule TCPConnection do case :gen_tcp.connect(host, port, socket_opts, timeout) do {:ok, sock} -> - # Monitor the socket so we can react to it being closed. See handle_info/2. - _ref = :inet.monitor(sock) {:ok, {sock, <<>>}} {:error, reason} -> @@ -145,21 +143,6 @@ defmodule TCPConnection do end end - # The handle_info callback is optional and can be removed if not needed. - # Here it is used to react to `:inet.monitor/1` messages which arrive - # when socket gets closed while the connection is idle. - def handle_info({:DOWN, _ref, _type, sock, _info}, {sock, _buffer}) do - {:disconnect, TCPConnection.Error.exception({:idle, :closed})} - end - - def handle_info(msg, state) do - Logger.info(fn -> - ["#{__MODULE__} (", inspect(self()), ") missed message: ", inspect(msg)] - end) - - :ok - end - @impl true def handle_close(_, _, s) do {:ok, nil, s} diff --git a/integration_test/cases/info_test.exs b/integration_test/cases/info_test.exs deleted file mode 100644 index 7d187c86..00000000 --- a/integration_test/cases/info_test.exs +++ /dev/null @@ -1,100 +0,0 @@ -defmodule InfoTest do - use ExUnit.Case, async: true - - alias TestPool, as: P - alias TestAgent, as: A - alias TestQuery, as: Q - - test "handle_info handles harmless message and moves on" do - stack = [ - fn opts -> - send(opts[:parent], {:connected, self()}) - {:ok, :state} - end, - :ok, - {:idle, :state}, - {:idle, :state} - ] - - {:ok, agent} = A.start_link(stack) - {:ok, pool} = P.start_link(agent: agent, parent: self()) - - assert_receive {:connected, conn} - send(conn, "some harmless message") - assert P.run(pool, fn _ -> :result end) == :result - - assert [ - connect: _, - handle_info: _, - handle_status: _, - handle_status: _ - ] = A.record(agent) - end - - test "handle_info can force disconnect" do - stack = [ - fn opts -> - send(opts[:parent], {:connected, self()}) - {:ok, :state} - end, - {:disconnect, RuntimeError.exception("TCP connection just closed")}, - :ok, - fn opts -> - send(opts[:parent], :reconnected) - {:ok, :state} - end - ] - - {:ok, agent} = A.start_link(stack) - P.start_link(agent: agent, parent: self()) - - assert_receive {:connected, conn} - send(conn, "monitor says TCP connection just closed") - assert_receive :reconnected - - assert [ - connect: _, - handle_info: _, - disconnect: _, - connect: _ - ] = A.record(agent) - end - - test "handle_info's disconnect while checked out client crashes is no-op" do - stack = [ - fn _opts -> - {:ok, %{conn_pid: self()}} - end, - fn _query, _params, _opts, %{conn_pid: conn_pid} -> - send(conn_pid, "monitor says TCP connection just closed") - - # This waits for the info message to be processed. - :sys.get_state(conn_pid) - - {:disconnect, RuntimeError.exception("TCP connection is closed"), :new_state} - end, - {:disconnect, RuntimeError.exception("TCP connection just closed")}, - :ok, - fn opts -> - send(opts[:parent], :reconnected) - {:ok, :state} - end - ] - - {:ok, agent} = A.start_link(stack) - {:ok, pool} = P.start_link(agent: agent, parent: self()) - - assert {:error, %RuntimeError{message: "TCP connection is closed"}} = - P.execute(pool, %Q{}, []) - - assert_receive :reconnected - - assert [ - connect: _, - handle_execute: _, - handle_info: _, - disconnect: _, - connect: _ - ] = A.record(agent) - end -end diff --git a/lib/db_connection/connection.ex b/lib/db_connection/connection.ex index b1c120f3..ae5f9115 100644 --- a/lib/db_connection/connection.ex +++ b/lib/db_connection/connection.ex @@ -330,22 +330,12 @@ defmodule DBConnection.Connection do handle_timeout(s) end - def handle_event(:info, msg, :no_state, %{mod: mod, state: state} = s) do - if function_exported?(mod, :handle_info, 2) do - case apply(mod, :handle_info, [msg, state]) do - :ok -> - handle_timeout(s) - - {:disconnect, err} -> - {:keep_state, s, {:next_event, :internal, {:disconnect, {:log, err}}}} - end - else - Logger.info(fn -> - [inspect(mod), ?\s, ?(, inspect(self()), ") missed message: " | inspect(msg)] - end) + def handle_event(:info, msg, :no_state, %{mod: mod} = s) do + Logger.info(fn -> + [inspect(mod), ?\s, ?(, inspect(self()), ") missed message: " | inspect(msg)] + end) - handle_timeout(s) - end + handle_timeout(s) end @doc false diff --git a/test/test_support.exs b/test/test_support.exs index a63818d1..dcbe445f 100644 --- a/test/test_support.exs +++ b/test/test_support.exs @@ -150,10 +150,6 @@ defmodule TestConnection do TestAgent.eval(:handle_deallocate, [query, cursor, opts, state]) end - def handle_info(message, state) do - TestAgent.eval(:handle_info, [message, state]) - end - defp put_agent_from_opts(opts) do Process.get(:agent) || Process.put(:agent, agent_from_opts(opts)) end