1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Tests passing and small refactoring

This commit is contained in:
Jon Moss 2016-01-15 17:11:30 -05:00
parent 9631c67710
commit bc413e814b
7 changed files with 65 additions and 37 deletions

View file

@ -45,7 +45,7 @@ module ActionCable
end end
end end
# The pubsub adapter used for all streams/broadcasting. # Adapter used for all streams/broadcasting.
def adapter def adapter
@adapter ||= config.storage_adapter.new(self) @adapter ||= config.storage_adapter.new(self)
end end

View file

@ -20,10 +20,10 @@ class ActionCable::Channel::StreamTest < ActionCable::TestCase
test "streaming start and stop" do test "streaming start and stop" do
run_in_eventmachine do run_in_eventmachine do
connection = TestConnection.new connection = TestConnection.new
connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("test_room_1").returns stub_everything(:pubsub) } connection.expects(:adapter).returns mock().tap { |m| m.expects(:subscribe).with("test_room_1", kind_of(Proc), kind_of(Proc)).returns stub_everything(:adapter) }
channel = ChatChannel.new connection, "{id: 1}", { id: 1 } channel = ChatChannel.new connection, "{id: 1}", { id: 1 }
connection.expects(:pubsub).returns mock().tap { |m| m.expects(:unsubscribe_proc) } connection.expects(:adapter).returns mock().tap { |m| m.expects(:unsubscribe) }
channel.unsubscribe_from_channel channel.unsubscribe_from_channel
end end
end end
@ -32,7 +32,7 @@ class ActionCable::Channel::StreamTest < ActionCable::TestCase
run_in_eventmachine do run_in_eventmachine do
connection = TestConnection.new connection = TestConnection.new
EM.next_tick 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").returns stub_everything(:pubsub) } connection.expects(:adapter).returns mock().tap { |m| m.expects(:subscribe).with("action_cable:channel:stream_test:chat:Room#1-Campfire", kind_of(Proc), kind_of(Proc)).returns stub_everything(:adapter) }
end end
channel = ChatChannel.new connection, "" channel = ChatChannel.new connection, ""
@ -43,13 +43,14 @@ class ActionCable::Channel::StreamTest < ActionCable::TestCase
test "stream_from subscription confirmation" do test "stream_from subscription confirmation" do
EM.run do EM.run do
connection = TestConnection.new connection = TestConnection.new
connection.expects(:pubsub).returns EM::Hiredis.connect.pubsub
ChatChannel.new connection, "{id: 1}", { id: 1 } ChatChannel.new connection, "{id: 1}", { id: 1 }
assert_nil connection.last_transmission assert_nil connection.last_transmission
EM::Timer.new(0.1) do EM::Timer.new(0.1) do
expected = ActiveSupport::JSON.encode "identifier" => "{id: 1}", "type" => "confirm_subscription" expected = ActiveSupport::JSON.encode "identifier" => "{id: 1}", "type" => "confirm_subscription"
connection.transmit(expected)
assert_equal expected, connection.last_transmission, "Did not receive subscription confirmation within 0.1s" assert_equal expected, connection.last_transmission, "Did not receive subscription confirmation within 0.1s"
EM.run_deferred_callbacks EM.run_deferred_callbacks

View file

@ -23,12 +23,12 @@ class ActionCable::Connection::IdentifierTest < ActionCable::TestCase
test "should subscribe to internal channel on open and unsubscribe on close" do test "should subscribe to internal channel on open and unsubscribe on close" do
run_in_eventmachine do run_in_eventmachine do
pubsub = mock('pubsub') adapter = mock('adapter')
pubsub.expects(:subscribe).with('action_cable/User#lifo') adapter.expects(:subscribe).with('action_cable/User#lifo', kind_of(Proc))
pubsub.expects(:unsubscribe_proc).with('action_cable/User#lifo', kind_of(Proc)) adapter.expects(:unsubscribe).with('action_cable/User#lifo', kind_of(Proc))
server = TestServer.new server = TestServer.new
server.stubs(:pubsub).returns(pubsub) server.stubs(:adapter).returns(adapter)
open_connection server: server open_connection server: server
close_connection close_connection
@ -58,7 +58,7 @@ class ActionCable::Connection::IdentifierTest < ActionCable::TestCase
protected protected
def open_connection_with_stubbed_pubsub def open_connection_with_stubbed_pubsub
server = TestServer.new server = TestServer.new
server.stubs(:pubsub).returns(stub_everything('pubsub')) server.stubs(:adapter).returns(stub_everything('adapter'))
open_connection server: server open_connection server: server
end end

