2016-01-24 12:25:53 -05:00
|
|
|
require 'test_helper'
|
|
|
|
require 'concurrent'
|
|
|
|
|
|
|
|
require 'active_support/core_ext/hash/indifferent_access'
|
|
|
|
require 'pathname'
|
|
|
|
|
|
|
|
require 'faye/websocket'
|
|
|
|
require 'json'
|
|
|
|
|
|
|
|
class ClientTest < ActionCable::TestCase
|
2016-03-01 19:50:19 -05:00
|
|
|
WAIT_WHEN_EXPECTING_EVENT = 8
|
|
|
|
WAIT_WHEN_NOT_EXPECTING_EVENT = 0.5
|
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
|
|
|
|
|
|
|
server.config.cable = { adapter: 'async' }.with_indifferent_access
|
2016-03-01 19:50:19 -05:00
|
|
|
server.config.use_faye = ENV['FAYE'].present?
|
2016-01-24 12:25:53 -05:00
|
|
|
|
|
|
|
# and now the "real" setup for our test:
|
|
|
|
server.config.disable_request_forgery_protection = true
|
2016-02-05 16:48:48 -05:00
|
|
|
server.config.channel_paths = [ File.expand_path('client/echo_channel.rb', __dir__) ]
|
2016-01-24 12:25:53 -05:00
|
|
|
|
2016-01-29 15:11:14 -05:00
|
|
|
Thread.new { EventMachine.run } unless EventMachine.reactor_running?
|
2016-01-31 11:30:35 -05:00
|
|
|
Thread.pass until EventMachine.reactor_running?
|
2016-01-24 12:25:53 -05:00
|
|
|
|
|
|
|
# faye-websocket is warning-rich
|
|
|
|
@previous_verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2016-01-28 06:32:48 -05:00
|
|
|
$VERBOSE = @previous_verbose
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def with_puma_server(rack_app = ActionCable.server, port = 3099)
|
|
|
|
server = ::Puma::Server.new(rack_app, ::Puma::Events.strings)
|
|
|
|
server.add_tcp_listener '127.0.0.1', port
|
|
|
|
server.min_threads = 1
|
|
|
|
server.max_threads = 4
|
|
|
|
|
|
|
|
t = Thread.new { server.run.join }
|
|
|
|
yield port
|
|
|
|
|
|
|
|
ensure
|
2016-01-28 07:16:33 -05:00
|
|
|
server.stop(true) if server
|
|
|
|
t.join if t
|
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-01-24 12:25:53 -05:00
|
|
|
@ws = Faye::WebSocket::Client.new("ws://127.0.0.1:#{port}/")
|
|
|
|
@messages = Queue.new
|
|
|
|
@closed = Concurrent::Event.new
|
2016-02-18 16:20:42 -05:00
|
|
|
@has_messages = Concurrent::Semaphore.new(0)
|
2016-01-24 12:25:53 -05:00
|
|
|
@pings = 0
|
|
|
|
|
|
|
|
open = Concurrent::Event.new
|
|
|
|
error = nil
|
|
|
|
|
|
|
|
@ws.on(:error) do |event|
|
|
|
|
if open.set?
|
|
|
|
@messages << RuntimeError.new(event.message)
|
|
|
|
else
|
|
|
|
error = event.message
|
|
|
|
open.set
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@ws.on(:open) do |event|
|
|
|
|
open.set
|
|
|
|
end
|
|
|
|
|
|
|
|
@ws.on(:message) do |event|
|
2016-03-11 18:32:02 -05:00
|
|
|
message = JSON.parse(event.data)
|
|
|
|
if message['type'] == 'ping'
|
2016-01-24 12:25:53 -05:00
|
|
|
@pings += 1
|
|
|
|
else
|
2016-03-11 18:32:02 -05:00
|
|
|
@messages << message
|
2016-02-18 16:20:42 -05:00
|
|
|
@has_messages.release
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@ws.on(:close) do |event|
|
|
|
|
@closed.set
|
|
|
|
end
|
|
|
|
|
|
|
|
open.wait(WAIT_WHEN_EXPECTING_EVENT)
|
|
|
|
raise error if error
|
|
|
|
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
|
|
|
|
|
|
|
|
def faye_client(port)
|
2016-01-29 15:11:14 -05:00
|
|
|
SyncClient.new(port)
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_single_client
|
|
|
|
with_puma_server do |port|
|
|
|
|
c = faye_client(port)
|
2016-02-29 19:21:19 -05:00
|
|
|
assert_equal({"type" => "welcome"}, c.read_message) # pop the first welcome message off the stack
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'subscribe', identifier: JSON.generate(channel: 'EchoChannel')
|
2016-01-24 12:25:53 -05:00
|
|
|
assert_equal({"identifier"=>"{\"channel\":\"EchoChannel\"}", "type"=>"confirm_subscription"}, c.read_message)
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'message', identifier: JSON.generate(channel: 'EchoChannel'), data: JSON.generate(action: 'ding', message: 'hello')
|
2016-01-24 12:25:53 -05:00
|
|
|
assert_equal({"identifier"=>"{\"channel\":\"EchoChannel\"}", "message"=>{"dong"=>"hello"}}, c.read_message)
|
|
|
|
c.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_interacting_clients
|
|
|
|
with_puma_server do |port|
|
2016-01-28 06:32:48 -05:00
|
|
|
clients = 10.times.map { faye_client(port) }
|
2016-01-24 12:25:53 -05:00
|
|
|
|
|
|
|
barrier_1 = Concurrent::CyclicBarrier.new(clients.size)
|
|
|
|
barrier_2 = Concurrent::CyclicBarrier.new(clients.size)
|
|
|
|
|
|
|
|
clients.map {|c| Concurrent::Future.execute {
|
2016-02-29 19:21:19 -05:00
|
|
|
assert_equal({"type" => "welcome"}, c.read_message) # pop the first welcome message off the stack
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'subscribe', identifier: JSON.generate(channel: 'EchoChannel')
|
2016-01-24 12:25:53 -05:00
|
|
|
assert_equal({"identifier"=>'{"channel":"EchoChannel"}', "type"=>"confirm_subscription"}, c.read_message)
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'message', identifier: JSON.generate(channel: 'EchoChannel'), data: JSON.generate(action: 'ding', message: 'hello')
|
2016-01-24 12:25:53 -05:00
|
|
|
assert_equal({"identifier"=>'{"channel":"EchoChannel"}', "message"=>{"dong"=>"hello"}}, c.read_message)
|
|
|
|
barrier_1.wait WAIT_WHEN_EXPECTING_EVENT
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'message', identifier: JSON.generate(channel: '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-01-24 12:25:53 -05:00
|
|
|
} }.each(&:wait!)
|
|
|
|
|
|
|
|
clients.map {|c| Concurrent::Future.execute { c.close } }.each(&:wait!)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_many_clients
|
|
|
|
with_puma_server do |port|
|
2016-02-05 05:56:16 -05:00
|
|
|
clients = 100.times.map { faye_client(port) }
|
2016-01-24 12:25:53 -05:00
|
|
|
|
|
|
|
clients.map {|c| Concurrent::Future.execute {
|
2016-02-29 19:21:19 -05:00
|
|
|
assert_equal({"type" => "welcome"}, c.read_message) # pop the first welcome message off the stack
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'subscribe', identifier: JSON.generate(channel: 'EchoChannel')
|
2016-01-24 12:25:53 -05:00
|
|
|
assert_equal({"identifier"=>'{"channel":"EchoChannel"}', "type"=>"confirm_subscription"}, c.read_message)
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'message', identifier: JSON.generate(channel: 'EchoChannel'), data: JSON.generate(action: 'ding', message: 'hello')
|
2016-01-24 12:25:53 -05:00
|
|
|
assert_equal({"identifier"=>'{"channel":"EchoChannel"}', "message"=>{"dong"=>"hello"}}, c.read_message)
|
|
|
|
} }.each(&:wait!)
|
|
|
|
|
|
|
|
clients.map {|c| Concurrent::Future.execute { c.close } }.each(&:wait!)
|
|
|
|
end
|
|
|
|
end
|
2016-01-28 03:16:14 -05:00
|
|
|
|
|
|
|
def test_disappearing_client
|
|
|
|
with_puma_server do |port|
|
|
|
|
c = faye_client(port)
|
2016-02-29 19:21:19 -05:00
|
|
|
assert_equal({"type" => "welcome"}, c.read_message) # pop the first welcome message off the stack
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'subscribe', identifier: JSON.generate(channel: 'EchoChannel')
|
2016-01-28 03:16:14 -05:00
|
|
|
assert_equal({"identifier"=>"{\"channel\":\"EchoChannel\"}", "type"=>"confirm_subscription"}, c.read_message)
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'message', identifier: JSON.generate(channel: '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
|
|
|
|
|
|
|
c = faye_client(port)
|
2016-02-29 19:21:19 -05:00
|
|
|
assert_equal({"type" => "welcome"}, c.read_message) # pop the first welcome message off the stack
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'subscribe', identifier: JSON.generate(channel: 'EchoChannel')
|
2016-01-28 03:16:14 -05:00
|
|
|
assert_equal({"identifier"=>"{\"channel\":\"EchoChannel\"}", "type"=>"confirm_subscription"}, c.read_message)
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'message', identifier: JSON.generate(channel: 'EchoChannel'), data: JSON.generate(action: 'ding', message: 'hello')
|
2016-01-28 03:16:14 -05:00
|
|
|
assert_equal({"identifier"=>'{"channel":"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-03-11 18:32:02 -05:00
|
|
|
identifier = JSON.generate(channel: 'EchoChannel')
|
2016-02-16 12:20:03 -05:00
|
|
|
|
|
|
|
c = faye_client(port)
|
2016-02-29 19:21:19 -05:00
|
|
|
assert_equal({"type" => "welcome"}, c.read_message)
|
2016-02-16 12:20:03 -05:00
|
|
|
c.send_message command: 'subscribe', identifier: identifier
|
|
|
|
assert_equal({"identifier"=>"{\"channel\":\"EchoChannel\"}", "type"=>"confirm_subscription"}, c.read_message)
|
|
|
|
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)
|
|
|
|
assert_not_equal 0, subscriptions.size, 'Missing EchoChannel subscription'
|
|
|
|
channel = subscriptions.first[1]
|
2016-02-16 12:20:03 -05:00
|
|
|
channel.expects(:unsubscribed)
|
|
|
|
c.close
|
|
|
|
sleep 0.1 # Data takes a moment to process
|
|
|
|
|
|
|
|
# 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|
|
|
|
|
c = faye_client(port)
|
2016-03-02 03:46:28 -05:00
|
|
|
assert_equal({"type" => "welcome"}, c.read_message)
|
2016-03-11 18:32:02 -05:00
|
|
|
c.send_message command: 'subscribe', identifier: JSON.generate(channel: 'EchoChannel')
|
2016-02-24 23:01:19 -05:00
|
|
|
assert_equal({"identifier"=>"{\"channel\":\"EchoChannel\"}", "type"=>"confirm_subscription"}, c.read_message)
|
|
|
|
|
|
|
|
ActionCable.server.restart
|
|
|
|
c.wait_for_close
|
|
|
|
assert c.closed?
|
|
|
|
end
|
|
|
|
end
|
2016-01-24 12:25:53 -05:00
|
|
|
end
|