2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-24 13:38:48 -05:00
|
|
|
require "test_helper"
|
2016-05-11 19:55:17 -04:00
|
|
|
require "stubs/test_server"
|
2016-02-24 13:38:48 -05:00
|
|
|
|
2018-06-08 16:19:39 -04:00
|
|
|
class BroadcastingTest < ActionCable::TestCase
|
2016-02-24 13:38:48 -05:00
|
|
|
test "fetching a broadcaster converts the broadcasting queue to a string" do
|
|
|
|
broadcasting = :test_queue
|
|
|
|
server = TestServer.new
|
|
|
|
broadcaster = server.broadcaster_for(broadcasting)
|
|
|
|
|
|
|
|
assert_equal "test_queue", broadcaster.broadcasting
|
|
|
|
end
|
2016-05-11 19:55:17 -04:00
|
|
|
|
|
|
|
test "broadcast generates notification" do
|
2018-12-20 12:44:01 -05:00
|
|
|
server = TestServer.new
|
|
|
|
|
|
|
|
events = []
|
|
|
|
ActiveSupport::Notifications.subscribe "broadcast.action_cable" do |*args|
|
|
|
|
events << ActiveSupport::Notifications::Event.new(*args)
|
2016-05-11 19:55:17 -04:00
|
|
|
end
|
2018-12-20 12:44:01 -05:00
|
|
|
|
|
|
|
broadcasting = "test_queue"
|
|
|
|
message = { body: "test message" }
|
|
|
|
server.broadcast(broadcasting, message)
|
|
|
|
|
|
|
|
assert_equal 1, events.length
|
|
|
|
assert_equal "broadcast.action_cable", events[0].name
|
|
|
|
assert_equal broadcasting, events[0].payload[:broadcasting]
|
|
|
|
assert_equal message, events[0].payload[:message]
|
|
|
|
assert_equal ActiveSupport::JSON, events[0].payload[:coder]
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Notifications.unsubscribe "broadcast.action_cable"
|
2016-05-11 19:55:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "broadcaster from broadcaster_for generates notification" do
|
2018-12-20 12:44:01 -05:00
|
|
|
server = TestServer.new
|
|
|
|
|
|
|
|
events = []
|
|
|
|
ActiveSupport::Notifications.subscribe "broadcast.action_cable" do |*args|
|
|
|
|
events << ActiveSupport::Notifications::Event.new(*args)
|
2016-05-11 19:55:17 -04:00
|
|
|
end
|
2018-12-20 12:44:01 -05:00
|
|
|
|
|
|
|
broadcasting = "test_queue"
|
|
|
|
message = { body: "test message" }
|
|
|
|
|
|
|
|
broadcaster = server.broadcaster_for(broadcasting)
|
|
|
|
broadcaster.broadcast(message)
|
|
|
|
|
|
|
|
assert_equal 1, events.length
|
|
|
|
assert_equal "broadcast.action_cable", events[0].name
|
|
|
|
assert_equal broadcasting, events[0].payload[:broadcasting]
|
|
|
|
assert_equal message, events[0].payload[:message]
|
|
|
|
assert_equal ActiveSupport::JSON, events[0].payload[:coder]
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Notifications.unsubscribe "broadcast.action_cable"
|
2016-05-11 19:55:17 -04:00
|
|
|
end
|
2016-02-24 13:38:48 -05:00
|
|
|
end
|