Skip to content

Commit f455e60

Browse files
authored
Add some test cases (#20)
1 parent 1ac9352 commit f455e60

File tree

12 files changed

+484
-354
lines changed

12 files changed

+484
-354
lines changed

Rakefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ end
1414
desc 'Wait for cluster to be ready'
1515
task :wait do
1616
$LOAD_PATH.unshift(File.expand_path('test', __dir__))
17-
require 'redis_client/cluster/controller'
18-
nodes = (6379..6384).map { |port| "#{ENV.fetch('REDIS_SCHEME', 'redis')}://127.0.0.1:#{port}" }
19-
ctrl = ::RedisClient::Cluster::Controller.new(nodes)
20-
ctrl.wait_for_cluster_to_be_ready
17+
require 'testing_helper'
18+
::TestingHelper::ClusterController.new(
19+
::TestingHelper::TEST_NODE_URIS,
20+
**::TestingHelper::TEST_GENERIC_OPTIONS
21+
).wait_for_cluster_to_be_ready
2122
end

lib/redis_client/cluster.rb

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class RedisClient
1010
class Cluster
11+
ZERO_CURSOR_FOR_SCAN = '0'
12+
1113
def initialize(config, pool: nil, **kwargs)
1214
@config = config.dup
1315
@pool = pool
@@ -34,7 +36,14 @@ def blocking_call(timeout, *command, **kwargs, &block)
3436
end
3537

3638
def scan(*args, **kwargs, &block)
37-
_scan(:scan, *args, **kwargs, &block)
39+
raise ArgumentError, 'block required' unless block
40+
41+
cursor = ZERO_CURSOR_FOR_SCAN
42+
loop do
43+
cursor, keys = _scan('SCAN', cursor, *args, **kwargs)
44+
keys.each(&block)
45+
break if cursor == ZERO_CURSOR_FOR_SCAN
46+
end
3847
end
3948

4049
def sscan(key, *args, **kwargs, &block)
@@ -83,32 +92,6 @@ def connected?
8392

8493
def close
8594
@node.each(&:close)
86-
true
87-
end
88-
89-
# TODO: remove
90-
def call_pipeline(pipeline)
91-
node_keys = pipeline.commands.filter_map { |cmd| find_node_key(cmd, primary_only: true) }.uniq
92-
if node_keys.size > 1
93-
raise(CrossSlotPipeliningError,
94-
pipeline.commands.map { |cmd| @command.extract_first_key(cmd) }.reject(&:empty?).uniq)
95-
end
96-
97-
try_send(find_node(node_keys.first), :call_pipeline, pipeline)
98-
end
99-
100-
# TODO: remove
101-
def process(commands, &block)
102-
if commands.size == 1 &&
103-
%w[unsubscribe punsubscribe].include?(commands.first.first.to_s.downcase) &&
104-
commands.first.size == 1
105-
106-
# Node is indeterminate. We do just a best-effort try here.
107-
@node.process_all(commands, &block)
108-
else
109-
node = assign_node(commands.first)
110-
try_send(node, :process, commands, &block)
111-
end
11295
end
11396

11497
private
@@ -131,7 +114,7 @@ def send_command(method, *command, **kwargs, &block) # rubocop:disable Metrics/A
131114
when 'wait' then @node.call_primary(method, *command, **kwargs, &block).sum
132115
when 'keys' then @node.call_replica(method, *command, **kwargs, &block).flatten.sort
133116
when 'dbsize' then @node.call_replica(method, *command, **kwargs, &block).sum
134-
when 'scan' then _scan(method, *command, **kwargs, &block)
117+
when 'scan' then _scan(*command, **kwargs)
135118
when 'lastsave' then @node.call_all(method, *command, **kwargs, &block).sort
136119
when 'role' then @node.call_all(method, *command, **kwargs, &block)
137120
when 'config' then send_config_command(method, *command, **kwargs, &block)
@@ -233,7 +216,8 @@ def try_send(node, method, *args, retry_count: 3, **kwargs, &block) # rubocop:di
233216
raise
234217
end
235218

236-
def _scan(method, *command, **kwargs, &block) # rubocop:disable Metrics/MethodLength
219+
def _scan(*command, **kwargs) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
220+
command[1] = ZERO_CURSOR_FOR_SCAN if command.size == 1
237221
input_cursor = Integer(command[1])
238222

239223
client_index = input_cursor % 256
@@ -242,11 +226,11 @@ def _scan(method, *command, **kwargs, &block) # rubocop:disable Metrics/MethodLe
242226
clients = @node.scale_reading_clients
243227

244228
client = clients[client_index]
245-
return ['0', []] unless client
229+
return [ZERO_CURSOR_FOR_SCAN, []] unless client
246230

247231
command[1] = raw_cursor.to_s
248232

249-
result_cursor, result_keys = client.send(method, *command, **kwargs, &block)
233+
result_cursor, result_keys = client.call(*command, **kwargs)
250234
result_cursor = Integer(result_cursor)
251235

252236
client_index += 1 if result_cursor == 0

test/redis_client/cluster/controller.rb

Lines changed: 0 additions & 292 deletions
This file was deleted.

0 commit comments

Comments
 (0)