Add Action Cable test adapter

This commit is contained in:
Vladimir Dementyev 2018-08-19 19:06:30 -04:00
parent 44007c0709
commit f7dd2d67d6
No known key found for this signature in database
GPG Key ID: 8E0A19D3D1EDF5EB
3 changed files with 88 additions and 0 deletions

@ -5,6 +5,7 @@ module SubscriptionAdapter
extend ActiveSupport::Autoload
autoload :Base
autoload :Test
autoload :SubscriberMap
autoload :ChannelPrefix
end

@ -0,0 +1,40 @@
# frozen_string_literal: true
require_relative "async"
module ActionCable
module SubscriptionAdapter
# == Test adapter for Action Cable
#
# The test adapter should be used only in testing. Along with
# <tt>ActionCable::TestHelper</tt> it makes a great tool to test your Rails application.
#
# To use the test adapter set adapter value to +test+ in your +cable.yml+.
#
# NOTE: Test adapter extends the <tt>ActionCable::SubscriptionsAdapter::Async</tt> adapter,
# so it could be used in system tests too.
class Test < Async
def broadcast(channel, payload)
broadcasts(channel) << payload
super
end
def broadcasts(channel)
channels_data[channel] ||= []
end
def clear_messages(channel)
channels_data[channel] = []
end
def clear
@channels_data = nil
end
private
def channels_data
@channels_data ||= {}
end
end
end
end

@ -0,0 +1,47 @@
# frozen_string_literal: true
require "test_helper"
require_relative "common"
class ActionCable::SubscriptionAdapter::TestTest < ActionCable::TestCase
include CommonSubscriptionAdapterTest
def setup
super
@tx_adapter.shutdown
@tx_adapter = @rx_adapter
end
def cable_config
{ adapter: "test" }
end
test "#broadcast stores messages for streams" do
@tx_adapter.broadcast("channel", "payload")
@tx_adapter.broadcast("channel2", "payload2")
assert_equal ["payload"], @tx_adapter.broadcasts("channel")
assert_equal ["payload2"], @tx_adapter.broadcasts("channel2")
end
test "#clear_messages deletes recorded broadcasts for the channel" do
@tx_adapter.broadcast("channel", "payload")
@tx_adapter.broadcast("channel2", "payload2")
@tx_adapter.clear_messages("channel")
assert_equal [], @tx_adapter.broadcasts("channel")
assert_equal ["payload2"], @tx_adapter.broadcasts("channel2")
end
test "#clear deletes all recorded broadcasts" do
@tx_adapter.broadcast("channel", "payload")
@tx_adapter.broadcast("channel2", "payload2")
@tx_adapter.clear
assert_equal [], @tx_adapter.broadcasts("channel")
assert_equal [], @tx_adapter.broadcasts("channel2")
end
end