Skip to content

Add infrastructure for capturing Redis commands in tests #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions test/command_capture_middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module CommandCaptureMiddleware
CapturedCommand = Struct.new(:server_url, :command, :pipelined, keyword_init: true) do
def inspect
"#<#{self.class.name} [on #{server_url}] #{command.join(' ')} >"
end
end

def call(command, redis_config)
redis_config.custom[:captured_commands] << CapturedCommand.new(
server_url: redis_config.server_url,
command: command,
pipelined: false
)
super
end

def call_pipelined(commands, redis_config)
commands.map do |command|
redis_config.custom[:captured_commands] << CapturedCommand.new(
server_url: redis_config.server_url,
command: command,
pipelined: true
)
end
super
end
end
22 changes: 17 additions & 5 deletions test/redis_client/test_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ class RedisClient
class TestCluster
module Mixin
def setup
@captured_commands = []
@client = new_test_client
@client.call('FLUSHDB')
wait_for_replication
@captured_commands.clear
end

def teardown
Expand Down Expand Up @@ -516,10 +518,12 @@ def hiredis_used?
class PrimaryOnly < TestingWrapper
include Mixin

def new_test_client
def new_test_client(capture_buffer: @captured_commands)
config = ::RedisClient::ClusterConfig.new(
nodes: TEST_NODE_URIS,
fixed_hostname: TEST_FIXED_HOSTNAME,
middlewares: [CommandCaptureMiddleware],
custom: { captured_commands: capture_buffer },
**TEST_GENERIC_OPTIONS
)
::RedisClient::Cluster.new(config)
Expand All @@ -529,12 +533,14 @@ def new_test_client
class ScaleReadRandom < TestingWrapper
include Mixin

def new_test_client
def new_test_client(capture_buffer: @captured_commands)
config = ::RedisClient::ClusterConfig.new(
nodes: TEST_NODE_URIS,
replica: true,
replica_affinity: :random,
fixed_hostname: TEST_FIXED_HOSTNAME,
middlewares: [CommandCaptureMiddleware],
custom: { captured_commands: capture_buffer },
**TEST_GENERIC_OPTIONS
)
::RedisClient::Cluster.new(config)
Expand All @@ -544,12 +550,14 @@ def new_test_client
class ScaleReadRandomWithPrimary < TestingWrapper
include Mixin

def new_test_client
def new_test_client(capture_buffer: @captured_commands)
config = ::RedisClient::ClusterConfig.new(
nodes: TEST_NODE_URIS,
replica: true,
replica_affinity: :random_with_primary,
fixed_hostname: TEST_FIXED_HOSTNAME,
middlewares: [CommandCaptureMiddleware],
custom: { captured_commands: capture_buffer },
**TEST_GENERIC_OPTIONS
)
::RedisClient::Cluster.new(config)
Expand All @@ -559,12 +567,14 @@ def new_test_client
class ScaleReadLatency < TestingWrapper
include Mixin

def new_test_client
def new_test_client(capture_buffer: @captured_commands)
config = ::RedisClient::ClusterConfig.new(
nodes: TEST_NODE_URIS,
replica: true,
replica_affinity: :latency,
fixed_hostname: TEST_FIXED_HOSTNAME,
middlewares: [CommandCaptureMiddleware],
custom: { captured_commands: capture_buffer },
**TEST_GENERIC_OPTIONS
)
::RedisClient::Cluster.new(config)
Expand All @@ -574,10 +584,12 @@ def new_test_client
class Pooled < TestingWrapper
include Mixin

def new_test_client
def new_test_client(capture_buffer: @captured_commands)
config = ::RedisClient::ClusterConfig.new(
nodes: TEST_NODE_URIS,
fixed_hostname: TEST_FIXED_HOSTNAME,
middlewares: [CommandCaptureMiddleware],
custom: { captured_commands: capture_buffer },
**TEST_GENERIC_OPTIONS
)
::RedisClient::Cluster.new(config, pool: { timeout: TEST_TIMEOUT_SEC, size: 2 })
Expand Down
1 change: 1 addition & 0 deletions test/testing_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'redis-cluster-client'
require 'testing_constants'
require 'cluster_controller'
require 'command_capture_middleware'

case ENV.fetch('REDIS_CONNECTION_DRIVER', 'ruby')
when 'hiredis' then require 'hiredis-client'
Expand Down