Skip to content

Commit ca3d5c8

Browse files
authored
test: test against legacy redis for redis-clustering gem (#119)
1 parent 6ffd832 commit ca3d5c8

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

.github/workflows/test.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,35 @@ jobs:
9797
run: bundle exec rake test
9898
- name: Stop containers
9999
run: docker compose -f $DOCKER_COMPOSE_FILE down
100+
legacy-redis:
101+
name: Legacy Redis
102+
timeout-minutes: 15
103+
strategy:
104+
fail-fast: false
105+
runs-on: ubuntu-latest
106+
env:
107+
REDIS_VERSION: '5.0.14'
108+
DOCKER_COMPOSE_FILE: 'compose.yaml'
109+
steps:
110+
- name: Check out code
111+
uses: actions/checkout@v3
112+
- name: Set up Ruby
113+
uses: ruby/setup-ruby@v1
114+
with:
115+
ruby-version: '3.1'
116+
bundler-cache: true
117+
- name: Pull Docker images
118+
run: docker pull redis:$REDIS_VERSION
119+
- name: Run containers
120+
run: docker compose -f $DOCKER_COMPOSE_FILE up -d
121+
- name: Wait for Redis cluster to be ready
122+
run: bundle exec rake wait
123+
- name: Print containers
124+
run: docker compose -f $DOCKER_COMPOSE_FILE ps
125+
- name: Run minitest
126+
run: bundle exec rake test
127+
- name: Stop containers
128+
run: docker compose -f $DOCKER_COMPOSE_FILE down
100129
cluster-state:
101130
name: Cluster State
102131
timeout-minutes: 15

test/redis_client/test_cluster.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def test_blocking_call
6363
@client.call(*%w[RPUSH foo hello])
6464
@client.call(*%w[RPUSH foo world])
6565
wait_for_replication
66-
client_side_timeout = 1.0
67-
server_side_timeout = 0.5
66+
client_side_timeout = TEST_REDIS_MAJOR_VERSION < 6 ? 1.5 : 1.0
67+
server_side_timeout = TEST_REDIS_MAJOR_VERSION < 6 ? '1' : '0.5'
6868
assert_equal(%w[foo world], @client.blocking_call(client_side_timeout, 'BRPOP', 'foo', server_side_timeout), 'Case: 1st')
6969
assert_equal(%w[foo hello], @client.blocking_call(client_side_timeout, 'BRPOP', 'foo', server_side_timeout), 'Case: 2nd')
7070
assert_nil(@client.blocking_call(client_side_timeout, 'BRPOP', 'foo', server_side_timeout), 'Case: 3rd')
@@ -129,10 +129,12 @@ def test_pipelined
129129
wait_for_replication
130130

131131
want = %w[PONG] + (0..9).map(&:to_s) + [%w[list 2]]
132+
client_side_timeout = TEST_REDIS_MAJOR_VERSION < 6 ? 1.5 : 1.0
133+
server_side_timeout = TEST_REDIS_MAJOR_VERSION < 6 ? '1' : '0.5'
132134
got = @client.pipelined do |pipeline|
133135
pipeline.call_once('PING')
134136
10.times { |i| pipeline.call('GET', "string#{i}") }
135-
pipeline.blocking_call(0.2, 'BRPOP', 'list', '0.1')
137+
pipeline.blocking_call(client_side_timeout, 'BRPOP', 'list', server_side_timeout)
136138
end
137139
assert_equal(want, got)
138140
end
@@ -162,22 +164,22 @@ def test_close
162164
assert_nil(@client.close)
163165
end
164166

165-
def test_dedicated_commands
167+
def test_dedicated_commands # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
166168
10.times { |i| @client.call('SET', "key#{i}", i) }
167169
wait_for_replication
168170
[
169-
{ command: %w[ACL HELP], is_a: Array },
171+
{ command: %w[ACL HELP], is_a: Array, supported_redis_version: 6 },
170172
{ command: ['WAIT', TEST_REPLICA_SIZE, '1'], is_a: Integer },
171173
{ command: %w[KEYS *], want: (0..9).map { |i| "key#{i}" } },
172174
{ command: %w[DBSIZE], want: (0..9).size },
173175
{ command: %w[SCAN], is_a: Array },
174176
{ command: %w[LASTSAVE], is_a: Array },
175177
{ command: %w[ROLE], is_a: Array },
176178
{ command: %w[CONFIG RESETSTAT], want: 'OK' },
177-
{ command: %w[CONFIG GET maxmemory], is_a: Hash },
179+
{ command: %w[CONFIG GET maxmemory], is_a: TEST_REDIS_MAJOR_VERSION < 6 ? Array : Hash },
178180
{ command: %w[CLIENT LIST], is_a: Array },
179181
{ command: %w[CLIENT PAUSE 100], want: 'OK' },
180-
{ command: %w[CLIENT INFO], is_a: String },
182+
{ command: %w[CLIENT INFO], is_a: String, supported_redis_version: 6 },
181183
{ command: %w[CLUSTER SET-CONFIG-EPOCH 0], error: ::RedisClient::Cluster::OrchestrationCommandNotSupported },
182184
{ command: %w[CLUSTER SAVECONFIG], want: 'OK' },
183185
{ command: %w[CLUSTER GETKEYSINSLOT 13252 1], want: %w[key0] },
@@ -196,6 +198,8 @@ def test_dedicated_commands
196198
{ command: %w[MULTI], error: ::RedisClient::Cluster::AmbiguousNodeError },
197199
{ command: %w[FLUSHDB], want: 'OK' }
198200
].each do |c|
201+
next if c.key?(:supported_redis_version) && TEST_REDIS_MAJOR_VERSION < c[:supported_redis_version]
202+
199203
msg = "Case: #{c[:command].join(' ')}"
200204
got = -> { @client.call(*c[:command]) }
201205
if c.key?(:error)

test/testing_constants.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,29 @@
2121
_base_opts = {
2222
timeout: TEST_TIMEOUT_SEC,
2323
reconnect_attempts: TEST_RECONNECT_ATTEMPTS
24-
}.freeze
24+
}
2525

