2015-07-12 11:07:31 -04:00
|
|
|
require 'test_helper'
|
|
|
|
require 'stubs/test_connection'
|
2015-07-13 11:43:52 -04:00
|
|
|
require 'stubs/room'
|
2015-07-12 11:07:31 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
class ActionCable::Channel::StreamTest < ActionCable::TestCase
|
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]
|
|
|
|
stream_from "test_room_#{@room.id}"
|
|
|
|
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
|
|
|
|
|
2015-07-12 11:07:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "streaming start and stop" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
2015-10-16 22:05:33 -04:00
|
|
|
connection = TestConnection.new
|
|
|
|
connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("test_room_1").returns stub_everything(:pubsub) }
|
|
|
|
channel = ChatChannel.new connection, "{id: 1}", { id: 1 }
|
2015-07-12 11:07:31 -04:00
|
|
|
|
2015-10-16 22:05:33 -04:00
|
|
|
connection.expects(:pubsub).returns mock().tap { |m| m.expects(:unsubscribe_proc) }
|
2015-10-15 22:11:49 -04:00
|
|
|
channel.unsubscribe_from_channel
|
|
|
|
end
|
2015-07-12 11:07:31 -04:00
|
|
|
end
|
2015-07-21 21:03:47 -04:00
|
|
|
|
|
|
|
test "stream_for" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
2015-10-16 22:05:33 -04:00
|
|
|
connection = TestConnection.new
|
2015-10-15 22:11:49 -04:00
|
|
|
EM.next_tick do
|
2015-10-16 22:05:33 -04:00
|
|
|
connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("action_cable:channel:stream_test:chat:Room#1-Campfire").returns stub_everything(:pubsub) }
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
|
|
|
|
2015-10-16 22:05:33 -04:00
|
|
|
channel = ChatChannel.new connection, ""
|
2015-10-15 22:11:49 -04:00
|
|
|
channel.stream_for Room.new(1)
|
|
|
|
end
|
2015-07-21 21:03:47 -04:00
|
|
|
end
|
2015-10-16 22:05:33 -04:00
|
|
|
|
|
|
|
test "stream_from subscription confirmation" do
|
|
|
|
EM.run do
|
|
|
|
connection = TestConnection.new
|
|
|
|
connection.expects(:pubsub).returns EM::Hiredis.connect.pubsub
|
|
|
|
|
2015-12-17 10:23:36 -05:00
|
|
|
ChatChannel.new connection, "{id: 1}", { id: 1 }
|
2015-10-16 22:05:33 -04:00
|
|
|
assert_nil connection.last_transmission
|
|
|
|
|
|
|
|
EM::Timer.new(0.1) do
|
|
|
|
expected = ActiveSupport::JSON.encode "identifier" => "{id: 1}", "type" => "confirm_subscription"
|
2015-10-16 22:13:46 -04:00
|
|
|
assert_equal expected, connection.last_transmission, "Did not receive subscription confirmation within 0.1s"
|
2015-10-16 22:05:33 -04:00
|
|
|
|
|
|
|
EM.run_deferred_callbacks
|
|
|
|
EM.stop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-19 16:27:43 -04:00
|
|
|
test "subscription confirmation should only be sent out once" do
|
2015-10-19 16:14:22 -04:00
|
|
|
EM.run do
|
|
|
|
connection = TestConnection.new
|
|
|
|
connection.stubs(:pubsub).returns EM::Hiredis.connect.pubsub
|
|
|
|
|
|
|
|
channel = ChatChannel.new connection, "test_channel"
|
|
|
|
channel.send_confirmation
|
|
|
|
channel.send_confirmation
|
|
|
|
|
|
|
|
EM.run_deferred_callbacks
|
|
|
|
|
|
|
|
expected = ActiveSupport::JSON.encode "identifier" => "test_channel", "type" => "confirm_subscription"
|
2015-10-19 16:28:26 -04:00
|
|
|
assert_equal expected, connection.last_transmission, "Did not receive subscription confirmation"
|
2015-10-19 16:14:22 -04:00
|
|
|
|
|
|
|
assert_equal 1, connection.transmissions.size
|
|
|
|
EM.stop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-12 11:07:31 -04:00
|
|
|
end
|