Skip to content

Commit 18d2898

Browse files
authored
Add cluster state test (#38)
1 parent d4f85b7 commit 18d2898

15 files changed

+278
-122
lines changed

.github/workflows/test.yaml

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ jobs:
3333
strategy:
3434
fail-fast: false
3535
matrix:
36-
redis: ['6.2.7', '7.0.1']
36+
redis: ['7.0.1', '6.2.7']
3737
ruby: ['3.1', '3.0', '2.7']
3838
driver: ['ruby'] # FIXME: freaky hiredis
3939
docker: ['docker-compose.yaml', 'docker-compose.ssl.yaml']
40-
os: ['ubuntu-latest']
41-
runs-on: ${{ matrix.os }}
40+
runs-on: ubuntu-latest
4241
env:
4342
REDIS_VERSION: ${{ matrix.redis }}
4443
REDIS_CONNECTION_DRIVER: ${{ matrix.driver }}
@@ -93,3 +92,35 @@ jobs:
9392
run: bundle exec rake test
9493
- name: Stop containers
9594
run: docker compose -f $DOCKER_COMPOSE_FILE down
95+
cluster-state:
96+
name: Cluster State
97+
timeout-minutes: 30
98+
strategy:
99+
fail-fast: false
100+
matrix:
101+
redis: ['7.0.1', '6.2.7']
102+
runs-on: ubuntu-latest
103+
env:
104+
REDIS_VERSION: ${{ matrix.redis }}
105+
REDIS_REPLICA_SIZE: '2'
106+
DOCKER_COMPOSE_FILE: 'docker-compose.replica.yaml'
107+
steps:
108+
- name: Check out code
109+
uses: actions/checkout@v3
110+
- name: Set up Ruby
111+
uses: ruby/setup-ruby@v1
112+
with:
113+
ruby-version: '3.1'
114+
bundler-cache: true
115+
- name: Pull Docker images
116+
run: docker pull redis:$REDIS_VERSION
117+
- name: Run containers
118+
run: docker compose -f $DOCKER_COMPOSE_FILE up -d
119+
- name: Wait for Redis cluster to be ready
120+
run: bundle exec rake wait
121+
- name: Print containers
122+
run: docker compose -f $DOCKER_COMPOSE_FILE ps
123+
- name: Run minitest
124+
run: bundle exec rake test_cluster_state
125+
- name: Stop containers
126+
run: docker compose -f $DOCKER_COMPOSE_FILE down

Rakefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@ task default: :test
77
Rake::TestTask.new :test do |t|
88
t.libs << :test
99
t.libs << :lib
10-
t.test_files = ARGV.size == 1 ? Dir['test/**/test_*.rb'] : ARGV[1..]
10+
files = Dir['test/**/test_*.rb'].grep_v(/test_against_cluster_state/)
11+
files = ARGV[1..] if ARGV.size > 1
12+
t.test_files = files
13+
t.options = '-v'
14+
end
15+
16+
Rake::TestTask.new :test_cluster_state do |t|
17+
t.libs << :test
18+
t.libs << :lib
19+
t.test_files = %w[test/test_against_cluster_state.rb]
1120
t.options = '-v'
1221
end
1322

1423
desc 'Wait for cluster to be ready'
1524
task :wait do
1625
$LOAD_PATH.unshift(File.expand_path('test', __dir__))
17-
require 'constants'
26+
require 'testing_constants'
1827
require 'cluster_controller'
1928
::ClusterController.new(
2029
TEST_NODE_URIS,

lib/redis_client/cluster.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def empty?
4040
@size.zero?
4141
end
4242

43+
# TODO: https://github.com/redis-rb/redis-cluster-client/issues/37
4344
def execute # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
4445
all_replies = Array.new(@size)
4546
threads = @grouped.map do |k, v|
@@ -185,7 +186,7 @@ def send_command(method, *command, **kwargs, &block) # rubocop:disable Metrics/A
185186
@node.call_all(method, *command, **kwargs, &block).first
186187
when 'flushall', 'flushdb'
187188
@node.call_primary(method, *command, **kwargs, &block).first
188-
when 'wait' then @node.call_primary(method, *command, **kwargs, &block).sum
189+
when 'wait' then send_wait_command(method, *command, **kwargs, &block)
189190
when 'keys' then @node.call_replica(method, *command, **kwargs, &block).flatten.sort
190191
when 'dbsize' then @node.call_replica(method, *command, **kwargs, &block).sum
191192
when 'scan' then _scan(*command, **kwargs)
@@ -207,6 +208,16 @@ def send_command(method, *command, **kwargs, &block) # rubocop:disable Metrics/A
207208
end
208209
end
209210

211+
def send_wait_command(method, *command, retry_count: 3, **kwargs, &block)
212+
@node.call_primary(method, *command, **kwargs, &block).sum
213+
rescue RedisClient::Cluster::CommandErrorCollection => e
214+
raise if e.errors.values.map(&:message).grep(/ERR WAIT cannot be used with replica instances/).size.zero?
215+
216+
update_cluster_info!
217+
retry_count -= 1
218+
retry
219+
end
220+
210221
def send_config_command(method, *command, **kwargs, &block)
211222
case command[1].to_s.downcase
212223
when 'resetstat', 'rewrite', 'set'

0 commit comments

Comments
 (0)