Skip to content

Commit

Permalink
Run connection tests in EM loop
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo committed Oct 16, 2015
1 parent db56e8b commit ee16ca8
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 128 deletions.
23 changes: 15 additions & 8 deletions test/channel/stream_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'stubs/test_connection'
require 'stubs/room'

class ActionCable::Channel::StreamTest < ActiveSupport::TestCase
class ActionCable::Channel::StreamTest < ActionCable::TestCase
class ChatChannel < ActionCable::Channel::Base
def subscribed
if params[:id]
Expand All @@ -17,16 +17,23 @@ def subscribed
end

test "streaming start and stop" do
@connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("test_room_1") }
channel = ChatChannel.new @connection, "{id: 1}", { id: 1 }
run_in_eventmachine do
@connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("test_room_1") }
channel = ChatChannel.new @connection, "{id: 1}", { id: 1 }

@connection.expects(:pubsub).returns mock().tap { |m| m.expects(:unsubscribe_proc) }
channel.unsubscribe_from_channel
@connection.expects(:pubsub).returns mock().tap { |m| m.expects(:unsubscribe_proc) }
channel.unsubscribe_from_channel
end
end

test "stream_for" do
@connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("action_cable:channel:stream_test:chat:Room#1-Campfire") }
channel = ChatChannel.new @connection, ""
channel.stream_for Room.new(1)
run_in_eventmachine do
EM.next_tick do
@connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("action_cable:channel:stream_test:chat:Room#1-Campfire") }
end

channel = ChatChannel.new @connection, ""
channel.stream_for Room.new(1)
end
end
end
20 changes: 9 additions & 11 deletions test/connection/authorization_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test_helper'
require 'stubs/test_server'

class ActionCable::Connection::AuthorizationTest < ActiveSupport::TestCase
class ActionCable::Connection::AuthorizationTest < ActionCable::TestCase
class Connection < ActionCable::Connection::Base
attr_reader :websocket

Expand All @@ -10,17 +10,15 @@ def connect
end
end

setup do
@server = TestServer.new

env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket'
@connection = Connection.new(@server, env)
end

test "unauthorized connection" do
@connection.websocket.expects(:close)
run_in_eventmachine do
server = TestServer.new
env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket'

@connection.process
@connection.send :on_open
connection = Connection.new(server, env)
connection.websocket.expects(:close)
connection.process
connection.send :on_open
end
end
end
110 changes: 74 additions & 36 deletions test/connection/base_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test_helper'
require 'stubs/test_server'

class ActionCable::Connection::BaseTest < ActiveSupport::TestCase
class ActionCable::Connection::BaseTest < ActionCable::TestCase
class Connection < ActionCable::Connection::Base
attr_reader :websocket, :subscriptions, :message_buffer, :connected

Expand All @@ -12,69 +12,107 @@ def connect
def disconnect
@connected = false
end

def send_async(method, *args)
# Bypass Celluloid
send method, *args
end
end

