From 1af531dcf7b8d9ee4237ea5fd392b43875309954 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 12 Jul 2015 10:07:31 -0500 Subject: [PATCH] Add some more tests --- lib/action_cable/channel/periodic_timers.rb | 2 +- .../{channel_test.rb => channel/base_test.rb} | 24 +---------- test/channel/periodic_timers_test.rb | 41 +++++++++++++++++++ test/channel/stream_test.rb | 25 +++++++++++ test/connection/base_test.rb | 17 ++++++++ test/stubs/test_connection.rb | 26 ++++++++++++ test/stubs/test_server.rb | 10 +++++ test/stubs/user.rb | 7 ++++ test/test_helper.rb | 3 +- 9 files changed, 130 insertions(+), 25 deletions(-) rename test/{channel_test.rb => channel/base_test.rb} (82%) create mode 100644 test/channel/periodic_timers_test.rb create mode 100644 test/channel/stream_test.rb create mode 100644 test/connection/base_test.rb create mode 100644 test/stubs/test_connection.rb create mode 100644 test/stubs/test_server.rb create mode 100644 test/stubs/user.rb diff --git a/lib/action_cable/channel/periodic_timers.rb b/lib/action_cable/channel/periodic_timers.rb index fea957563f..9bdcc87aa5 100644 --- a/lib/action_cable/channel/periodic_timers.rb +++ b/lib/action_cable/channel/periodic_timers.rb @@ -2,7 +2,7 @@ module ActionCable module Channel module PeriodicTimers extend ActiveSupport::Concern - + included do class_attribute :periodic_timers, instance_reader: false self.periodic_timers = [] diff --git a/test/channel_test.rb b/test/channel/base_test.rb similarity index 82% rename from test/channel_test.rb rename to test/channel/base_test.rb index 3f7847a12d..e525631642 100644 --- a/test/channel_test.rb +++ b/test/channel/base_test.rb @@ -1,28 +1,8 @@ require 'test_helper' +require 'stubs/test_connection' -class ChannelTest < ActiveSupport::TestCase +class ActionCable::Channel::BaseTest < ActiveSupport::TestCase Room = Struct.new(:id) - User = Struct.new(:name) - - class TestConnection - attr_reader :identifiers, :logger, :current_user, :transmissions - - def initialize(user) - @identifiers = [ :current_user ] - - @current_user = user - @logger = Logger.new(StringIO.new) - @transmissions = [] - end - - def transmit(data) - @transmissions << data - end - - def last_transmission - @transmissions.last - end - end class ChatChannel < ActionCable::Channel::Base attr_reader :room, :last_action diff --git a/test/channel/periodic_timers_test.rb b/test/channel/periodic_timers_test.rb new file mode 100644 index 0000000000..96d3e01783 --- /dev/null +++ b/test/channel/periodic_timers_test.rb @@ -0,0 +1,41 @@ +require 'test_helper' +require 'stubs/test_connection' + +class ActionCable::Channel::PeriodicTimersTest < ActiveSupport::TestCase + Room = Struct.new(:id) + + class ChatChannel < ActionCable::Channel::Base + periodically -> { ping }, every: 5 + periodically :send_updates, every: 1 + + private + def ping + end + end + + setup do + @connection = TestConnection.new + end + + test "periodic timers definition" do + timers = ChatChannel.periodic_timers + + assert_equal 2, timers.size + + first_timer = timers[0] + assert_kind_of Proc, first_timer[0] + assert_equal 5, first_timer[1][:every] + + second_timer = timers[1] + assert_equal :send_updates, second_timer[0] + assert_equal 1, second_timer[1][:every] + end + + test "timer start and stop" do + EventMachine::PeriodicTimer.expects(:new).times(2).returns(true) + channel = ChatChannel.new @connection, "{id: 1}", { id: 1 } + + channel.expects(:stop_periodic_timers).once + channel.unsubscribe_from_channel + end +end diff --git a/test/channel/stream_test.rb b/test/channel/stream_test.rb new file mode 100644 index 0000000000..2f4c988adf --- /dev/null +++ b/test/channel/stream_test.rb @@ -0,0 +1,25 @@ +require 'test_helper' +require 'stubs/test_connection' + +class ActionCable::Channel::StreamTest < ActiveSupport::TestCase + Room = Struct.new(:id) + + class ChatChannel < ActionCable::Channel::Base + def subscribed + @room = Room.new params[:id] + stream_from "test_room_#{@room.id}" + end + end + + setup do + @connection = TestConnection.new + end + + test "streaming start and stop" do + @connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe) } + channel = ChatChannel.new @connection, "{id: 1}", { id: 1 } + + @connection.expects(:pubsub).returns mock().tap { |m| m.expects(:unsubscribe_proc) } + channel.unsubscribe_from_channel + end +end diff --git a/test/connection/base_test.rb b/test/connection/base_test.rb new file mode 100644 index 0000000000..66e303a804 --- /dev/null +++ b/test/connection/base_test.rb @@ -0,0 +1,17 @@ +require 'test_helper' +require 'stubs/test_server' + +class ActionCable::Connection::BaseTest < ActiveSupport::TestCase + setup do + @server = TestServer.new + + env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' + @connection = ActionCable::Connection::Base.new(@server, env) + 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] + end +end diff --git a/test/stubs/test_connection.rb b/test/stubs/test_connection.rb new file mode 100644 index 0000000000..1c353f9ee3 --- /dev/null +++ b/test/stubs/test_connection.rb @@ -0,0 +1,26 @@ +require 'stubs/user' + +class TestConnection + attr_reader :identifiers, :logger, :current_user, :transmissions + + def initialize(user = User.new("lifo")) + @identifiers = [ :current_user ] + + @current_user = user + @logger = ActiveSupport::TaggedLogging.new ActiveSupport::Logger.new(StringIO.new) + @transmissions = [] + end + + def transmit(data) + @transmissions << data + end + + 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 new file mode 100644 index 0000000000..aec80859c3 --- /dev/null +++ b/test/stubs/test_server.rb @@ -0,0 +1,10 @@ +require 'ostruct' + +class TestServer + attr_reader :logger, :config + + def initialize + @logger = ActiveSupport::TaggedLogging.new ActiveSupport::Logger.new(StringIO.new) + @config = OpenStruct.new(log_tags: []) + end +end diff --git a/test/stubs/user.rb b/test/stubs/user.rb new file mode 100644 index 0000000000..af90007af7 --- /dev/null +++ b/test/stubs/user.rb @@ -0,0 +1,7 @@ +class User + attr_reader :name + + def initialize(name) + @name = name + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index f3b994a9fc..5b8ba110db 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,12 +4,11 @@ gem 'minitest' require "minitest/autorun" -require 'mocha/mini_test' - Bundler.setup Bundler.require :default, :test require 'puma' +require 'mocha/mini_test' require 'action_cable' ActiveSupport.test_order = :sorted -- GitLab