mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add some more tests
This commit is contained in:
parent
53c82f6dc0
commit
1af531dcf7
9 changed files with 130 additions and 25 deletions
|
@ -1,28 +1,8 @@
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
require 'stubs/test_connection'
|
||||||
|
|
||||||
class ChannelTest < ActiveSupport::TestCase
|
class ActionCable::Channel::BaseTest < ActiveSupport::TestCase
|
||||||
Room = Struct.new(:id)
|
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
|
class ChatChannel < ActionCable::Channel::Base
|
||||||
attr_reader :room, :last_action
|
attr_reader :room, :last_action
|
41
test/channel/periodic_timers_test.rb
Normal file
41
test/channel/periodic_timers_test.rb
Normal file
|
@ -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
|
25
test/channel/stream_test.rb
Normal file
25
test/channel/stream_test.rb
Normal file
|
@ -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
|
17
test/connection/base_test.rb
Normal file
17
test/connection/base_test.rb
Normal file
|
@ -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
|
26
test/stubs/test_connection.rb
Normal file
26
test/stubs/test_connection.rb
Normal file
|
@ -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
|
10
test/stubs/test_server.rb
Normal file
10
test/stubs/test_server.rb
Normal file
|
@ -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
|
7
test/stubs/user.rb
Normal file
7
test/stubs/user.rb
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
class User
|
||||||
|
attr_reader :name
|
||||||
|
|
||||||
|
def initialize(name)
|
||||||
|
@name = name
|
||||||
|
end
|
||||||
|
end
|
|
@ -4,12 +4,11 @@ require "bundler"
|
||||||
gem 'minitest'
|
gem 'minitest'
|
||||||
require "minitest/autorun"
|
require "minitest/autorun"
|
||||||
|
|
||||||
require 'mocha/mini_test'
|
|
||||||
|
|
||||||
Bundler.setup
|
Bundler.setup
|
||||||
Bundler.require :default, :test
|
Bundler.require :default, :test
|
||||||
|
|
||||||
require 'puma'
|
require 'puma'
|
||||||
|
require 'mocha/mini_test'
|
||||||
|
|
||||||
require 'action_cable'
|
require 'action_cable'
|
||||||
ActiveSupport.test_order = :sorted
|
ActiveSupport.test_order = :sorted
|
||||||
|
|
Loading…
Reference in a new issue