aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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