From 68f7a7d96d94070d45975380031ba90043385361 Mon Sep 17 00:00:00 2001 From: Ivan Yurov Date: Fri, 2 Aug 2024 21:35:37 -0700 Subject: [PATCH] fixes #1335 --- lib/absinthe/subscription.ex | 2 +- test/absinthe/execution/subscription_test.exs | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/absinthe/subscription.ex b/lib/absinthe/subscription.ex index e6bddec21c..e4386019a8 100644 --- a/lib/absinthe/subscription.ex +++ b/lib/absinthe/subscription.ex @@ -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) diff --git a/test/absinthe/execution/subscription_test.exs b/test/absinthe/execution/subscription_test.exs index 34aa543517..14416c4afe 100644 --- a/test/absinthe/execution/subscription_test.exs +++ b/test/absinthe/execution/subscription_test.exs @@ -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