Skip to content

Commit

Permalink
refactor: update iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
tlux committed Mar 29, 2024
1 parent c09c69b commit a5c34ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 39 deletions.
14 changes: 9 additions & 5 deletions lib/graphql_ws_client/iterator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ defmodule GraphQLWSClient.Iterator do
end

@impl true
def terminate(_reason, %State{} = state) do
if state.monitor_ref do
Process.demonitor(state.monitor_ref, [:flush])
def terminate(_reason, %State{
client: client,
monitor_ref: monitor_ref,
subscription_id: subscription_id
}) do
if monitor_ref do
Process.demonitor(monitor_ref, [:flush])
end

if state.subscription_id do
GraphQLWSClient.unsubscribe(state.client, state.subscription_id)
if subscription_id do
GraphQLWSClient.unsubscribe(client, subscription_id)
end
end

Expand Down
48 changes: 14 additions & 34 deletions test/graphql_ws_client/iterator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ defmodule GraphQLWSClient.IteratorTest do
@payload_2 %{"baz" => 23}

setup do
{:ok, test_pid: self()}
end
test_pid = self()

describe "open!/4" do
test "success", %{test_pid: test_pid} do
expect(MockDriver, :connect, fn @conn ->
send(test_pid, :connected)
{:ok, @conn}
end)
expect(MockDriver, :connect, fn @conn ->
send(test_pid, :connected)
{:ok, @conn}
end)

client = start_supervised!({GraphQLWSClient, @config}, id: :test_client)

client = start_supervised!({GraphQLWSClient, @config}, id: :test_client)
{:ok, client: client, test_pid: self()}
end

describe "open!/4" do
test "success", %{client: client, test_pid: test_pid} do
expect(MockDriver, :push_message, fn @conn,
%Message{
type: :subscribe,
Expand All @@ -50,29 +52,15 @@ defmodule GraphQLWSClient.IteratorTest do
assert_receive :subscribed
end

test "non-numeric buffer size", %{test_pid: test_pid} do
expect(MockDriver, :connect, fn @conn ->
send(test_pid, :connected)
{:ok, @conn}
end)

client = start_supervised!({GraphQLWSClient, @config}, id: :test_client)

test "non-numeric buffer size", %{client: client} do
assert_receive :connected

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

test "negative buffer size", %{test_pid: test_pid} do
expect(MockDriver, :connect, fn @conn ->
send(test_pid, :connected)
{:ok, @conn}
end)

client = start_supervised!({GraphQLWSClient, @config}, id: :test_client)

test "negative buffer size", %{client: client} do
assert_receive :connected

assert_raise ArgumentError, "invalid buffer size", fn ->
Expand All @@ -82,16 +70,8 @@ defmodule GraphQLWSClient.IteratorTest do
end

describe "next/1" do
setup %{test_pid: test_pid} do
expect(MockDriver, :connect, fn @conn ->
send(test_pid, :connected)
{:ok, @conn}
end)

client = start_supervised!({GraphQLWSClient, @config}, id: :test_client)

setup %{client: client} do
{:ok,
client: client,
opts: Opts.new(client: client, query: @query, variables: @variables)}
end

Expand Down

0 comments on commit a5c34ee

Please sign in to comment.