Skip to content

Commit

Permalink
Merge pull request #1336 from youroff/main
Browse files Browse the repository at this point in the history
Unregsiter duplicate (listening to the same topic) subscriptions individually
  • Loading branch information
benwilson512 authored Aug 3, 2024
2 parents 5705d9a + 68f7a7d commit 5daf8d7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/absinthe/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ defmodule Absinthe.Subscription do
registry = pubsub |> registry_name

for field_key <- pdict_fields(doc_id) do
Registry.unregister(registry, field_key)
Registry.unregister_match(registry, field_key, doc_id)
end

Registry.unregister(registry, doc_id)
Expand Down
33 changes: 33 additions & 0 deletions test/absinthe/execution/subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,39 @@ defmodule Absinthe.Execution.SubscriptionTest do
refute_receive({:broadcast, _})
end

test "can unsubscribe from duplicate subscriptions individually" do
client_id = "abc"

assert {:ok, %{"subscribed" => topic1}} =
run_subscription(
@query,
Schema,
variables: %{"clientId" => client_id},
context: %{pubsub: PubSub}
)

assert {:ok, %{"subscribed" => topic2}} =
run_subscription(
@query,
Schema,
variables: %{"clientId" => client_id},
context: %{pubsub: PubSub}
)

Absinthe.Subscription.publish(PubSub, "foo", thing: client_id)
assert_receive({:broadcast, a})
assert_receive({:broadcast, b})
doc_ids = Enum.map([a, b], &(&1.topic))
assert topic1 in doc_ids
assert topic2 in doc_ids

Absinthe.Subscription.unsubscribe(PubSub, topic1)
Absinthe.Subscription.publish(PubSub, "bar", thing: client_id)
assert_receive({:broadcast, a})
refute_receive({:broadcast, _})
assert topic2 == a.topic
end

@query """
subscription {
multipleTopics
Expand Down

0 comments on commit 5daf8d7

Please sign in to comment.