From f7dd2d67d6a8b74f2762be51dff4f96983175aee Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Sun, 19 Aug 2018 19:06:30 -0400 Subject: Add Action Cable test adapter --- .../lib/action_cable/subscription_adapter.rb | 1 + .../lib/action_cable/subscription_adapter/test.rb | 40 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 actioncable/lib/action_cable/subscription_adapter/test.rb (limited to 'actioncable/lib/action_cable') diff --git a/actioncable/lib/action_cable/subscription_adapter.rb b/actioncable/lib/action_cable/subscription_adapter.rb index bcece8d33b..6a9d5c2080 100644 --- a/actioncable/lib/action_cable/subscription_adapter.rb +++ b/actioncable/lib/action_cable/subscription_adapter.rb @@ -5,6 +5,7 @@ module ActionCable extend ActiveSupport::Autoload autoload :Base + autoload :Test autoload :SubscriberMap autoload :ChannelPrefix end diff --git a/actioncable/lib/action_cable/subscription_adapter/test.rb b/actioncable/lib/action_cable/subscription_adapter/test.rb new file mode 100644 index 0000000000..52226a7c71 --- /dev/null +++ b/actioncable/lib/action_cable/subscription_adapter/test.rb @@ -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 + # ActionCable::TestHelper 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 ActionCable::SubscriptionsAdapter::Async 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 -- cgit v1.2.3