Skip to content

Commit 88e3407

Browse files
committed
fix: trap exits in iterator server
1 parent eb43078 commit 88e3407

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

lib/graphql_ws_client/iterator.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ defmodule GraphQLWSClient.Iterator do
6262

6363
@impl true
6464
def init(%Opts{} = opts) do
65+
Process.flag(:trap_exit, true)
66+
6567
case GraphQLWSClient.subscribe(opts.client, opts.query, opts.variables) do
6668
{:ok, subscription_id} ->
67-
monitor_ref = Process.monitor(opts.client)
68-
6969
{:ok,
7070
%State{
7171
buffer_size: opts.buffer_size,
7272
client: opts.client,
73-
monitor_ref: monitor_ref,
73+
monitor_ref: Process.monitor(opts.client),
7474
subscription_id: subscription_id
7575
}}
7676

@@ -81,7 +81,9 @@ defmodule GraphQLWSClient.Iterator do
8181

8282
@impl true
8383
def terminate(_reason, %State{} = state) do
84-
Process.demonitor(state.monitor_ref)
84+
if state.monitor_ref do
85+
Process.demonitor(state.monitor_ref, [:flush])
86+
end
8587

8688
if state.subscription_id do
8789
GraphQLWSClient.unsubscribe(state.client, state.subscription_id)

test/graphql_ws_client/iterator_test.exs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule GraphQLWSClient.IteratorTest do
99
alias GraphQLWSClient.Iterator
1010
alias GraphQLWSClient.Iterator.Opts
1111
alias GraphQLWSClient.Message
12+
alias GraphQLWSClient.SocketError
1213

1314
setup :set_mox_from_context
1415
setup :verify_on_exit!
@@ -21,19 +22,18 @@ defmodule GraphQLWSClient.IteratorTest do
2122
@payload_2 %{"baz" => 23}
2223

2324
setup do
24-
test_pid = self()
25-
26-
expect(MockDriver, :connect, fn @conn ->
27-
send(test_pid, :connected)
28-
{:ok, @conn}
29-
end)
30-
31-
client = start_supervised!({GraphQLWSClient, @config}, id: :test_client)
32-
{:ok, client: client, test_pid: test_pid}
25+
{:ok, test_pid: self()}
3326
end
3427

3528
describe "open!/4" do
36-
test "success", %{client: client, test_pid: test_pid} do
29+
test "success", %{test_pid: test_pid} do
30+
expect(MockDriver, :connect, fn @conn ->
31+
send(test_pid, :connected)
32+
{:ok, @conn}
33+
end)
34+
35+
client = start_supervised!({GraphQLWSClient, @config}, id: :test_client)
36+
3737
expect(MockDriver, :push_message, fn @conn,
3838
%Message{
3939
type: :subscribe,
@@ -51,15 +51,29 @@ defmodule GraphQLWSClient.IteratorTest do
5151
assert_receive :subscribed
5252
end
5353

54-
test "non-numeric buffer size", %{client: client} do
54+
test "non-numeric buffer size", %{test_pid: test_pid} do
55+
expect(MockDriver, :connect, fn @conn ->
56+
send(test_pid, :connected)
57+
{:ok, @conn}
58+
end)
59+
60+
client = start_supervised!({GraphQLWSClient, @config}, id: :test_client)
61+
5562
assert_receive :connected
5663

5764
assert_raise ArgumentError, "invalid buffer size", fn ->
5865
Iterator.open!(client, @query, @variables, buffer_size: "__invalid__")
5966
end
6067
end
6168

62-
test "negative buffer size", %{client: client} do
69+
test "negative buffer size", %{test_pid: test_pid} do
70+
expect(MockDriver, :connect, fn @conn ->
71+
send(test_pid, :connected)
72+
{:ok, @conn}
73+
end)
74+
75+
client = start_supervised!({GraphQLWSClient, @config}, id: :test_client)
76+
6377
assert_receive :connected
6478

6579
assert_raise ArgumentError, "invalid buffer size", fn ->
@@ -69,8 +83,16 @@ defmodule GraphQLWSClient.IteratorTest do
6983
end
7084

7185
describe "next/1" do
72-
setup %{client: client} do
86+
setup %{test_pid: test_pid} do
87+
expect(MockDriver, :connect, fn @conn ->
88+
send(test_pid, :connected)
89+
{:ok, @conn}
90+
end)
91+
92+
client = start_supervised!({GraphQLWSClient, @config}, id: :test_client)
93+
7394
{:ok,
95+
client: client,
7496
opts: Opts.new(client: client, query: @query, variables: @variables)}
7597
end
7698

0 commit comments

Comments
 (0)