diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2015-07-12 11:44:46 -0500 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2015-07-12 11:44:56 -0500 |
commit | edc68d7bf63bb40d440213a449031c1f07a1f95f (patch) | |
tree | ff5eeb3c93cb4849ad4092e9336ba711c736b6b2 | |
parent | 1af531dcf7b8d9ee4237ea5fd392b43875309954 (diff) | |
download | rails-edc68d7bf63bb40d440213a449031c1f07a1f95f.tar.gz rails-edc68d7bf63bb40d440213a449031c1f07a1f95f.tar.bz2 rails-edc68d7bf63bb40d440213a449031c1f07a1f95f.zip |
More connection tests
-rw-r--r-- | test/connection/base_test.rb | 64 | ||||
-rw-r--r-- | test/stubs/test_connection.rb | 5 | ||||
-rw-r--r-- | test/stubs/test_server.rb | 2 |
3 files changed, 65 insertions, 6 deletions
diff --git a/test/connection/base_test.rb b/test/connection/base_test.rb index 66e303a804..2f008652ee 100644 --- a/test/connection/base_test.rb +++ b/test/connection/base_test.rb @@ -2,11 +2,24 @@ require 'test_helper' require 'stubs/test_server' class ActionCable::Connection::BaseTest < ActiveSupport::TestCase + class Connection < ActionCable::Connection::Base + attr_reader :websocket, :heartbeat, :subscriptions, :message_buffer, :connected + + def connect + @connected = true + end + + def disconnect + @connected = false + end + end + setup do @server = TestServer.new env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' - @connection = ActionCable::Connection::Base.new(@server, env) + @connection = Connection.new(@server, env) + @response = @connection.process end test "making a connection with invalid headers" do @@ -14,4 +27,53 @@ class ActionCable::Connection::BaseTest < ActiveSupport::TestCase response = connection.process assert_equal 404, response[0] end + + test "websocket connection" do + assert @connection.websocket.possible? + assert @connection.websocket.alive? + end + + test "rack response" do + assert_equal [ -1, {}, [] ], @response + end + + test "on connection open" do + assert ! @connection.connected + + EventMachine.expects(:add_periodic_timer) + @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 + end + + test "on connection close" do + # Setup the connection + EventMachine.stubs(:add_periodic_timer).returns(true) + @connection.send :on_open + assert @connection.connected + + EventMachine.expects(:cancel_timer) + @connection.subscriptions.expects(:unsubscribe_from_all) + @connection.send :on_close + + assert ! @connection.connected + assert_equal [], @server.connections + end + + test "connection statistics" do + statistics = @connection.statistics + + assert statistics[:identifier].blank? + assert_kind_of Time, statistics[:started_at] + assert_equal [], statistics[:subscriptions] + end + + test "explicitly closing a connection" do + @connection.websocket.expects(:close) + @connection.close + end end diff --git a/test/stubs/test_connection.rb b/test/stubs/test_connection.rb index 1c353f9ee3..384abc5e76 100644 --- a/test/stubs/test_connection.rb +++ b/test/stubs/test_connection.rb @@ -18,9 +18,4 @@ class TestConnection def last_transmission @transmissions.last end - - # Disable async in tests - def send_async(method, *arguments) - send method, *arguments - end end diff --git a/test/stubs/test_server.rb b/test/stubs/test_server.rb index aec80859c3..2a7ac3e927 100644 --- a/test/stubs/test_server.rb +++ b/test/stubs/test_server.rb @@ -1,6 +1,8 @@ require 'ostruct' class TestServer + include ActionCable::Server::Connections + attr_reader :logger, :config def initialize |