diff options
Diffstat (limited to 'test/connection/subscriptions_test.rb')
-rw-r--r-- | test/connection/subscriptions_test.rb | 85 |
1 files changed, 57 insertions, 28 deletions
diff --git a/test/connection/subscriptions_test.rb b/test/connection/subscriptions_test.rb index 24fe8f9300..4f6760827e 100644 --- a/test/connection/subscriptions_test.rb +++ b/test/connection/subscriptions_test.rb @@ -1,8 +1,13 @@ require 'test_helper' -class ActionCable::Connection::SubscriptionsTest < ActiveSupport::TestCase +class ActionCable::Connection::SubscriptionsTest < ActionCable::TestCase class Connection < ActionCable::Connection::Base attr_reader :websocket + + def send_async(method, *args) + # Bypass Celluloid + send method, *args + end end class ChatChannel < ActionCable::Channel::Base @@ -22,59 +27,76 @@ class ActionCable::Connection::SubscriptionsTest < ActiveSupport::TestCase @server = TestServer.new @server.stubs(:channel_classes).returns(ChatChannel.name => ChatChannel) - env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' - @connection = Connection.new(@server, env) - - @subscriptions = ActionCable::Connection::Subscriptions.new(@connection) - @chat_identifier = { id: 1, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel' }.to_json + @chat_identifier = ActiveSupport::JSON.encode(id: 1, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel') end test "subscribe command" do - channel = subscribe_to_chat_channel + run_in_eventmachine do + setup_connection + channel = subscribe_to_chat_channel - assert_kind_of ChatChannel, channel - assert_equal 1, channel.room.id + assert_kind_of ChatChannel, channel + assert_equal 1, channel.room.id + end end test "subscribe command without an identifier" do - @subscriptions.execute_command 'command' => 'subscribe' - assert @subscriptions.identifiers.empty? + run_in_eventmachine do + setup_connection + + @subscriptions.execute_command 'command' => 'subscribe' + assert @subscriptions.identifiers.empty? + end end test "unsubscribe command" do - subscribe_to_chat_channel + run_in_eventmachine do + setup_connection + subscribe_to_chat_channel - channel = subscribe_to_chat_channel - channel.expects(:unsubscribe_from_channel) + channel = subscribe_to_chat_channel + channel.expects(:unsubscribe_from_channel) - @subscriptions.execute_command 'command' => 'unsubscribe', 'identifier' => @chat_identifier - assert @subscriptions.identifiers.empty? + @subscriptions.execute_command 'command' => 'unsubscribe', 'identifier' => @chat_identifier + assert @subscriptions.identifiers.empty? + end end test "unsubscribe command without an identifier" do - @subscriptions.execute_command 'command' => 'unsubscribe' - assert @subscriptions.identifiers.empty? + run_in_eventmachine do + setup_connection + + @subscriptions.execute_command 'command' => 'unsubscribe' + assert @subscriptions.identifiers.empty? + end end test "message command" do - channel = subscribe_to_chat_channel + run_in_eventmachine do + setup_connection + channel = subscribe_to_chat_channel - data = { 'content' => 'Hello World!', 'action' => 'speak' } - @subscriptions.execute_command 'command' => 'message', 'identifier' => @chat_identifier, 'data' => data.to_json + data = { 'content' => 'Hello World!', 'action' => 'speak' } + @subscriptions.execute_command 'command' => 'message', 'identifier' => @chat_identifier, 'data' => ActiveSupport::JSON.encode(data) - assert_equal [ data ], channel.lines + assert_equal [ data ], channel.lines + end end test "unsubscrib from all" do - channel1 = subscribe_to_chat_channel + run_in_eventmachine do + setup_connection - channel2_id = { id: 2, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel' }.to_json - channel2 = subscribe_to_chat_channel(channel2_id) + channel1 = subscribe_to_chat_channel - channel1.expects(:unsubscribe_from_channel) - channel2.expects(:unsubscribe_from_channel) + channel2_id = ActiveSupport::JSON.encode(id: 2, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel') + channel2 = subscribe_to_chat_channel(channel2_id) - @subscriptions.unsubscribe_from_all + channel1.expects(:unsubscribe_from_channel) + channel2.expects(:unsubscribe_from_channel) + + @subscriptions.unsubscribe_from_all + end end private @@ -84,4 +106,11 @@ class ActionCable::Connection::SubscriptionsTest < ActiveSupport::TestCase @subscriptions.send :find, 'identifier' => identifier end + + def setup_connection + env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' + @connection = Connection.new(@server, env) + + @subscriptions = ActionCable::Connection::Subscriptions.new(@connection) + end end |