Add Action Cable test adapter
This commit is contained in:
parent
44007c0709
commit
f7dd2d67d6
@ -5,6 +5,7 @@ module SubscriptionAdapter
|
||||
extend ActiveSupport::Autoload
|
||||
|
||||
autoload :Base
|
||||
autoload :Test
|
||||
autoload :SubscriberMap
|
||||
autoload :ChannelPrefix
|
||||
end
|
||||
|
40
actioncable/lib/action_cable/subscription_adapter/test.rb
Normal file
40
actioncable/lib/action_cable/subscription_adapter/test.rb
Normal file
@ -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
|
47
actioncable/test/subscription_adapter/test_adapter_test.rb
Normal file
47
actioncable/test/subscription_adapter/test_adapter_test.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user