mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #24540 from sgrif/sg-actioncable-callbacks
Run Action Cable callbacks through the worker pool
This commit is contained in:
commit
4769fc4b4d
2 changed files with 30 additions and 5 deletions
|
@ -76,8 +76,11 @@ module ActionCable
|
||||||
# Don't send the confirmation until pubsub#subscribe is successful
|
# Don't send the confirmation until pubsub#subscribe is successful
|
||||||
defer_subscription_confirmation!
|
defer_subscription_confirmation!
|
||||||
|
|
||||||
if handler = callback || block
|
if user_handler = callback || block
|
||||||
handler = -> message { handler.(coder.decode(message)) } if coder
|
user_handler = -> message { handler.(coder.decode(message)) } if coder
|
||||||
|
handler = -> message do
|
||||||
|
connection.worker_pool.async_invoke(user_handler, :call, message)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
handler = default_stream_handler(broadcasting, coder: coder)
|
handler = default_stream_handler(broadcasting, coder: coder)
|
||||||
end
|
end
|
||||||
|
|
|
@ -116,11 +116,22 @@ module ActionCable::StreamTests
|
||||||
|
|
||||||
require 'action_cable/subscription_adapter/inline'
|
require 'action_cable/subscription_adapter/inline'
|
||||||
|
|
||||||
|
class UserCallbackChannel < ActionCable::Channel::Base
|
||||||
|
def subscribed
|
||||||
|
stream_from :channel do
|
||||||
|
Thread.current[:ran_callback] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class StreamEncodingTest < ActionCable::TestCase
|
class StreamEncodingTest < ActionCable::TestCase
|
||||||
setup do
|
setup do
|
||||||
@server = TestServer.new(subscription_adapter: ActionCable::SubscriptionAdapter::Inline)
|
@server = TestServer.new(subscription_adapter: ActionCable::SubscriptionAdapter::Inline)
|
||||||
@server.config.allowed_request_origins = %w( http://rubyonrails.com )
|
@server.config.allowed_request_origins = %w( http://rubyonrails.com )
|
||||||
@server.stubs(:channel_classes).returns(ChatChannel.name => ChatChannel)
|
@server.stubs(:channel_classes).returns(
|
||||||
|
ChatChannel.name => ChatChannel,
|
||||||
|
UserCallbackChannel.name => UserCallbackChannel,
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'custom encoder' do
|
test 'custom encoder' do
|
||||||
|
@ -134,6 +145,17 @@ module ActionCable::StreamTests
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "user supplied callbacks are run through the worker pool" do
|
||||||
|
run_in_eventmachine do
|
||||||
|
connection = open_connection
|
||||||
|
receive(connection, command: 'subscribe', channel: UserCallbackChannel.name, identifiers: { id: 1 })
|
||||||
|
|
||||||
|
@server.broadcast 'channel', {}
|
||||||
|
wait_for_async
|
||||||
|
refute Thread.current[:ran_callback], "User callback was not run through the worker pool"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def subscribe_to(connection, identifiers:)
|
def subscribe_to(connection, identifiers:)
|
||||||
receive connection, command: 'subscribe', identifiers: identifiers
|
receive connection, command: 'subscribe', identifiers: identifiers
|
||||||
|
@ -151,8 +173,8 @@ module ActionCable::StreamTests
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def receive(connection, command:, identifiers:)
|
def receive(connection, command:, identifiers:, channel: 'ActionCable::StreamTests::ChatChannel')
|
||||||
identifier = JSON.generate(channel: 'ActionCable::StreamTests::ChatChannel', **identifiers)
|
identifier = JSON.generate(channel: channel, **identifiers)
|
||||||
connection.dispatch_websocket_message JSON.generate(command: command, identifier: identifier)
|
connection.dispatch_websocket_message JSON.generate(command: command, identifier: identifier)
|
||||||
wait_for_async
|
wait_for_async
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue