aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2015-10-19 15:14:22 -0500
committerPratik Naik <pratiknaik@gmail.com>2015-10-19 15:14:22 -0500
commit506d84c157b041208f40b7034cac9c5d5e66fff5 (patch)
treefba84aec142722d5381906a2ed5d9c01dc5e2152
parent0d99cfd5ca755643a23beec22513072987c8edba (diff)
downloadrails-506d84c157b041208f40b7034cac9c5d5e66fff5.tar.gz
rails-506d84c157b041208f40b7034cac9c5d5e66fff5.tar.bz2
rails-506d84c157b041208f40b7034cac9c5d5e66fff5.zip
Make sure the subscription confirmaion is only sent out once
-rw-r--r--lib/action_cable/channel/base.rb12
-rw-r--r--test/channel/stream_test.rb24
2 files changed, 34 insertions, 2 deletions
diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb
index 0b9aa2f4ad..221730dbc4 100644
--- a/lib/action_cable/channel/base.rb
+++ b/lib/action_cable/channel/base.rb
@@ -180,6 +180,10 @@ module ActionCable
@defer_subscription_confirmation
end
+ def subscription_confirmation_sent?
+ @subscription_confirmation_sent
+ end
+
private
def delegate_connection_identifiers
connection.identifiers.each do |identifier|
@@ -231,8 +235,12 @@ module ActionCable
end
def transmit_subscription_confirmation
- logger.info "#{self.class.name} is transmitting the subscription confirmation"
- connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE)
+ unless subscription_confirmation_sent?
+ logger.info "#{self.class.name} is transmitting the subscription confirmation"
+ connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE)
+
+ @subscription_confirmation_sent = true
+ end
end
end
diff --git a/test/channel/stream_test.rb b/test/channel/stream_test.rb
index 08cfef5736..d651ba1746 100644
--- a/test/channel/stream_test.rb
+++ b/test/channel/stream_test.rb
@@ -10,6 +10,11 @@ class ActionCable::Channel::StreamTest < ActionCable::TestCase
stream_from "test_room_#{@room.id}"
end
end
+
+ def send_confirmation
+ transmit_subscription_confirmation
+ end
+
end
test "streaming start and stop" do
@@ -53,4 +58,23 @@ class ActionCable::Channel::StreamTest < ActionCable::TestCase
end
end
+ test "stream_from subscription confirmation should only be sent out once" do
+ 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"
+ assert_equal expected, connection.last_transmission, "Did not receive subscription confirmation within 0.1s"
+
+ assert_equal 1, connection.transmissions.size
+ EM.stop
+ end
+ end
+
end