-
Notifications
You must be signed in to change notification settings - Fork 534
Ensure unsubscribing only removes a single subscription #1315
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
Closed
tomasz-tomczyk
wants to merge
5
commits into
absinthe-graphql:main
from
tomasz-tomczyk:unsubscribe-once
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
519f33f
Ensure unsubscribing only removes a single subscription
tomasz-tomczyk e64a6fa
Does not receive messages after unsubscribing
tomasz-tomczyk 55039ae
Registry doesn't grow indefinitely
tomasz-tomczyk 7b50faa
Feedback via @howleysv
tomasz-tomczyk 71cad59
Merge branch 'absinthe-graphql:main' into unsubscribe-once
tomasz-tomczyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -77,6 +77,10 @@ defmodule Absinthe.Execution.SubscriptionTest do | |||||
# this pubsub is local and doesn't support clusters | ||||||
:ok | ||||||
end | ||||||
|
||||||
def list_registry_keys() do | ||||||
Registry.keys(__MODULE__, self()) | ||||||
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. This is the registry name that is used to store the subscription entries internally
Suggested change
|
||||||
end | ||||||
end | ||||||
|
||||||
defmodule PubSubWithDocsetRunner do | ||||||
|
@@ -122,6 +126,7 @@ defmodule Absinthe.Execution.SubscriptionTest do | |||||
|
||||||
defmodule Schema do | ||||||
use Absinthe.Schema | ||||||
import_types Absinthe.Type.Custom | ||||||
|
||||||
query do | ||||||
field :foo, :string | ||||||
|
@@ -190,6 +195,19 @@ defmodule Absinthe.Execution.SubscriptionTest do | |||||
end | ||||||
end | ||||||
|
||||||
field :schedule, :string do | ||||||
arg :location_id, non_null(:id) | ||||||
arg :date, :date | ||||||
|
||||||
config fn | ||||||
args, _ -> | ||||||
{ | ||||||
:ok, | ||||||
topic: args.location_id | ||||||
} | ||||||
end | ||||||
end | ||||||
|
||||||
field :multiple_topics, :string do | ||||||
config fn _, _ -> | ||||||
{:ok, topic: ["topic_1", "topic_2", "topic_3"]} | ||||||
|
@@ -400,6 +418,128 @@ defmodule Absinthe.Execution.SubscriptionTest do | |||||
) | ||||||
end | ||||||
|
||||||
@query """ | ||||||
subscription ($locationId: ID!, $date: Date) { | ||||||
schedule(locationId: $locationId, date: $date) | ||||||
} | ||||||
""" | ||||||
test "subscribing twice and unsubscribing once keeps one subscription active" do | ||||||
location_id = "12" | ||||||
date1 = "2020-01-01" | ||||||
date2 = "2020-01-02" | ||||||
|
||||||
assert {:ok, %{"subscribed" => topic1}} = | ||||||
run_subscription( | ||||||
@query, | ||||||
Schema, | ||||||
variables: %{"locationId" => location_id, "date" => date1}, | ||||||
context: %{pubsub: PubSub} | ||||||
) | ||||||
|
||||||
assert {:ok, %{"subscribed" => topic2}} = | ||||||
run_subscription( | ||||||
@query, | ||||||
Schema, | ||||||
variables: %{"locationId" => location_id, "date" => date2}, | ||||||
context: %{pubsub: PubSub} | ||||||
) | ||||||
|
||||||
Absinthe.Subscription.unsubscribe(PubSub, topic1) | ||||||
|
||||||
Absinthe.Subscription.publish(PubSub, "foo", schedule: location_id) | ||||||
|
||||||
assert_receive({:broadcast, msg}) | ||||||
|
||||||
assert %{ | ||||||
event: "subscription:data", | ||||||
result: %{data: %{"schedule" => "foo"}}, | ||||||
topic: topic2 | ||||||
} == msg | ||||||
end | ||||||
|
||||||
@query """ | ||||||
subscription ($locationId: ID!, $date: Date) { | ||||||
schedule(locationId: $locationId, date: $date) | ||||||
} | ||||||
""" | ||||||
test "subscribing twice and unsubscribing once means new messages are only sent to one subscription" do | ||||||
location_id = "12" | ||||||
date1 = "2020-01-01" | ||||||
date2 = "2020-01-02" | ||||||
|
||||||
assert {:ok, %{"subscribed" => topic1}} = | ||||||
run_subscription( | ||||||
@query, | ||||||
Schema, | ||||||
variables: %{"locationId" => location_id, "date" => date1}, | ||||||
context: %{pubsub: PubSub} | ||||||
) | ||||||
|
||||||
assert {:ok, %{"subscribed" => topic2}} = | ||||||
run_subscription( | ||||||
@query, | ||||||
Schema, | ||||||
variables: %{"locationId" => location_id, "date" => date2}, | ||||||
context: %{pubsub: PubSub} | ||||||
) | ||||||
|
||||||
Absinthe.Subscription.unsubscribe(PubSub, topic1) | ||||||
|
||||||
Absinthe.Subscription.publish(PubSub, "foo", schedule: location_id) | ||||||
|
||||||
assert_receive({:broadcast, msg}) | ||||||
|
||||||
assert %{ | ||||||
event: "subscription:data", | ||||||
result: %{data: %{"schedule" => "foo"}}, | ||||||
topic: topic2 | ||||||
} == msg | ||||||
|
||||||
refute_receive({:broadcast, _}) | ||||||
end | ||||||
|
||||||
@query """ | ||||||
subscription ($clientId: ID!) { | ||||||
thing(clientId: $clientId) | ||||||
} | ||||||
""" | ||||||
test "repeatedly subscribing and unsubscribing on the same topic doesn't grow registry indefinitely" do | ||||||
client_id = "abc" | ||||||
|
||||||
for _i <- 1..3 do | ||||||
assert {:ok, %{"subscribed" => topic}} = | ||||||
run_subscription(@query, Schema, | ||||||
variables: %{"clientId" => client_id}, | ||||||
context: %{pubsub: PubSub} | ||||||
) | ||||||
|
||||||
Absinthe.Subscription.unsubscribe(PubSub, topic) | ||||||
end | ||||||
|
||||||
assert [] == PubSub.list_registry_keys() | ||||||
end | ||||||
|
||||||
@query """ | ||||||
subscription ($clientId: ID!) { | ||||||
thing(clientId: $clientId) | ||||||
} | ||||||
""" | ||||||
test "repeatedly subscribing and unsubscribing on multiple topics doesn't grow registry indefinitely" do | ||||||
client_id = "abc" | ||||||
|
||||||
for i <- 1..3 do | ||||||
assert {:ok, %{"subscribed" => topic}} = | ||||||
run_subscription(@query, Schema, | ||||||
variables: %{"clientId" => "#{client_id}#{i}"}, | ||||||
context: %{pubsub: PubSub} | ||||||
) | ||||||
|
||||||
Absinthe.Subscription.unsubscribe(PubSub, topic) | ||||||
end | ||||||
|
||||||
assert [] == PubSub.list_registry_keys() | ||||||
end | ||||||
|
||||||
@query """ | ||||||
subscription ($userId: ID!) { | ||||||
user(id: $userId) { id name } | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We still want to clean up the record in the registry, but this only removes the entry associated with the
doc_id
and not all subscriptions with the samefield_key