2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
require "test_helper"
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
class ActionCable::Connection::SubscriptionsTest < ActionCable::TestCase
|
2020-03-20 13:35:10 -04:00
|
|
|
class ChatChannelError < Exception; end
|
|
|
|
|
2015-07-13 12:30:43 -04:00
|
|
|
class Connection < ActionCable::Connection::Base
|
2020-03-20 13:35:10 -04:00
|
|
|
attr_reader :websocket, :exceptions
|
|
|
|
|
|
|
|
rescue_from ChatChannelError, with: :error_handler
|
|
|
|
|
|
|
|
def initialize(*)
|
|
|
|
super
|
|
|
|
@exceptions = []
|
|
|
|
end
|
2015-10-15 22:11:49 -04:00
|
|
|
|
|
|
|
def send_async(method, *args)
|
|
|
|
send method, *args
|
|
|
|
end
|
2020-03-20 13:35:10 -04:00
|
|
|
|
|
|
|
def error_handler(e)
|
|
|
|
@exceptions << e
|
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class ChatChannel < ActionCable::Channel::Base
|
|
|
|
attr_reader :room, :lines
|
|
|
|
|
|
|
|
def subscribed
|
|
|
|
@room = Room.new params[:id]
|
|
|
|
@lines = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def speak(data)
|
|
|
|
@lines << data
|
|
|
|
end
|
2020-03-20 13:35:10 -04:00
|
|
|
|
|
|
|
def throw_exception(_data)
|
|
|
|
raise ChatChannelError.new("Uh Oh")
|
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@server = TestServer.new
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
@chat_identifier = ActiveSupport::JSON.encode(id: 1, channel: "ActionCable::Connection::SubscriptionsTest::ChatChannel")
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "subscribe command" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
channel = subscribe_to_chat_channel
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
assert_kind_of ChatChannel, channel
|
|
|
|
assert_equal 1, channel.room.id
|
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "subscribe command without an identifier" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
@subscriptions.execute_command "command" => "subscribe"
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty @subscriptions.identifiers
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
2020-11-24 22:30:03 -05:00
|
|
|
test "subscribe command with Base channel" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
|
|
|
|
identifier = ActiveSupport::JSON.encode(id: 1, channel: "ActionCable::Channel::Base")
|
|
|
|
@subscriptions.execute_command "command" => "subscribe", "identifier" => identifier
|
|
|
|
|
|
|
|
assert_empty @subscriptions.identifiers
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-13 12:30:43 -04:00
|
|
|
test "unsubscribe command" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
subscribe_to_chat_channel
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
channel = subscribe_to_chat_channel
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2018-04-22 12:33:40 -04:00
|
|
|
assert_called(channel, :unsubscribe_from_channel) do
|
|
|
|
@subscriptions.execute_command "command" => "unsubscribe", "identifier" => @chat_identifier
|
|
|
|
end
|
|
|
|
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty @subscriptions.identifiers
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "unsubscribe command without an identifier" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
@subscriptions.execute_command "command" => "unsubscribe"
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty @subscriptions.identifiers
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "message command" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
channel = subscribe_to_chat_channel
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
data = { "content" => "Hello World!", "action" => "speak" }
|
|
|
|
@subscriptions.execute_command "command" => "message", "identifier" => @chat_identifier, "data" => ActiveSupport::JSON.encode(data)
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
assert_equal [ data ], channel.lines
|
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
2020-03-20 13:35:10 -04:00
|
|
|
test "accessing exceptions thrown during command execution" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
subscribe_to_chat_channel
|
|
|
|
|
|
|
|
data = { "content" => "Hello World!", "action" => "throw_exception" }
|
|
|
|
@subscriptions.execute_command "command" => "message", "identifier" => @chat_identifier, "data" => ActiveSupport::JSON.encode(data)
|
|
|
|
|
|
|
|
exception = @connection.exceptions.first
|
|
|
|
assert_kind_of ChatChannelError, exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-12 21:13:37 -05:00
|
|
|
test "unsubscribe from all" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
channel1 = subscribe_to_chat_channel
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
channel2_id = ActiveSupport::JSON.encode(id: 2, channel: "ActionCable::Connection::SubscriptionsTest::ChatChannel")
|
2015-10-15 22:11:49 -04:00
|
|
|
channel2 = subscribe_to_chat_channel(channel2_id)
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2018-04-22 12:33:40 -04:00
|
|
|
assert_called(channel1, :unsubscribe_from_channel) do
|
|
|
|
assert_called(channel2, :unsubscribe_from_channel) do
|
|
|
|
@subscriptions.unsubscribe_from_all
|
|
|
|
end
|
|
|
|
end
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def subscribe_to_chat_channel(identifier = @chat_identifier)
|
2016-08-06 13:15:15 -04:00
|
|
|
@subscriptions.execute_command "command" => "subscribe", "identifier" => identifier
|
2015-07-13 12:30:43 -04:00
|
|
|
assert_equal identifier, @subscriptions.identifiers.last
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
@subscriptions.send :find, "identifier" => identifier
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
2015-10-15 22:11:49 -04:00
|
|
|
|
|
|
|
def setup_connection
|
2016-08-06 13:15:15 -04:00
|
|
|
env = Rack::MockRequest.env_for "/test", "HTTP_HOST" => "localhost", "HTTP_CONNECTION" => "upgrade", "HTTP_UPGRADE" => "websocket"
|
2015-10-15 22:11:49 -04:00
|
|
|
@connection = Connection.new(@server, env)
|
|
|
|
|
|
|
|
@subscriptions = ActionCable::Connection::Subscriptions.new(@connection)
|
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|