1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actioncable/test/subscription_adapter/redis_test.rb
Sergey Ponomarev 723375147b Add ActionCable subscription connection identificator to PostgreSQL adapter
* You can distinguish connection among others with specific `application_name`

    ```sql
    SELECT application_name FROM pg_stat_activity;
    /*
        application_name
    ------------------------
    psql
    ActionCable-PID-42
    (2 rows)
    */
     ```

  * It's possible to customize connection identification with `id` option in `cable.yml`

    `ActionCable-PID-#{$$}` is the default value

  * Related tests refactoring

  * `ActionCable::Server#config.cable` is no mutated anymore inside Redis subscription adapter
2019-12-31 13:23:01 +03:00

82 lines
2 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
require_relative "common"
require_relative "channel_prefix"
require "action_cable/subscription_adapter/redis"
class RedisAdapterTest < ActionCable::TestCase
include CommonSubscriptionAdapterTest
include ChannelPrefixTest
def cable_config
{ adapter: "redis", driver: "ruby" }.tap do |x|
if host = URI(ENV["REDIS_URL"] || "").hostname
x[:host] = host
end
end
end
end
class RedisAdapterTest::Hiredis < RedisAdapterTest
def cable_config
super.merge(driver: "hiredis")
end
end
class RedisAdapterTest::AlternateConfiguration < RedisAdapterTest
def cable_config
alt_cable_config = super.dup
alt_cable_config.delete(:url)
alt_cable_config.merge(host: URI(ENV["REDIS_URL"] || "").hostname || "127.0.0.1", port: 6379, db: 12)
end
end
class RedisAdapterTest::ConnectorDefaultID < ActionCable::TestCase
def setup
server = ActionCable::Server::Base.new
server.config.cable = cable_config.merge(adapter: "redis").with_indifferent_access
server.config.logger = Logger.new(StringIO.new).tap { |l| l.level = Logger::UNKNOWN }
@adapter = server.config.pubsub_adapter.new(server)
end
def cable_config
{ url: 1, host: 2, port: 3, db: 4, password: 5 }
end
def connection_id
"ActionCable-PID-#{$$}"
end
def expected_connection
cable_config.merge(id: connection_id)
end
test "sets connection id for connection" do
assert_called_with ::Redis, :new, [ expected_connection.stringify_keys ] do
@adapter.send(:redis_connection)
end
end
end
class RedisAdapterTest::ConnectorCustomID < RedisAdapterTest::ConnectorDefaultID
def cable_config
super.merge(id: connection_id)
end
def connection_id
"Some custom ID"
end
end
class RedisAdapterTest::ConnectorWithExcluded < RedisAdapterTest::ConnectorDefaultID
def cable_config
super.merge(adapter: "redis", channel_prefix: "custom")
end
def expected_connection
super.except(:adapter, :channel_prefix)
end
end