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 12:25:53 -05:00
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
require "websocket-client-simple"
|
2016-08-06 13:15:15 -04:00
|
|
|
require "json"
|
2016-01-24 12:25:53 -05:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
require "active_support/hash_with_indifferent_access"
|
2016-06-28 12:54:30 -04:00
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
####
|
|
|
|
# 😷 Warning suppression 😷
|
|
|
|
WebSocket::Frame::Handler::Handler03.prepend Module.new {
|
|
|
|
def initialize(*)
|
|
|
|
@application_data_buffer = nil
|
|
|
|
super
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocket::Frame::Data.prepend Module.new {
|
|
|
|
def initialize(*)
|
|
|
|
@masking_key = nil
|
|
|
|
super
|
|
|
|
end
|
|
|
|
}
|
|
|
|
#
|
|
|
|
####
|
|
|
|
|
2016-01-24 12:25:53 -05:00
|
|
|
class ClientTest < ActionCable::TestCase
|
2016-10-01 20:39:18 -04:00
|
|
|
WAIT_WHEN_EXPECTING_EVENT = 2
|
2016-03-01 19:50:19 -05:00
|
|
|
WAIT_WHEN_NOT_EXPECTING_EVENT = 0.5
|
2016-01-24 12:25:53 -05:00
|
|
|
|
2016-06-01 14:38:48 -04:00
|
|
|
class EchoChannel < ActionCable::Channel::Base
|
|
|
|
def subscribed
|
|
|
|
stream_from "global"
|
|
|
|
end
|
|
|
|
|
|
|
|
def unsubscribed
|
2016-08-06 13:15:15 -04:00
|
|
|
"Goodbye from EchoChannel!"
|
2016-06-01 14:38:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ding(data)
|
2019-09-06 14:21:45 -04:00
|
|
|
transmit({ dong: data["message"] })
|
2016-06-01 14:38:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delay(data)
|
|
|
|
sleep 1
|
2019-09-06 14:21:45 -04:00
|
|
|
transmit({ dong: data["message"] })
|
2016-06-01 14:38:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def bulk(data)
|
2019-09-06 14:21:45 -04:00
|
|
|
ActionCable.server.broadcast "global", { wide: data["message"] }
|
2016-06-01 14:38:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-24 12:25:53 -05:00
|
|
|
def setup
|
|
|
|
ActionCable.instance_variable_set(:@server, nil)
|
|
|
|
server = ActionCable.server
|
2016-02-06 14:40:42 -05:00
|
|
|
server.config.logger = Logger.new(StringIO.new).tap { |l| l.level = Logger::UNKNOWN }
|
2016-01-24 12:25:53 -05:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
server.config.cable = ActiveSupport::HashWithIndifferentAccess.new(adapter: "async")
|
2016-01-24 12:25:53 -05:00
|
|
|
|
|
|
|
# and now the "real" setup for our test:
|
|
|
|
server.config.disable_request_forgery_protection = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_puma_server(rack_app = ActionCable.server, port = 3099)
|
|
|
|
server = ::Puma::Server.new(rack_app, ::Puma::Events.strings)
|
2016-08-06 13:15:15 -04:00
|
|
|
server.add_tcp_listener "127.0.0.1", port
|
2016-01-24 12:25:53 -05:00
|
|
|
server.min_threads = 1
|
|
|
|
server.max_threads = 4
|
|
|
|
|
2017-04-04 16:55:25 -04:00
|
|
|
thread = server.run
|
2016-01-24 12:25:53 -05:00
|
|
|
|
2017-04-04 16:55:25 -04:00
|
|
|
begin
|
|
|
|
yield port
|
|
|
|
|
|
|
|
ensure
|
|
|
|
server.stop
|
|
|
|
|
|
|
|
begin
|
|
|
|
thread.join
|
2017-04-05 17:43:16 -04:00
|
|
|
|
|
|
|
rescue IOError
|
|
|
|
# Work around https://bugs.ruby-lang.org/issues/13405
|
|
|
|
#
|
|
|
|
# Puma's sometimes raising while shutting down, when it closes
|
|
|
|
# its internal pipe. We can safely ignore that, but we do need
|
|
|
|
# to do the step skipped by the exception:
|
|
|
|
server.binder.close
|
|
|
|
|
2017-04-04 16:55:25 -04:00
|
|
|
rescue RuntimeError => ex
|
2017-04-05 17:43:16 -04:00
|
|
|
# Work around https://bugs.ruby-lang.org/issues/13239
|
2019-07-29 01:23:10 -04:00
|
|
|
raise unless ex.message.match?(/can't modify frozen IOError/)
|
2017-04-04 16:55:25 -04:00
|
|
|
|
2017-04-05 17:43:16 -04:00
|
|
|
# Handle this as if it were the IOError: do the same as above.
|
2017-04-04 16:55:25 -04:00
|
|
|
server.binder.close
|
|
|
|
end
|
|
|
|
end
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class SyncClient
|
|
|
|
attr_reader :pings
|
|
|
|
|
2016-01-29 15:11:14 -05:00
|
|
|
def initialize(port)
|
2016-10-01 20:39:18 -04:00
|
|
|
messages = @messages = Queue.new
|
|
|
|
closed = @closed = Concurrent::Event.new
|
|
|
|
has_messages = @has_messages = Concurrent::Semaphore.new(0)
|
|
|
|
pings = @pings = Concurrent::AtomicFixnum.new(0)
|
|
|
|
|
|
|
|
open = Concurrent::Promise.new
|
|
|
|
|
|
|
|
@ws = WebSocket::Client::Simple.connect("ws://127.0.0.1:#{port}/") do |ws|
|
|
|
|
ws.on(:error) do |event|
|
|
|
|
event = RuntimeError.new(event.message) unless event.is_a?(Exception)
|
|
|
|
|
|
|
|
if open.pending?
|
|
|
|
open.fail(event)
|
|
|
|
else
|
|
|
|
messages << event
|
|
|
|
has_messages.release
|
|
|
|
end
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
ws.on(:open) do |event|
|
|
|
|
open.set(true)
|
|
|
|
end
|
2016-01-24 12:25:53 -05:00
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
ws.on(:message) do |event|
|
|
|
|
if event.type == :close
|
|
|
|
closed.set
|
|
|
|
else
|
|
|
|
message = JSON.parse(event.data)
|
|
|
|
if message["type"] == "ping"
|
|
|
|
pings.increment
|
|
|
|
else
|
|
|
|
messages << message
|
|
|
|
has_messages.release
|
|
|
|
end
|
|
|
|
end
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
|
2019-01-17 11:32:35 -05:00
|
|
|
ws.on(:close) do |_|
|
2016-10-01 20:39:18 -04:00
|
|
|
closed.set
|
|
|
|
end
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
open.wait!(WAIT_WHEN_EXPECTING_EVENT)
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_message
|
2016-02-18 16:20:42 -05:00
|
|
|
@has_messages.try_acquire(1, WAIT_WHEN_EXPECTING_EVENT)
|
2016-01-24 12:25:53 -05:00
|
|
|
|
|
|
|
msg = @messages.pop(true)
|
|
|
|
raise msg if msg.is_a?(Exception)
|
|
|
|
|
|
|
|
msg
|
|
|
|
end
|
|
|
|
|
2016-01-28 05:49:10 -05:00
|
|
|
def read_messages(expected_size = 0)
|
2016-01-24 12:25:53 -05:00
|
|
|
list = []
|
|
|
|
loop do
|
2016-02-18 16:20:42 -05:00
|
|
|
if @has_messages.try_acquire(1, list.size < expected_size ? WAIT_WHEN_EXPECTING_EVENT : WAIT_WHEN_NOT_EXPECTING_EVENT)
|
|
|
|
msg = @messages.pop(true)
|
|
|
|
raise msg if msg.is_a?(Exception)
|
|
|
|
|
|
|
|
list << msg
|
2016-01-24 12:25:53 -05:00
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
list
|
|
|
|
end
|
|
|
|
|
2016-03-11 18:32:02 -05:00
|
|
|
def send_message(message)
|
|
|
|
@ws.send(JSON.generate(message))
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
sleep WAIT_WHEN_NOT_EXPECTING_EVENT
|
|
|
|
|
|
|
|
unless @messages.empty?
|
|
|
|
raise "#{@messages.size} messages unprocessed"
|
|
|
|
end
|
|
|
|
|
|
|
|
@ws.close
|
2016-02-24 23:01:19 -05:00
|
|
|
wait_for_close
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for_close
|
2016-01-24 12:25:53 -05:00
|
|
|
@closed.wait(WAIT_WHEN_EXPECTING_EVENT)
|
|
|
|
end
|
2016-02-24 23:01:19 -05:00
|
|
|
|
|
|
|
def closed?
|
|
|
|
@closed.set?
|
|
|
|
end
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
def websocket_client(port)
|
2016-01-29 15:11:14 -05:00
|
|
|
SyncClient.new(port)
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
def concurrently(enum)
|
2021-09-15 11:50:49 -04:00
|
|
|
enum.map { |*x| Concurrent::Promises.future { yield(*x) } }.map(&:value!)
|
2016-10-01 20:39:18 -04:00
|
|
|
end
|
|
|
|
|
2016-01-24 12:25:53 -05:00
|
|
|
def test_single_client
|
|
|
|
with_puma_server do |port|
|
2016-10-01 20:39:18 -04:00
|
|
|
c = websocket_client(port)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "type" => "welcome" }, c.read_message) # pop the first welcome message off the stack
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "subscribe", identifier: JSON.generate(channel: "ClientTest::EchoChannel")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "identifier" => "{\"channel\":\"ClientTest::EchoChannel\"}", "type" => "confirm_subscription" }, c.read_message)
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "message", identifier: JSON.generate(channel: "ClientTest::EchoChannel"), data: JSON.generate(action: "ding", message: "hello")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "identifier" => "{\"channel\":\"ClientTest::EchoChannel\"}", "message" => { "dong" => "hello" } }, c.read_message)
|
2016-01-24 12:25:53 -05:00
|
|
|
c.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_interacting_clients
|
|
|
|
with_puma_server do |port|
|
2016-10-01 20:39:18 -04:00
|
|
|
clients = concurrently(10.times) { websocket_client(port) }
|
2016-01-24 12:25:53 -05:00
|
|
|
|
|
|
|
barrier_1 = Concurrent::CyclicBarrier.new(clients.size)
|
|
|
|
barrier_2 = Concurrent::CyclicBarrier.new(clients.size)
|
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
concurrently(clients) do |c|
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "type" => "welcome" }, c.read_message) # pop the first welcome message off the stack
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "subscribe", identifier: JSON.generate(channel: "ClientTest::EchoChannel")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "identifier" => '{"channel":"ClientTest::EchoChannel"}', "type" => "confirm_subscription" }, c.read_message)
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "message", identifier: JSON.generate(channel: "ClientTest::EchoChannel"), data: JSON.generate(action: "ding", message: "hello")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "identifier" => '{"channel":"ClientTest::EchoChannel"}', "message" => { "dong" => "hello" } }, c.read_message)
|
2016-01-24 12:25:53 -05:00
|
|
|
barrier_1.wait WAIT_WHEN_EXPECTING_EVENT
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "message", identifier: JSON.generate(channel: "ClientTest::EchoChannel"), data: JSON.generate(action: "bulk", message: "hello")
|
2016-01-24 12:25:53 -05:00
|
|
|
barrier_2.wait WAIT_WHEN_EXPECTING_EVENT
|
2016-01-28 05:49:10 -05:00
|
|
|
assert_equal clients.size, c.read_messages(clients.size).size
|
2016-10-01 20:39:18 -04:00
|
|
|
end
|
2016-01-24 12:25:53 -05:00
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
concurrently(clients, &:close)
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_many_clients
|
|
|
|
with_puma_server do |port|
|
2016-10-01 20:39:18 -04:00
|
|
|
clients = concurrently(100.times) { websocket_client(port) }
|
2016-01-24 12:25:53 -05:00
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
concurrently(clients) do |c|
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "type" => "welcome" }, c.read_message) # pop the first welcome message off the stack
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "subscribe", identifier: JSON.generate(channel: "ClientTest::EchoChannel")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "identifier" => '{"channel":"ClientTest::EchoChannel"}', "type" => "confirm_subscription" }, c.read_message)
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "message", identifier: JSON.generate(channel: "ClientTest::EchoChannel"), data: JSON.generate(action: "ding", message: "hello")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "identifier" => '{"channel":"ClientTest::EchoChannel"}', "message" => { "dong" => "hello" } }, c.read_message)
|
2016-10-01 20:39:18 -04:00
|
|
|
end
|
2016-01-24 12:25:53 -05:00
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
concurrently(clients, &:close)
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
end
|
2016-01-28 03:16:14 -05:00
|
|
|
|
|
|
|
def test_disappearing_client
|
|
|
|
with_puma_server do |port|
|
2016-10-01 20:39:18 -04:00
|
|
|
c = websocket_client(port)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "type" => "welcome" }, c.read_message) # pop the first welcome message off the stack
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "subscribe", identifier: JSON.generate(channel: "ClientTest::EchoChannel")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "identifier" => "{\"channel\":\"ClientTest::EchoChannel\"}", "type" => "confirm_subscription" }, c.read_message)
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "message", identifier: JSON.generate(channel: "ClientTest::EchoChannel"), data: JSON.generate(action: "delay", message: "hello")
|
2016-01-29 16:21:41 -05:00
|
|
|
c.close # disappear before write
|
2016-01-28 03:16:14 -05:00
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
c = websocket_client(port)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "type" => "welcome" }, c.read_message) # pop the first welcome message off the stack
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "subscribe", identifier: JSON.generate(channel: "ClientTest::EchoChannel")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "identifier" => "{\"channel\":\"ClientTest::EchoChannel\"}", "type" => "confirm_subscription" }, c.read_message)
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "message", identifier: JSON.generate(channel: "ClientTest::EchoChannel"), data: JSON.generate(action: "ding", message: "hello")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "identifier" => '{"channel":"ClientTest::EchoChannel"}', "message" => { "dong" => "hello" } }, c.read_message)
|
2016-01-29 16:21:41 -05:00
|
|
|
c.close # disappear before read
|
2016-01-28 03:16:14 -05:00
|
|
|
end
|
|
|
|
end
|
2016-02-16 12:20:03 -05:00
|
|
|
|
|
|
|
def test_unsubscribe_client
|
|
|
|
with_puma_server do |port|
|
|
|
|
app = ActionCable.server
|
2016-08-06 13:15:15 -04:00
|
|
|
identifier = JSON.generate(channel: "ClientTest::EchoChannel")
|
2016-02-16 12:20:03 -05:00
|
|
|
|
2016-10-01 20:39:18 -04:00
|
|
|
c = websocket_client(port)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "type" => "welcome" }, c.read_message)
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "subscribe", identifier: identifier
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "identifier" => "{\"channel\":\"ClientTest::EchoChannel\"}", "type" => "confirm_subscription" }, c.read_message)
|
2016-02-16 12:20:03 -05:00
|
|
|
assert_equal(1, app.connections.count)
|
|
|
|
assert(app.remote_connections.where(identifier: identifier))
|
|
|
|
|
2016-04-15 02:04:42 -04:00
|
|
|
subscriptions = app.connections.first.subscriptions.send(:subscriptions)
|
2016-08-06 13:15:15 -04:00
|
|
|
assert_not_equal 0, subscriptions.size, "Missing EchoChannel subscription"
|
2016-04-15 02:04:42 -04:00
|
|
|
channel = subscriptions.first[1]
|
2018-04-22 12:33:40 -04:00
|
|
|
assert_called(channel, :unsubscribed) do
|
|
|
|
c.close
|
|
|
|
sleep 0.1 # Data takes a moment to process
|
|
|
|
end
|
2016-02-16 12:20:03 -05:00
|
|
|
|
|
|
|
# All data is removed: No more connection or subscription information!
|
|
|
|
assert_equal(0, app.connections.count)
|
|
|
|
end
|
|
|
|
end
|
2016-02-24 23:01:19 -05:00
|
|
|
|
|
|
|
def test_server_restart
|
|
|
|
with_puma_server do |port|
|
2016-10-01 20:39:18 -04:00
|
|
|
c = websocket_client(port)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "type" => "welcome" }, c.read_message)
|
2016-08-06 13:15:15 -04:00
|
|
|
c.send_message command: "subscribe", identifier: JSON.generate(channel: "ClientTest::EchoChannel")
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "identifier" => "{\"channel\":\"ClientTest::EchoChannel\"}", "type" => "confirm_subscription" }, c.read_message)
|
2016-02-24 23:01:19 -05:00
|
|
|
|
|
|
|
ActionCable.server.restart
|
|
|
|
c.wait_for_close
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate c, :closed?
|
2016-02-24 23:01:19 -05:00
|
|
|
end
|
|
|
|
end
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|