2626
_ssl_opts = {
2727
ssl: true,
2828
ssl_params: TEST_SSL_PARAMS
2929
}.freeze
3030

31+
_redis_scheme = 'redis'
32+
3133
begin
32-
_new_raw_cli.call(**_base_opts).call('PING')
33-
TEST_REDIS_SCHEME = 'redis'
34+
_tmp_cli = _new_raw_cli.call(**_base_opts)
35+
_tmp_cli.call('PING')
36+
rescue ::RedisClient::UnsupportedServer
37+
_base_opts.merge!(protocol: 2)
3438
rescue ::RedisClient::ConnectionError => e
3539
raise e if e.message != 'Connection reset by peer'
3640

37-
TEST_REDIS_SCHEME = 'rediss'
41+
_redis_scheme = 'rediss'
42+
ensure
43+
_tmp_cli&.close
3844
end
3945

46+
TEST_REDIS_SCHEME = _redis_scheme
4047
TEST_REDIS_SSL = TEST_REDIS_SCHEME == 'rediss'
4148
TEST_FIXED_HOSTNAME = TEST_REDIS_SSL ? TEST_REDIS_HOST : nil
4249

@@ -49,6 +56,11 @@
4956
TEST_NODE_URIS = TEST_REDIS_PORTS.map { |v| "#{TEST_REDIS_SCHEME}://#{TEST_REDIS_HOST}:#{v}" }.freeze
5057
TEST_NODE_OPTIONS = TEST_REDIS_PORTS.to_h { |v| ["#{TEST_REDIS_HOST}:#{v}", { host: TEST_REDIS_HOST, port: v }] }.freeze
5158

52-
TEST_GENERIC_OPTIONS = TEST_REDIS_SSL ? _base_opts.merge(_ssl_opts).freeze : _base_opts
59+
TEST_GENERIC_OPTIONS = (TEST_REDIS_SSL ? _base_opts.merge(_ssl_opts) : _base_opts).freeze
60+
61+
_tmp_cli = _new_raw_cli.call(**TEST_GENERIC_OPTIONS)
62+
TEST_REDIS_VERSION = _tmp_cli.call('INFO', 'SERVER').split("\r\n").grep(/redis_version.+/).first.split(':')[1]
63+
TEST_REDIS_MAJOR_VERSION = Integer(TEST_REDIS_VERSION.split('.').first)
64+
_tmp_cli.close
5365

5466
# rubocop:enable Lint/UnderscorePrefixedVariableName

0 commit comments

Comments
 (0)