Skip to content

Commit 2453cb4

Browse files
authored
Add some test cases (#15)
1 parent 959a1e1 commit 2453cb4

File tree

6 files changed

+93
-11
lines changed

6 files changed

+93
-11
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ desc 'Wait for cluster to be ready'
1414
task :wait do
1515
$LOAD_PATH.unshift(File.expand_path('test', __dir__))
1616
require 'redis_client/cluster/controller'
17-
nodes = (7000..7005).map { |port| "redis://127.0.0.1:#{port}" }
17+
nodes = (7000..7005).map { |port| "#{ENV.fetch('REDIS_SCHEME', 'redis')}://127.0.0.1:#{port}" }
1818
ctrl = ::RedisClient::Cluster::Controller.new(nodes)
1919
ctrl.wait_for_cluster_to_be_ready
2020
end

lib/redis_client/cluster/node_key.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ def hashify(node_key)
1616
end
1717

1818
def split(node_key)
19-
node_key.split(DELIMITER)
19+
pos = node_key&.rindex(DELIMITER, -1)
20+
return [node_key, nil] if pos.nil?
21+
22+
[node_key[0, pos], node_key[pos + 1, node_key.size - pos - 1]]
2023
end
2124

2225
def build_from_uri(uri)
26+
return '' if uri.nil?
27+
2328
"#{uri.host}#{DELIMITER}#{uri.port}"
2429
end
2530

test/redis_client/cluster/test_command.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TestCommand < Minitest::Test
1313

1414
def test_load
1515
[
16-
{ nodes: @clients, error: nil },
16+
{ nodes: @raw_clients, error: nil },
1717
{ nodes: [], error: ::RedisClient::Cluster::InitialSetupError },
1818
{ nodes: [''], error: NoMethodError },
1919
{ nodes: nil, error: ::RedisClient::Cluster::InitialSetupError }
@@ -64,7 +64,7 @@ def test_parse_command_details
6464
end
6565

6666
def test_extract_first_key
67-
cmd = ::RedisClient::Cluster::Command.load(@clients)
67+
cmd = ::RedisClient::Cluster::Command.load(@raw_clients)
6868
[
6969
{ command: %w[SET foo 1], want: 'foo' },
7070
{ command: %w[GET foo], want: 'foo' },
@@ -83,7 +83,7 @@ def test_extract_first_key
8383
end
8484

8585
def test_should_send_to_primary?
86-
cmd = ::RedisClient::Cluster::Command.load(@clients)
86+
cmd = ::RedisClient::Cluster::Command.load(@raw_clients)
8787
[
8888
{ command: %w[SET foo 1], want: true },
8989
{ command: %w[GET foo], want: false },
@@ -98,7 +98,7 @@ def test_should_send_to_primary?
9898
end
9999

100100
def test_should_send_to_replica?
101-
cmd = ::RedisClient::Cluster::Command.load(@clients)
101+
cmd = ::RedisClient::Cluster::Command.load(@raw_clients)
102102
[
103103
{ command: %w[SET foo 1], want: false },
104104
{ command: %w[GET foo], want: true },
@@ -167,7 +167,7 @@ def test_dig_details
167167
end
168168

169169
def test_determine_first_key_position
170-
cmd = ::RedisClient::Cluster::Command.load(@clients)
170+
cmd = ::RedisClient::Cluster::Command.load(@raw_clients)
171171
[
172172
{ command: %w[EVAL "return ARGV[1]" 0 hello], want: 3 },
173173
{ command: [['EVAL'], '"return ARGV[1]"', 0, 'hello'], want: 3 },
@@ -192,7 +192,7 @@ def test_determine_first_key_position
192192
end
193193

194194
def test_determine_optional_key_position
195-
cmd = ::RedisClient::Cluster::Command.load(@clients)
195+
cmd = ::RedisClient::Cluster::Command.load(@raw_clients)
196196
[
197197
{ params: { command: %w[XREAD COUNT 2 STREAMS mystream writers 0-0 0-0], option_name: 'streams' }, want: 4 },
198198
{ params: { command: %w[XREADGROUP GROUP group consumer STREAMS key id], option_name: 'streams' }, want: 5 },
@@ -210,7 +210,7 @@ def test_determine_optional_key_position
210210
end
211211

212212
def test_extract_hash_tag
213-
cmd = ::RedisClient::Cluster::Command.load(@clients)
213+
cmd = ::RedisClient::Cluster::Command.load(@raw_clients)
214214
[
215215
{ key: 'foo', want: '' },
216216
{ key: 'foo{bar}baz', want: 'bar' },
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
require 'testing_helper'
4+
require 'redis_client/cluster/key_slot_converter'
5+
6+
class RedisClient
7+
class Cluster
8+
class TestKeySlotConverter < Minitest::Test
9+
include TestingHelper
10+
11+
def test_convert
12+
(1..255).map { |i| "key#{i}" }.each_with_index do |key, idx|
13+
want = @raw_clients.first.call('CLUSTER', 'KEYSLOT', key)
14+
got = ::RedisClient::Cluster::KeySlotConverter.convert(key)
15+
assert_equal(want, got, "Case: #{idx}")
16+
end
17+
end
18+
end
19+
end
20+
end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
require 'uri'
4+
require 'testing_helper'
5+
require 'redis_client/cluster/node_key'
6+
7+
class RedisClient
8+
class Cluster
9+
class TestNodeKey < Minitest::Test
10+
def test_hashify
11+
[
12+
{ node_key: '127.0.0.1:6379', want: { host: '127.0.0.1', port: '6379' } },
13+
{ node_key: '::1:6379', want: { host: '::1', port: '6379' } },
14+
{ node_key: 'foobar', want: { host: 'foobar', port: nil } },
15+
{ node_key: '', want: { host: '', port: nil } },
16+
{ node_key: nil, want: { host: nil, port: nil } }
17+
].each_with_index do |c, idx|
18+
got = ::RedisClient::Cluster::NodeKey.hashify(c[:node_key])
19+
assert_equal(c[:want], got, "Case: #{idx}")
20+
end
21+
end
22+
23+
def test_split
24+
[
25+
{ node_key: '127.0.0.1:6379', want: ['127.0.0.1', '6379'] },
26+
{ node_key: '::1:6379', want: ['::1', '6379'] },
27+
{ node_key: 'foobar', want: ['foobar', nil] },
28+
{ node_key: '', want: ['', nil] },
29+
{ node_key: nil, want: [nil, nil] }
30+
].each_with_index do |c, idx|
31+
got = ::RedisClient::Cluster::NodeKey.split(c[:node_key])
32+
assert_equal(c[:want], got, "Case: #{idx}")
33+
end
34+
end
35+
36+
def test_build_from_uri
37+
[
38+
{ uri: URI('redis://127.0.0.1:6379'), want: '127.0.0.1:6379' },
39+
{ uri: nil, want: '' }
40+
].each_with_index do |c, idx|
41+
got = ::RedisClient::Cluster::NodeKey.build_from_uri(c[:uri])
42+
assert_equal(c[:want], got, "Case: #{idx}")
43+
end
44+
end
45+
46+
def test_build_from_host_port
47+
[
48+
{ params: { host: '127.0.0.1', port: 6379 }, want: '127.0.0.1:6379' },
49+
{ params: { host: nil, port: nil }, want: ':' }
50+
].each_with_index do |c, idx|
51+
got = ::RedisClient::Cluster::NodeKey.build_from_host_port(c[:params][:host], c[:params][:port])
52+
assert_equal(c[:want], got, "Case: #{idx}")
53+
end
54+
end
55+
end
56+
end
57+
end

test/testing_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ module TestingHelper
99
NODE_ADDRS = (7000..7005).map { |port| "#{REDIS_SCHEME}://127.0.0.1:#{port}" }.freeze
1010

1111
def setup
12-
@clients = NODE_ADDRS.map { |addr| ::RedisClient.config(url: addr).new_client }
12+
@raw_clients = NODE_ADDRS.map { |addr| ::RedisClient.config(url: addr).new_client }
1313
end
1414

1515
def teardown
16-
@clients&.each(&:close)
16+
@raw_clients&.each(&:close)
1717
end
1818
end
1919
end

0 commit comments

Comments
 (0)