Skip to content
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

Bugfix: multiple pushes per client for subscriptions that have a context_id #1249

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/absinthe/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ defmodule Absinthe.Subscription do
pubsub
|> registry_name
|> Registry.lookup(key)
|> Enum.uniq_by(fn {_, {doc_id, _doc}} -> doc_id end)
|> Enum.map(fn match ->
{_, {doc_id, doc}} = match
beligante marked this conversation as resolved.
Show resolved Hide resolved
doc = Map.update!(doc, :initial_phases, &PipelineSerializer.unpack/1)
Expand Down
39 changes: 38 additions & 1 deletion test/absinthe/execution/subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule Absinthe.Execution.SubscriptionTest do
@behaviour Absinthe.Subscription.Pubsub

def start_link() do
Registry.start_link(keys: :unique, name: __MODULE__)
Registry.start_link(keys: :duplicate, name: __MODULE__)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to change this to allow duplicated keys to make the test fail and also because the registry in the sub supervisor allows duplicated keys: https://github.com/absinthe-graphql/absinthe/blob/master/lib/absinthe/subscription/supervisor.ex#L37

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, good call.

end

def node_name() do
Expand Down Expand Up @@ -678,6 +678,43 @@ defmodule Absinthe.Execution.SubscriptionTest do
:telemetry.detach(context.test)
end

@query """
subscription {
otherUser { id }
}
"""
test "de-duplicates pushes to the same context" do
documents =
Enum.map(1..5, fn _index ->
{:ok, doc} = run_subscription(@query, Schema, context: %{context_id: "global"})
doc
end)

# assert that all documents are the same
assert [document] = Enum.dedup(documents)

Absinthe.Subscription.publish(
PubSub,
%{id: "global_user_id"},
other_user: "*"
)

topic_id = document["subscribed"]

for _i <- 1..5 do
assert_receive(
{:broadcast,
%{
event: "subscription:data",
result: %{data: %{"otherUser" => %{"id" => "global_user_id"}}},
topic: ^topic_id
}}
)
end

refute_receive({:broadcast, _})
end

defp run_subscription(query, schema, opts \\ []) do
opts = Keyword.update(opts, :context, %{pubsub: PubSub}, &Map.put(&1, :pubsub, PubSub))

Expand Down