2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
require "test_helper"
|
|
|
|
require "concurrent"
|
2016-01-24 05:43:40 -05:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
require "active_support/core_ext/hash/indifferent_access"
|
|
|
|
require "pathname"
|
2016-01-24 05:43:40 -05:00
|
|
|
|
|
|
|
module CommonSubscriptionAdapterTest
|
|
|
|
WAIT_WHEN_EXPECTING_EVENT = 3
|
|
|
|
WAIT_WHEN_NOT_EXPECTING_EVENT = 0.2
|
|
|
|
|
|
|
|
def setup
|
|
|
|
server = ActionCable::Server::Base.new
|
|
|
|
server.config.cable = cable_config.with_indifferent_access
|
2016-10-01 20:31:30 -04:00
|
|
|
server.config.logger = Logger.new(StringIO.new).tap { |l| l.level = Logger::UNKNOWN }
|
2016-01-24 05:43:40 -05:00
|
|
|
|
|
|
|
adapter_klass = server.config.pubsub_adapter
|
|
|
|
|
|
|
|
@rx_adapter = adapter_klass.new(server)
|
|
|
|
@tx_adapter = adapter_klass.new(server)
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2016-10-03 16:10:38 -04:00
|
|
|
[@rx_adapter, @tx_adapter].uniq.compact.each(&:shutdown)
|
2016-01-24 05:43:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def subscribe_as_queue(channel, adapter = @rx_adapter)
|
|
|
|
queue = Queue.new
|
|
|
|
|
|
|
|
callback = -> data { queue << data }
|
|
|
|
subscribed = Concurrent::Event.new
|
|
|
|
adapter.subscribe(channel, callback, Proc.new { subscribed.set })
|
|
|
|
subscribed.wait(WAIT_WHEN_EXPECTING_EVENT)
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate subscribed, :set?
|
2016-01-24 05:43:40 -05:00
|
|
|
|
|
|
|
yield queue
|
|
|
|
|
|
|
|
sleep WAIT_WHEN_NOT_EXPECTING_EVENT
|
|
|
|
assert_empty queue
|
|
|
|
ensure
|
|
|
|
adapter.unsubscribe(channel, callback) if subscribed.set?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_subscribe_and_unsubscribe
|
2016-08-06 13:15:15 -04:00
|
|
|
subscribe_as_queue("channel") do |queue|
|
2016-01-24 05:43:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_basic_broadcast
|
2016-08-06 13:15:15 -04:00
|
|
|
subscribe_as_queue("channel") do |queue|
|
|
|
|
@tx_adapter.broadcast("channel", "hello world")
|
2016-01-24 05:43:40 -05:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
assert_equal "hello world", queue.pop
|
2016-01-24 05:43:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_broadcast_after_unsubscribe
|
|
|
|
keep_queue = nil
|
2016-08-06 13:15:15 -04:00
|
|
|
subscribe_as_queue("channel") do |queue|
|
2016-01-24 05:43:40 -05:00
|
|
|
keep_queue = queue
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
@tx_adapter.broadcast("channel", "hello world")
|
2016-01-24 05:43:40 -05:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
assert_equal "hello world", queue.pop
|
2016-01-24 05:43:40 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
@tx_adapter.broadcast("channel", "hello void")
|
2016-01-24 05:43:40 -05:00
|
|
|
|
|
|
|
sleep WAIT_WHEN_NOT_EXPECTING_EVENT
|
|
|
|
assert_empty keep_queue
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_multiple_broadcast
|
2016-08-06 13:15:15 -04:00
|
|
|
subscribe_as_queue("channel") do |queue|
|
|
|
|
@tx_adapter.broadcast("channel", "bananas")
|
|
|
|
@tx_adapter.broadcast("channel", "apples")
|
2016-01-24 05:43:40 -05:00
|
|
|
|
|
|
|
received = []
|
|
|
|
2.times { received << queue.pop }
|
2016-08-06 13:15:15 -04:00
|
|
|
assert_equal ["apples", "bananas"], received.sort
|
2016-01-24 05:43:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_identical_subscriptions
|
2016-08-06 13:15:15 -04:00
|
|
|
subscribe_as_queue("channel") do |queue|
|
|
|
|
subscribe_as_queue("channel") do |queue_2|
|
|
|
|
@tx_adapter.broadcast("channel", "hello")
|
2016-01-24 05:43:40 -05:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
assert_equal "hello", queue_2.pop
|
2016-01-24 05:43:40 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
assert_equal "hello", queue.pop
|
2016-01-24 05:43:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_simultaneous_subscriptions
|
2016-08-06 13:15:15 -04:00
|
|
|
subscribe_as_queue("channel") do |queue|
|
|
|
|
subscribe_as_queue("other channel") do |queue_2|
|
|
|
|
@tx_adapter.broadcast("channel", "apples")
|
|
|
|
@tx_adapter.broadcast("other channel", "oranges")
|
2016-01-24 05:43:40 -05:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
assert_equal "apples", queue.pop
|
|
|
|
assert_equal "oranges", queue_2.pop
|
2016-01-24 05:43:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_channel_filtered_broadcast
|
2016-08-06 13:15:15 -04:00
|
|
|
subscribe_as_queue("channel") do |queue|
|
|
|
|
@tx_adapter.broadcast("other channel", "one")
|
|
|
|
@tx_adapter.broadcast("channel", "two")
|
2016-01-24 05:43:40 -05:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
assert_equal "two", queue.pop
|
2016-01-24 05:43:40 -05:00
|
|
|
end
|
|
|
|
end
|
2017-07-06 10:34:05 -04:00
|
|
|
|
|
|
|
def test_long_identifiers
|
|
|
|
channel_1 = "a" * 100 + "1"
|
|
|
|
channel_2 = "a" * 100 + "2"
|
|
|
|
subscribe_as_queue(channel_1) do |queue|
|
|
|
|
subscribe_as_queue(channel_2) do |queue_2|
|
|
|
|
@tx_adapter.broadcast(channel_1, "apples")
|
|
|
|
@tx_adapter.broadcast(channel_2, "oranges")
|
|
|
|
|
|
|
|
assert_equal "apples", queue.pop
|
|
|
|
assert_equal "oranges", queue_2.pop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-01-24 05:43:40 -05:00
|
|
|
end
|