1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actioncable/test/channel/stream_test.rb

91 lines
2.9 KiB
Ruby
Raw Normal View History

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
if params[:id]
@room = Room.new params[:id]
stream_from "test_room_#{@room.id}"
end
2015-07-12 11:07:31 -04:00
end
def send_confirmation
transmit_subscription_confirmation
end
2015-07-12 11:07:31 -04:00
end
class SymbolChannel < ActionCable::Channel::Base
def subscribed
stream_from :channel
end
end
2015-07-12 11:07:31 -04:00
test "streaming start and stop" do
2015-10-15 22:11:49 -04:00
run_in_eventmachine do
connection = TestConnection.new
connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("test_room_1", kind_of(Proc), kind_of(Proc)).returns stub_everything(:pubsub) }
channel = ChatChannel.new connection, "{id: 1}", { id: 1 }
2015-07-12 11:07:31 -04:00
connection.expects(:pubsub).returns mock().tap { |m| m.expects(:unsubscribe) }
2015-10-15 22:11:49 -04:00
channel.unsubscribe_from_channel
end
2015-07-12 11:07:31 -04:00
end
test "stream from non-string channel" do
run_in_eventmachine do
connection = TestConnection.new
connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("channel", kind_of(Proc), kind_of(Proc)).returns stub_everything(:pubsub) }
channel = SymbolChannel.new connection, ""
connection.expects(:pubsub).returns mock().tap { |m| m.expects(:unsubscribe) }
channel.unsubscribe_from_channel
end
end
test "stream_for" do
2015-10-15 22:11:49 -04:00
run_in_eventmachine do
connection = TestConnection.new
connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("action_cable:channel:stream_test:chat:Room#1-Campfire", kind_of(Proc), kind_of(Proc)).returns stub_everything(:pubsub) }
2015-10-15 22:11:49 -04:00
channel = ChatChannel.new connection, ""
2015-10-15 22:11:49 -04:00
channel.stream_for Room.new(1)
end
end
test "stream_from subscription confirmation" do
run_in_eventmachine do
connection = TestConnection.new
ChatChannel.new connection, "{id: 1}", { id: 1 }
assert_nil connection.last_transmission
wait_for_async
2016-01-15 17:11:30 -05:00
expected = ActiveSupport::JSON.encode "identifier" => "{id: 1}", "type" => "confirm_subscription"
connection.transmit(expected)
assert_equal expected, connection.last_transmission, "Did not receive subscription confirmation within 0.1s"
end
end
2015-10-19 16:27:43 -04:00
test "subscription confirmation should only be sent out once" do
run_in_eventmachine do
connection = TestConnection.new
channel = ChatChannel.new connection, "test_channel"
channel.send_confirmation
channel.send_confirmation
wait_for_async
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"
assert_equal 1, connection.transmissions.size
end
end
2015-07-12 11:07:31 -04:00
end