2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
require "test_helper"
|
2018-05-27 14:50:04 -04:00
|
|
|
require "minitest/mock"
|
2016-08-06 13:15:15 -04:00
|
|
|
require "stubs/test_connection"
|
|
|
|
require "stubs/room"
|
2015-07-12 11:07:31 -04:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
module ActionCable::StreamTests
|
|
|
|
class Connection < ActionCable::Connection::Base
|
|
|
|
attr_reader :websocket
|
|
|
|
|
|
|
|
def send_async(method, *args)
|
|
|
|
send method, *args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-12 11:07:31 -04:00
|
|
|
class ChatChannel < ActionCable::Channel::Base
|
|
|
|
def subscribed
|
2015-07-21 21:03:47 -04:00
|
|
|
if params[:id]
|
|
|
|
@room = Room.new params[:id]
|
2016-03-11 18:32:02 -05:00
|
|
|
stream_from "test_room_#{@room.id}", coder: pick_coder(params[:coder])
|
2015-07-21 21:03:47 -04:00
|
|
|
end
|
2015-07-12 11:07:31 -04:00
|
|
|
end
|
2015-10-19 16:14:22 -04:00
|
|
|
|
|
|
|
def send_confirmation
|
|
|
|
transmit_subscription_confirmation
|
|
|
|
end
|
2016-03-11 18:32:02 -05:00
|
|
|
|
2018-09-22 07:45:29 -04:00
|
|
|
private
|
|
|
|
def pick_coder(coder)
|
|
|
|
case coder
|
|
|
|
when nil, "json"
|
|
|
|
ActiveSupport::JSON
|
|
|
|
when "custom"
|
|
|
|
DummyEncoder
|
|
|
|
when "none"
|
|
|
|
nil
|
|
|
|
end
|
2016-03-11 18:32:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module DummyEncoder
|
|
|
|
extend self
|
|
|
|
def encode(*) '{ "foo": "encoded" }' end
|
2016-08-06 13:15:15 -04:00
|
|
|
def decode(*) { foo: "decoded" } end
|
2015-07-12 11:07:31 -04:00
|
|
|
end
|
|
|
|
|
2016-02-24 13:35:36 -05:00
|
|
|
class SymbolChannel < ActionCable::Channel::Base
|
|
|
|
def subscribed
|
|
|
|
stream_from :channel
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
class StreamTest < ActionCable::TestCase
|
|
|
|
test "streaming start and stop" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = TestConnection.new
|
2018-05-27 14:50:04 -04:00
|
|
|
pubsub = Minitest::Mock.new connection.pubsub
|
|
|
|
|
|
|
|
pubsub.expect(:subscribe, nil, ["test_room_1", Proc, Proc])
|
|
|
|
pubsub.expect(:unsubscribe, nil, ["test_room_1", Proc])
|
|
|
|
|
|
|
|
connection.stub(:pubsub, pubsub) do
|
|
|
|
channel = ChatChannel.new connection, "{id: 1}", id: 1
|
|
|
|
channel.subscribe_to_channel
|
2015-07-12 11:07:31 -04:00
|
|
|
|
2018-05-27 14:50:04 -04:00
|
|
|
wait_for_async
|
|
|
|
channel.unsubscribe_from_channel
|
|
|
|
end
|
2017-03-22 15:22:38 -04:00
|
|
|
|
2018-05-27 14:50:04 -04:00
|
|
|
assert pubsub.verify
|
2016-03-11 18:32:02 -05:00
|
|
|
end
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-21 21:03:47 -04:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
test "stream from non-string channel" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = TestConnection.new
|
2018-05-27 14:50:04 -04:00
|
|
|
pubsub = Minitest::Mock.new connection.pubsub
|
2018-05-13 12:00:54 -04:00
|
|
|
|
2018-05-27 14:50:04 -04:00
|
|
|
pubsub.expect(:subscribe, nil, ["channel", Proc, Proc])
|
|
|
|
pubsub.expect(:unsubscribe, nil, ["channel", Proc])
|
|
|
|
|
|
|
|
connection.stub(:pubsub, pubsub) do
|
|
|
|
channel = SymbolChannel.new connection, ""
|
|
|
|
channel.subscribe_to_channel
|
|
|
|
|
|
|
|
wait_for_async
|
2016-02-24 13:35:36 -05:00
|
|
|
|
2018-05-27 14:50:04 -04:00
|
|
|
channel.unsubscribe_from_channel
|
|
|
|
end
|
2017-03-22 15:22:38 -04:00
|
|
|
|
2018-05-27 14:50:04 -04:00
|
|
|
assert pubsub.verify
|
2016-03-11 18:32:02 -05:00
|
|
|
end
|
2016-02-24 13:35:36 -05:00
|
|
|
end
|
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
test "stream_for" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = TestConnection.new
|
2015-10-15 22:11:49 -04:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
channel = ChatChannel.new connection, ""
|
2016-09-20 19:57:10 -04:00
|
|
|
channel.subscribe_to_channel
|
2016-03-11 18:32:02 -05:00
|
|
|
channel.stream_for Room.new(1)
|
2018-05-31 11:32:52 -04:00
|
|
|
wait_for_async
|
|
|
|
|
|
|
|
pubsub_call = channel.pubsub.class.class_variable_get "@@subscribe_called"
|
|
|
|
|
|
|
|
assert_equal "action_cable:stream_tests:chat:Room#1-Campfire", pubsub_call[:channel]
|
|
|
|
assert_instance_of Proc, pubsub_call[:callback]
|
|
|
|
assert_instance_of Proc, pubsub_call[:success_callback]
|
2016-03-11 18:32:02 -05:00
|
|
|
end
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-10-16 22:05:33 -04:00
|
|
|
|
2020-03-07 13:09:08 -05:00
|
|
|
test "stream_or_reject_for" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = TestConnection.new
|
|
|
|
|
|
|
|
channel = ChatChannel.new connection, ""
|
|
|
|
channel.subscribe_to_channel
|
|
|
|
channel.stream_or_reject_for Room.new(1)
|
|
|
|
wait_for_async
|
|
|
|
|
|
|
|
pubsub_call = channel.pubsub.class.class_variable_get "@@subscribe_called"
|
|
|
|
|
|
|
|
assert_equal "action_cable:stream_tests:chat:Room#1-Campfire", pubsub_call[:channel]
|
|
|
|
assert_instance_of Proc, pubsub_call[:callback]
|
|
|
|
assert_instance_of Proc, pubsub_call[:success_callback]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "reject subscription when nil is passed to stream_or_reject_for" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = TestConnection.new
|
|
|
|
channel = ChatChannel.new connection, "{id: 1}", id: 1
|
|
|
|
channel.subscribe_to_channel
|
|
|
|
channel.stream_or_reject_for nil
|
|
|
|
assert_nil connection.last_transmission
|
|
|
|
|
|
|
|
wait_for_async
|
|
|
|
|
|
|
|
rejection = { "identifier" => "{id: 1}", "type" => "reject_subscription" }
|
|
|
|
connection.transmit(rejection)
|
|
|
|
assert_equal rejection, connection.last_transmission
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
test "stream_from subscription confirmation" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = TestConnection.new
|
2015-10-16 22:05:33 -04:00
|
|
|
|
2016-09-19 05:29:23 -04:00
|
|
|
channel = ChatChannel.new connection, "{id: 1}", id: 1
|
2016-09-20 19:57:10 -04:00
|
|
|
channel.subscribe_to_channel
|
2016-09-19 05:29:23 -04:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
assert_nil connection.last_transmission
|
2015-10-16 22:05:33 -04:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
wait_for_async
|
2016-01-15 17:11:30 -05:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
confirmation = { "identifier" => "{id: 1}", "type" => "confirm_subscription" }
|
|
|
|
connection.transmit(confirmation)
|
2015-10-16 22:05:33 -04:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
assert_equal confirmation, connection.last_transmission, "Did not receive subscription confirmation within 0.1s"
|
|
|
|
end
|
2015-10-16 22:05:33 -04:00
|
|
|
end
|
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
test "subscription confirmation should only be sent out once" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = TestConnection.new
|
2015-10-19 16:14:22 -04:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
channel = ChatChannel.new connection, "test_channel"
|
|
|
|
channel.send_confirmation
|
|
|
|
channel.send_confirmation
|
2015-10-19 16:14:22 -04:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
wait_for_async
|
2015-10-19 16:14:22 -04:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
expected = { "identifier" => "test_channel", "type" => "confirm_subscription" }
|
|
|
|
assert_equal expected, connection.last_transmission, "Did not receive subscription confirmation"
|
2015-10-19 16:14:22 -04:00
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
assert_equal 1, connection.transmissions.size
|
|
|
|
end
|
2015-10-19 16:14:22 -04:00
|
|
|
end
|
2020-01-17 16:39:06 -05:00
|
|
|
|
|
|
|
test "stop_all_streams" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = TestConnection.new
|
|
|
|
|
|
|
|
channel = ChatChannel.new connection, "{id: 3}"
|
|
|
|
channel.subscribe_to_channel
|
|
|
|
|
|
|
|
assert_equal 0, subscribers_of(connection).size
|
|
|
|
|
|
|
|
channel.stream_from "room_one"
|
|
|
|
channel.stream_from "room_two"
|
|
|
|
|
|
|
|
wait_for_async
|
|
|
|
assert_equal 2, subscribers_of(connection).size
|
|
|
|
|
|
|
|
channel2 = ChatChannel.new connection, "{id: 3}"
|
|
|
|
channel2.subscribe_to_channel
|
|
|
|
|
|
|
|
channel2.stream_from "room_one"
|
|
|
|
wait_for_async
|
|
|
|
|
|
|
|
subscribers = subscribers_of(connection)
|
|
|
|
|
|
|
|
assert_equal 2, subscribers.size
|
|
|
|
assert_equal 2, subscribers["room_one"].size
|
|
|
|
assert_equal 1, subscribers["room_two"].size
|
|
|
|
|
|
|
|
channel.stop_all_streams
|
|
|
|
|
|
|
|
subscribers = subscribers_of(connection)
|
|
|
|
assert_equal 1, subscribers.size
|
|
|
|
assert_equal 1, subscribers["room_one"].size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "stop_stream_from" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = TestConnection.new
|
|
|
|
|
|
|
|
channel = ChatChannel.new connection, "{id: 3}"
|
|
|
|
channel.subscribe_to_channel
|
|
|
|
|
|
|
|
channel.stream_from "room_one"
|
|
|
|
channel.stream_from "room_two"
|
|
|
|
|
|
|
|
channel2 = ChatChannel.new connection, "{id: 3}"
|
|
|
|
channel2.subscribe_to_channel
|
|
|
|
|
|
|
|
channel2.stream_from "room_one"
|
|
|
|
|
|
|
|
subscribers = subscribers_of(connection)
|
|
|
|
|
|
|
|
wait_for_async
|
|
|
|
|
|
|
|
assert_equal 2, subscribers.size
|
|
|
|
assert_equal 2, subscribers["room_one"].size
|
|
|
|
assert_equal 1, subscribers["room_two"].size
|
|
|
|
|
|
|
|
channel.stop_stream_from "room_one"
|
|
|
|
|
|
|
|
subscribers = subscribers_of(connection)
|
|
|
|
|
|
|
|
assert_equal 2, subscribers.size
|
|
|
|
assert_equal 1, subscribers["room_one"].size
|
|
|
|
assert_equal 1, subscribers["room_two"].size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "stop_stream_for" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = TestConnection.new
|
|
|
|
|
|
|
|
channel = ChatChannel.new connection, "{id: 3}"
|
|
|
|
channel.subscribe_to_channel
|
|
|
|
|
|
|
|
channel.stream_for Room.new(1)
|
|
|
|
channel.stream_for Room.new(2)
|
|
|
|
|
|
|
|
channel2 = ChatChannel.new connection, "{id: 3}"
|
|
|
|
channel2.subscribe_to_channel
|
|
|
|
|
|
|
|
channel2.stream_for Room.new(1)
|
|
|
|
|
|
|
|
subscribers = subscribers_of(connection)
|
|
|
|
|
|
|
|
wait_for_async
|
|
|
|
|
|
|
|
assert_equal 2, subscribers.size
|
|
|
|
|
|
|
|
assert_equal 2, subscribers[ChatChannel.broadcasting_for(Room.new(1))].size
|
|
|
|
assert_equal 1, subscribers[ChatChannel.broadcasting_for(Room.new(2))].size
|
|
|
|
|
|
|
|
channel.stop_stream_for Room.new(1)
|
|
|
|
|
|
|
|
subscribers = subscribers_of(connection)
|
|
|
|
|
|
|
|
assert_equal 2, subscribers.size
|
|
|
|
assert_equal 1, subscribers[ChatChannel.broadcasting_for(Room.new(1))].size
|
|
|
|
assert_equal 1, subscribers[ChatChannel.broadcasting_for(Room.new(2))].size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def subscribers_of(connection)
|
|
|
|
connection
|
|
|
|
.pubsub
|
|
|
|
.subscriber_map
|
|
|
|
end
|
2015-10-19 16:14:22 -04:00
|
|
|
end
|
|
|
|
|
2016-09-19 05:29:23 -04:00
|
|
|
require "action_cable/subscription_adapter/async"
|
2016-03-11 18:32:02 -05:00
|
|
|
|
2016-04-13 15:08:00 -04:00
|
|
|
class UserCallbackChannel < ActionCable::Channel::Base
|
|
|
|
def subscribed
|
|
|
|
stream_from :channel do
|
|
|
|
Thread.current[:ran_callback] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-19 05:29:23 -04:00
|
|
|
class MultiChatChannel < ActionCable::Channel::Base
|
|
|
|
def subscribed
|
|
|
|
stream_from "main_room"
|
|
|
|
stream_from "test_all_rooms"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class StreamFromTest < ActionCable::TestCase
|
2016-03-11 18:32:02 -05:00
|
|
|
setup do
|
2016-09-19 05:29:23 -04:00
|
|
|
@server = TestServer.new(subscription_adapter: ActionCable::SubscriptionAdapter::Async)
|
2016-03-11 18:32:02 -05:00
|
|
|
@server.config.allowed_request_origins = %w( http://rubyonrails.com )
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
test "custom encoder" do
|
2016-03-11 18:32:02 -05:00
|
|
|
run_in_eventmachine do
|
|
|
|
connection = open_connection
|
|
|
|
subscribe_to connection, identifiers: { id: 1 }
|
|
|
|
|
2018-04-22 12:33:40 -04:00
|
|
|
assert_called(connection.websocket, :transmit) do
|
2019-09-06 14:20:07 -04:00
|
|
|
@server.broadcast "test_room_1", { foo: "bar" }, coder: DummyEncoder
|
2018-04-22 12:33:40 -04:00
|
|
|
wait_for_async
|
|
|
|
wait_for_executor connection.server.worker_pool.executor
|
|
|
|
end
|
2016-03-11 18:32:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-13 15:08:00 -04:00
|
|
|
test "user supplied callbacks are run through the worker pool" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = open_connection
|
2016-08-06 13:15:15 -04:00
|
|
|
receive(connection, command: "subscribe", channel: UserCallbackChannel.name, identifiers: { id: 1 })
|
2016-04-13 15:08:00 -04:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
@server.broadcast "channel", {}
|
2016-04-13 15:08:00 -04:00
|
|
|
wait_for_async
|
2018-01-24 22:04:11 -05:00
|
|
|
assert_not Thread.current[:ran_callback], "User callback was not run through the worker pool"
|
2016-04-13 15:08:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-01 19:54:42 -04:00
|
|
|
test "subscription confirmation should only be sent out once with multiple stream_from" do
|
2016-09-19 05:29:23 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
connection = open_connection
|
|
|
|
expected = { "identifier" => { "channel" => MultiChatChannel.name }.to_json, "type" => "confirm_subscription" }
|
2018-05-28 05:08:57 -04:00
|
|
|
assert_called_with(connection.websocket, :transmit, [expected.to_json]) do
|
2018-04-22 12:33:40 -04:00
|
|
|
receive(connection, command: "subscribe", channel: MultiChatChannel.name, identifiers: {})
|
|
|
|
wait_for_async
|
|
|
|
end
|
2016-09-19 05:29:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
private
|
|
|
|
def subscribe_to(connection, identifiers:)
|
2016-08-06 13:15:15 -04:00
|
|
|
receive connection, command: "subscribe", identifiers: identifiers
|
2016-03-11 18:32:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def open_connection
|
2016-08-06 13:15:15 -04:00
|
|
|
env = Rack::MockRequest.env_for "/test", "HTTP_HOST" => "localhost", "HTTP_CONNECTION" => "upgrade", "HTTP_UPGRADE" => "websocket", "HTTP_ORIGIN" => "http://rubyonrails.com"
|
2016-03-11 18:32:02 -05:00
|
|
|
|
|
|
|
Connection.new(@server, env).tap do |connection|
|
|
|
|
connection.process
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate connection.websocket, :possible?
|
2016-03-11 18:32:02 -05:00
|
|
|
|
|
|
|
wait_for_async
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate connection.websocket, :alive?
|
2016-03-11 18:32:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
def receive(connection, command:, identifiers:, channel: "ActionCable::StreamTests::ChatChannel")
|
2018-04-13 01:23:19 -04:00
|
|
|
identifier = JSON.generate(identifiers.merge(channel: channel))
|
2016-03-11 18:32:02 -05:00
|
|
|
connection.dispatch_websocket_message JSON.generate(command: command, identifier: identifier)
|
|
|
|
wait_for_async
|
|
|
|
end
|
|
|
|
end
|
2015-07-12 11:07:31 -04:00
|
|
|
end
|