-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proxy info messages to the adapter #316
Changes from 4 commits
da511ac
00c6958
ba7677a
9fe84fc
595439a
a3ece3c
a5cd7d7
f4e74de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test,integration_test}/**/*.{ex,exs}"] | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test,examples,integration_test}/**/*.{ex,exs}"] | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
defmodule InfoTest do | ||
use ExUnit.Case, async: true | ||
alias TestPool, as: P | ||
alias TestAgent, as: A | ||
|
||
test "handle_info handles 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using this just to "sync" with the connection process, i.e. wait until it has processed all the messages. Maybe there is a better way? I know some people use |
||
|
||
assert [ | ||
connect: _, | ||
handle_info: _, | ||
handle_status: _, | ||
handle_status: _ | ||
] = A.record(agent) | ||
end | ||
|
||
test "handle_info can force disconnect" do | ||
ruslandoga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stack = [ | ||
fn opts -> | ||
send(opts[:parent], {:connected, self()}) | ||
{:ok, :state} | ||
end, | ||
{:disconnect, :reason}, | ||
: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, "some harmful message") | ||
assert_receive :reconnected | ||
|
||
assert [ | ||
connect: _, | ||
handle_info: _, | ||
disconnect: _, | ||
connect: _ | ||
] = A.record(agent) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -330,12 +330,22 @@ defmodule DBConnection.Connection do | |
handle_timeout(s) | ||
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) | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still passing the state to the callback since the monitor ref / socket ref would be there, and they are probably needed to match on the incoming message. |
||
: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) | ||
|
||
handle_timeout(s) | ||
handle_timeout(s) | ||
end | ||
end | ||
|
||
@doc false | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like here.