setup do
@server = TestServer.new
@server.config.allowed_request_origins = %w( http://rubyonrails.com )

env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket',
'HTTP_ORIGIN' => 'http://rubyonrails.com'

@connection = Connection.new(@server, env)
@response = @connection.process
end

test "making a connection with invalid headers" do
connection = ActionCable::Connection::Base.new(@server, Rack::MockRequest.env_for("/test"))
response = connection.process
assert_equal 404, response[0]
run_in_eventmachine do
connection = ActionCable::Connection::Base.new(@server, Rack::MockRequest.env_for("/test"))
response = connection.process
assert_equal 404, response[0]
end
end

test "websocket connection" do
assert @connection.websocket.possible?
assert @connection.websocket.alive?
run_in_eventmachine do
connection = open_connection
connection.process

assert connection.websocket.possible?
assert connection.websocket.alive?
end
end

test "rack response" do
assert_equal [ -1, {}, [] ], @response
run_in_eventmachine do
connection = open_connection
response = connection.process

assert_equal [ -1, {}, [] ], response
end
end

test "on connection open" do
assert ! @connection.connected

@connection.websocket.expects(:transmit).with(regexp_matches(/\_ping/))
@connection.message_buffer.expects(:process!)

@connection.send :on_open

assert_equal [ @connection ], @server.connections
assert @connection.connected
run_in_eventmachine do
connection = open_connection
connection.process

connection.websocket.expects(:transmit).with(regexp_matches(/\_ping/))
connection.message_buffer.expects(:process!)

# Allow EM to run on_open callback
EM.next_tick do
assert_equal [ connection ], @server.connections
assert connection.connected
end
end
end

test "on connection close" do
# Setup the connection
EventMachine.stubs(:add_periodic_timer).returns(true)
@connection.send :on_open
assert @connection.connected
run_in_eventmachine do
connection = open_connection
connection.process

# Setup the connection
EventMachine.stubs(:add_periodic_timer).returns(true)
connection.send :on_open
assert connection.connected

@connection.subscriptions.expects(:unsubscribe_from_all)
@connection.send :on_close
connection.subscriptions.expects(:unsubscribe_from_all)
connection.send :on_close

assert ! @connection.connected
assert_equal [], @server.connections
assert ! connection.connected
assert_equal [], @server.connections
end
end

test "connection statistics" do
statistics = @connection.statistics
run_in_eventmachine do
connection = open_connection
connection.process

statistics = connection.statistics

assert statistics[:identifier].blank?
assert_kind_of Time, statistics[:started_at]
assert_equal [], statistics[:subscriptions]
assert statistics[:identifier].blank?
assert_kind_of Time, statistics[:started_at]
assert_equal [], statistics[:subscriptions]
end
end

test "explicitly closing a connection" do
@connection.websocket.expects(:close)
@connection.close
run_in_eventmachine do
connection = open_connection
connection.process

connection.websocket.expects(:close)
connection.close
end
end

private
def open_connection
env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket',
'HTTP_ORIGIN' => 'http://rubyonrails.com'

Connection.new(@server, env)
end
end
17 changes: 15 additions & 2 deletions test/connection/cross_site_forgery_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
require 'test_helper'
require 'stubs/test_server'

class ActionCable::Connection::CrossSiteForgeryTest < ActiveSupport::TestCase
class ActionCable::Connection::CrossSiteForgeryTest < ActionCable::TestCase
HOST = 'rubyonrails.com'

class Connection < ActionCable::Connection::Base
def send_async(method, *args)
# Bypass Celluloid
send method, *args
end
end

setup do
@server = TestServer.new
@server.config.allowed_request_origins = %w( http://rubyonrails.com )
Expand Down Expand Up @@ -45,7 +52,13 @@ def assert_origin_not_allowed(origin)
end

def connect_with_origin(origin)
ActionCable::Connection::Base.new(@server, env_for_origin(origin)).process
response = nil

run_in_eventmachine do
response = Connection.new(@server, env_for_origin(origin)).process
end

response
end

def env_for_origin(origin)
Expand Down
70 changes: 35 additions & 35 deletions test/connection/identifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'stubs/test_server'
require 'stubs/user'

class ActionCable::Connection::IdentifierTest < ActiveSupport::TestCase
class ActionCable::Connection::IdentifierTest < ActionCable::TestCase
class Connection < ActionCable::Connection::Base
identified_by :current_user
attr_reader :websocket
Expand All @@ -14,59 +14,59 @@ def connect
end
end

setup do
@server = TestServer.new

env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket'
@connection = Connection.new(@server, env)
end

test "connection identifier" do
open_connection_with_stubbed_pubsub
assert_equal "User#lifo", @connection.connection_identifier
end

test "should subscribe to internal channel on open" do
pubsub = mock('pubsub')
pubsub.expects(:subscribe).with('action_cable/User#lifo')
@server.expects(:pubsub).returns(pubsub)

open_connection
run_in_eventmachine do
open_connection_with_stubbed_pubsub
assert_equal "User#lifo", @connection.connection_identifier
end
end

test "should unsubscribe from internal channel on close" do
open_connection_with_stubbed_pubsub
test "should subscribe to internal channel on open and unsubscribe on close" do
run_in_eventmachine do
pubsub = mock('pubsub')
pubsub.expects(:subscribe).with('action_cable/User#lifo')
pubsub.expects(:unsubscribe_proc).with('action_cable/User#lifo', kind_of(Proc))

pubsub = mock('pubsub')
pubsub.expects(:unsubscribe_proc).with('action_cable/User#lifo', kind_of(Proc))
@server.expects(:pubsub).returns(pubsub)
server = TestServer.new
server.stubs(:pubsub).returns(pubsub)

close_connection
open_connection server: server
close_connection
end
end

test "processing disconnect message" do
open_connection_with_stubbed_pubsub
run_in_eventmachine do
open_connection_with_stubbed_pubsub

@connection.websocket.expects(:close)
message = { 'type' => 'disconnect' }.to_json
@connection.process_internal_message message
@connection.websocket.expects(:close)
message = { 'type' => 'disconnect' }.to_json
@connection.process_internal_message message
end
end

test "processing invalid message" do
open_connection_with_stubbed_pubsub
run_in_eventmachine do
open_connection_with_stubbed_pubsub

@connection.websocket.expects(:close).never
message = { 'type' => 'unknown' }.to_json
@connection.process_internal_message message
@connection.websocket.expects(:close).never
message = { 'type' => 'unknown' }.to_json
@connection.process_internal_message message
end
end

protected
def open_connection_with_stubbed_pubsub
@server.stubs(:pubsub).returns(stub_everything('pubsub'))
open_connection
server = TestServer.new
server.stubs(:pubsub).returns(stub_everything('pubsub'))

open_connection server: server
end

def open_connection
def open_connection(server:)
env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket'
@connection = Connection.new(server, env)

@connection.process
@connection.send :on_open
end
Expand Down
Loading

0 comments on commit ee16ca8

Please sign in to comment.