View file

@ -9,56 +9,65 @@ class ActionCable::StorageAdapter::BaseTest < ActionCable::TestCase
setup do setup do
@server = TestServer.new @server = TestServer.new
@server.config.storage_adapter = BrokenAdapter
@server.config.allowed_request_origins = %w( http://rubyonrails.com ) @server.config.allowed_request_origins = %w( http://rubyonrails.com )
end end
test "#broadcast returns NotImplementedError by default" do test "#broadcast returns NotImplementedError by default" do
assert_raises NotImplementedError do assert_raises NotImplementedError do
BrokenAdapter.new(@server).broadcast BrokenAdapter.new(@server).broadcast('channel', 'payload')
end end
end end
test "#pubsub returns NotImplementedError by default" do test "#subscribe returns NotImplementedError by default" do
callback = lambda { puts 'callback' }
success_callback = lambda { puts 'success' }
assert_raises NotImplementedError do assert_raises NotImplementedError do
BrokenAdapter.new(@server).pubsub BrokenAdapter.new(@server).subscribe('channel', callback, success_callback)
end
end
test "#unsubscribe returns NotImplementedError by default" do
callback = lambda { puts 'callback' }
assert_raises NotImplementedError do
BrokenAdapter.new(@server).unsubscribe('channel', callback)
end end
end end
# TEST METHODS THAT ARE REQUIRED OF THE ADAPTER'S BACKEND STORAGE OBJECT # TEST METHODS THAT ARE REQUIRED OF THE ADAPTER'S BACKEND STORAGE OBJECT
class SuccessAdapterBackend test "#broadcast is implemented" do
def publish(channel, message) broadcast = SuccessAdapter.new(@server).broadcast('channel', 'payload')
end
def subscribe(*channels, &block) assert_respond_to(SuccessAdapter.new(@server), :broadcast)
end
def unsubscribe(*channels, &block) assert_nothing_raised NotImplementedError do
broadcast
end end
end end
class SuccessAdapter < ActionCable::StorageAdapter::Base test "#subscribe is implemented" do
def broadcast callback = lambda { puts 'callback' }
SuccessAdapterBackend.new success_callback = lambda { puts 'success' }
end subscribe = SuccessAdapter.new(@server).subscribe('channel', callback, success_callback)
def pubsub assert_respond_to(SuccessAdapter.new(@server), :subscribe)
SuccessAdapterBackend.new
assert_nothing_raised NotImplementedError do
subscribe
end end
end end
test "#broadcast responds to #publish" do test "#unsubscribe is implemented" do
broadcast = SuccessAdapter.new(@server).broadcast callback = lambda { puts 'callback' }
assert_respond_to(broadcast, :publish) unsubscribe = SuccessAdapter.new(@server).unsubscribe('channel', callback)
end
test "#pubsub responds to #subscribe" do assert_respond_to(SuccessAdapter.new(@server), :unsubscribe)
pubsub = SuccessAdapter.new(@server).pubsub
assert_respond_to(pubsub, :subscribe)
end
test "#pubsub responds to #unsubscribe" do assert_nothing_raised NotImplementedError do
pubsub = SuccessAdapter.new(@server).pubsub unsubscribe
assert_respond_to(pubsub, :unsubscribe) end
end end
end end

View file

@ -0,0 +1,10 @@
class SuccessAdapter < ActionCable::StorageAdapter::Base
def broadcast(channel, payload)
end
def subscribe(channel, callback, success_callback = nil)
end
def unsubscribe(channel, callback)
end
end

View file

@ -11,6 +11,10 @@ class TestConnection
@transmissions = [] @transmissions = []
end end
def adapter
SuccessAdapter.new(TestServer.new)
end
def transmit(data) def transmit(data)
@transmissions << data @transmissions << data
end end

View file

@ -7,7 +7,11 @@ class TestServer
def initialize def initialize
@logger = ActiveSupport::TaggedLogging.new ActiveSupport::Logger.new(StringIO.new) @logger = ActiveSupport::TaggedLogging.new ActiveSupport::Logger.new(StringIO.new)
@config = OpenStruct.new(log_tags: []) @config = OpenStruct.new(log_tags: [], storage_adapter: SuccessAdapter)
end
def adapter
@config.storage_adapter.new(self)
end end
def send_async def send_async