Skip to content

Commit 11d4b68

Browse files
author
KJ Tsanaktsidis
authored
Add infrastructure for capturing Redis commands in tests (#304)
1 parent 8a3d03e commit 11d4b68

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

test/command_capture_middleware.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
module CommandCaptureMiddleware
4+
CapturedCommand = Struct.new(:server_url, :command, :pipelined, keyword_init: true) do
5+
def inspect
6+
"#<#{self.class.name} [on #{server_url}] #{command.join(' ')} >"
7+
end
8+
end
9+
10+
def call(command, redis_config)
11+
redis_config.custom[:captured_commands] << CapturedCommand.new(
12+
server_url: redis_config.server_url,
13+
command: command,
14+
pipelined: false
15+
)
16+
super
17+
end
18+
19+
def call_pipelined(commands, redis_config)
20+
commands.map do |command|
21+
redis_config.custom[:captured_commands] << CapturedCommand.new(
22+
server_url: redis_config.server_url,
23+
command: command,
24+
pipelined: true
25+
)
26+
end
27+
super
28+
end
29+
end

test/redis_client/test_cluster.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ class RedisClient
66
class TestCluster
77
module Mixin
88
def setup
9+
@captured_commands = []
910
@client = new_test_client
1011
@client.call('FLUSHDB')
1112
wait_for_replication
13+
@captured_commands.clear
1214
end
1315

1416
def teardown
@@ -516,10 +518,12 @@ def hiredis_used?
516518
class PrimaryOnly < TestingWrapper
517519
include Mixin
518520

519-
def new_test_client
521+
def new_test_client(capture_buffer: @captured_commands)
520522
config = ::RedisClient::ClusterConfig.new(
521523
nodes: TEST_NODE_URIS,
522524
fixed_hostname: TEST_FIXED_HOSTNAME,
525+
middlewares: [CommandCaptureMiddleware],
526+
custom: { captured_commands: capture_buffer },
523527
**TEST_GENERIC_OPTIONS
524528
)
525529
::RedisClient::Cluster.new(config)
@@ -529,12 +533,14 @@ def new_test_client
529533
class ScaleReadRandom < TestingWrapper
530534
include Mixin
531535

532-
def new_test_client
536+
def new_test_client(capture_buffer: @captured_commands)
533537
config = ::RedisClient::ClusterConfig.new(
534538
nodes: TEST_NODE_URIS,
535539
replica: true,
536540
replica_affinity: :random,
537541
fixed_hostname: TEST_FIXED_HOSTNAME,
542+
middlewares: [CommandCaptureMiddleware],
543+
custom: { captured_commands: capture_buffer },
538544
**TEST_GENERIC_OPTIONS
539545
)
540546
::RedisClient::Cluster.new(config)
@@ -544,12 +550,14 @@ def new_test_client
544550
class ScaleReadRandomWithPrimary < TestingWrapper
545551
include Mixin
546552

547-
def new_test_client
553+
def new_test_client(capture_buffer: @captured_commands)
548554
config = ::RedisClient::ClusterConfig.new(
549555
nodes: TEST_NODE_URIS,
550556
replica: true,
551557
replica_affinity: :random_with_primary,
552558
fixed_hostname: TEST_FIXED_HOSTNAME,
559+
middlewares: [CommandCaptureMiddleware],
560+
custom: { captured_commands: capture_buffer },
553561
**TEST_GENERIC_OPTIONS
554562
)
555563
::RedisClient::Cluster.new(config)
@@ -559,12 +567,14 @@ def new_test_client
559567
class ScaleReadLatency < TestingWrapper
560568
include Mixin
561569

562-
def new_test_client
570+
def new_test_client(capture_buffer: @captured_commands)
563571
config = ::RedisClient::ClusterConfig.new(
564572
nodes: TEST_NODE_URIS,
565573
replica: true,
566574
replica_affinity: :latency,
567575
fixed_hostname: TEST_FIXED_HOSTNAME,
576+
middlewares: [CommandCaptureMiddleware],
577+
custom: { captured_commands: capture_buffer },
568578
**TEST_GENERIC_OPTIONS
569579
)
570580
::RedisClient::Cluster.new(config)
@@ -574,10 +584,12 @@ def new_test_client
574584
class Pooled < TestingWrapper
575585
include Mixin
576586

577-
def new_test_client
587+
def new_test_client(capture_buffer: @captured_commands)
578588
config = ::RedisClient::ClusterConfig.new(
579589
nodes: TEST_NODE_URIS,
580590
fixed_hostname: TEST_FIXED_HOSTNAME,
591+
middlewares: [CommandCaptureMiddleware],
592+
custom: { captured_commands: capture_buffer },
581593
**TEST_GENERIC_OPTIONS
582594
)
583595
::RedisClient::Cluster.new(config, pool: { timeout: TEST_TIMEOUT_SEC, size: 2 })

test/testing_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'redis-cluster-client'
77
require 'testing_constants'
88
require 'cluster_controller'
9+
require 'command_capture_middleware'
910

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

0 commit comments

Comments
 (0)