-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove using Marshal, correct serialized objects
- Loading branch information
1 parent
f08db8e
commit 646d680
Showing
6 changed files
with
164 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_job" | ||
|
||
module GraphQL | ||
module Adapters | ||
class BaseJob < ActiveJob::Base | ||
DEFAULT_QUEUE_NAME = :default | ||
|
||
queue_as { GraphQL::AnyCable.config.async_broadcasting["queue"] || DEFAULT_QUEUE_NAME } | ||
|
||
def perform(serialized_payload, execute_method, event_name, args = {}, object = nil, options = {}) | ||
schema = schema_parse(serialized_payload) | ||
|
||
schema.public_send(execute_method, event_name, args, object, **options) | ||
end | ||
|
||
private | ||
|
||
def schema_parse(serialized_payload) | ||
payload = JSON.parse(serialized_payload) | ||
|
||
payload["schema"] = payload["schema"].constantize | ||
payload["serializer"] = payload["serializer"].constantize | ||
|
||
GraphQL::Subscriptions::AnyCableSubscriptions.new(**payload.transform_keys(&:to_sym)) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_job" | ||
|
||
RSpec.describe GraphQL::Adapters::BaseJob, type: :job do | ||
ActiveJob::Base.queue_adapter = :inline | ||
|
||
subject(:job) { described_class.perform_later(*job_payload) } | ||
subject(:broadcast_changes) { AnycableSchema.subscriptions.trigger(*trigger_sync_arguments) } | ||
|
||
before do | ||
AnycableSchema.execute( | ||
query: query, | ||
context: { channel: channel, subscription_id: subscription_id }, | ||
variables: {}, | ||
operation_name: "SomeSubscription", | ||
) | ||
end | ||
|
||
let(:trigger_sync_arguments) do | ||
[ | ||
:product_updated, | ||
{}, | ||
{id: 1, title: "foo"} | ||
] | ||
end | ||
|
||
let(:job_payload) do | ||
[ | ||
"{\"schema\":\"AnycableSchema\",\"serializer\":\"GraphQL::Subscriptions::Serialize\"}", | ||
"trigger_sync", | ||
*trigger_sync_arguments | ||
] | ||
end | ||
|
||
let(:query) do | ||
<<~GRAPHQL | ||
subscription SomeSubscription { productUpdated { id } } | ||
GRAPHQL | ||
end | ||
|
||
let(:channel) do | ||
socket = double("Socket", istate: AnyCable::Socket::State.new({})) | ||
connection = double("Connection", anycable_socket: socket) | ||
double("Channel", id: "legacy_id", params: { "channelId" => "legacy_id" }, stream_from: nil, connection: connection) | ||
end | ||
|
||
let(:subscription_id) do | ||
"some-truly-random-number" | ||
end | ||
|
||
context "when config.use_async_broadcasting is true" do | ||
around do |ex| | ||
GraphQL::AnyCable.config.use_async_broadcasting = true | ||
ex.run | ||
GraphQL::AnyCable.config.use_async_broadcasting = false | ||
end | ||
|
||
it "executes AnyCableSubscriptions" do | ||
ActiveJob::Base.queue_adapter = :inline | ||
|
||
expect_any_instance_of(GraphQL::Subscriptions::AnyCableSubscriptions) | ||
.to receive(:trigger_sync).with(*trigger_sync_arguments) | ||
|
||
expect_any_instance_of(GraphQL::Adapters::BaseJob).to receive(:perform).and_call_original | ||
|
||
broadcast_changes | ||
end | ||
|
||
it "adds BaseJob to enqueued_jobs" do | ||
ActiveJob::Base.queue_adapter = :test | ||
|
||
expect { broadcast_changes }.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(1) | ||
end | ||
end | ||
|
||
context "when config.use_async_broadcasting is false" do | ||
before do | ||
GraphQL::AnyCable.config.use_async_broadcasting = false | ||
end | ||
|
||
it "executes AnyCableSubscriptions" do | ||
ActiveJob::Base.queue_adapter = :inline | ||
|
||
expect_any_instance_of(GraphQL::Subscriptions::AnyCableSubscriptions) | ||
.to receive(:trigger_sync).with(*trigger_sync_arguments) | ||
|
||
expect_any_instance_of(GraphQL::Adapters::BaseJob).to_not receive(:perform) | ||
|
||
broadcast_changes | ||
end | ||
|
||
it "does not add BaseJob to enqueued_jobs" do | ||
ActiveJob::Base.queue_adapter = :test | ||
|
||
expect { broadcast_changes }.to change(ActiveJob::Base.queue_adapter.enqueued_jobs, :size).by(0) | ||
end | ||
end | ||
end | ||
|