redis 3.3.5 → 4.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis/Gemfile +8 -1
  4. data/.travis.yml +34 -62
  5. data/CHANGELOG.md +45 -2
  6. data/Gemfile +5 -1
  7. data/README.md +32 -76
  8. data/benchmarking/logging.rb +1 -1
  9. data/bin/build +71 -0
  10. data/bors.toml +14 -0
  11. data/lib/redis/client.rb +38 -20
  12. data/lib/redis/cluster/command.rb +81 -0
  13. data/lib/redis/cluster/command_loader.rb +32 -0
  14. data/lib/redis/cluster/key_slot_converter.rb +72 -0
  15. data/lib/redis/cluster/node.rb +104 -0
  16. data/lib/redis/cluster/node_key.rb +35 -0
  17. data/lib/redis/cluster/node_loader.rb +35 -0
  18. data/lib/redis/cluster/option.rb +76 -0
  19. data/lib/redis/cluster/slot.rb +69 -0
  20. data/lib/redis/cluster/slot_loader.rb +47 -0
  21. data/lib/redis/cluster.rb +285 -0
  22. data/lib/redis/connection/command_helper.rb +2 -8
  23. data/lib/redis/connection/hiredis.rb +2 -2
  24. data/lib/redis/connection/ruby.rb +13 -30
  25. data/lib/redis/connection/synchrony.rb +12 -4
  26. data/lib/redis/connection.rb +2 -2
  27. data/lib/redis/distributed.rb +29 -8
  28. data/lib/redis/errors.rb +46 -0
  29. data/lib/redis/hash_ring.rb +20 -64
  30. data/lib/redis/pipeline.rb +9 -7
  31. data/lib/redis/version.rb +1 -1
  32. data/lib/redis.rb +287 -52
  33. data/makefile +74 -0
  34. data/redis.gemspec +9 -10
  35. data/test/bitpos_test.rb +13 -19
  36. data/test/blocking_commands_test.rb +3 -5
  37. data/test/client_test.rb +18 -1
  38. data/test/cluster_abnormal_state_test.rb +38 -0
  39. data/test/cluster_blocking_commands_test.rb +15 -0
  40. data/test/cluster_client_internals_test.rb +77 -0
  41. data/test/cluster_client_key_hash_tags_test.rb +88 -0
  42. data/test/cluster_client_options_test.rb +147 -0
  43. data/test/cluster_client_pipelining_test.rb +59 -0
  44. data/test/cluster_client_replicas_test.rb +36 -0
  45. data/test/cluster_client_slots_test.rb +94 -0
  46. data/test/cluster_client_transactions_test.rb +71 -0
  47. data/test/cluster_commands_on_cluster_test.rb +165 -0
  48. data/test/cluster_commands_on_connection_test.rb +40 -0
  49. data/test/cluster_commands_on_geo_test.rb +74 -0
  50. data/test/cluster_commands_on_hashes_test.rb +11 -0
  51. data/test/cluster_commands_on_hyper_log_log_test.rb +17 -0
  52. data/test/cluster_commands_on_keys_test.rb +134 -0
  53. data/test/cluster_commands_on_lists_test.rb +15 -0
  54. data/test/cluster_commands_on_pub_sub_test.rb +101 -0
  55. data/test/cluster_commands_on_scripting_test.rb +56 -0
  56. data/test/cluster_commands_on_server_test.rb +221 -0
  57. data/test/cluster_commands_on_sets_test.rb +39 -0
  58. data/test/cluster_commands_on_sorted_sets_test.rb +35 -0
  59. data/test/cluster_commands_on_streams_test.rb +196 -0
  60. data/test/cluster_commands_on_strings_test.rb +15 -0
  61. data/test/cluster_commands_on_transactions_test.rb +41 -0
  62. data/test/cluster_commands_on_value_types_test.rb +14 -0
  63. data/test/command_map_test.rb +3 -5
  64. data/test/commands_on_geo_test.rb +116 -0
  65. data/test/commands_on_hashes_test.rb +2 -16
  66. data/test/commands_on_hyper_log_log_test.rb +3 -17
  67. data/test/commands_on_lists_test.rb +2 -15
  68. data/test/commands_on_sets_test.rb +2 -72
  69. data/test/commands_on_sorted_sets_test.rb +2 -132
  70. data/test/commands_on_strings_test.rb +2 -96
  71. data/test/commands_on_value_types_test.rb +80 -6
  72. data/test/connection_handling_test.rb +5 -7
  73. data/test/distributed_blocking_commands_test.rb +10 -4
  74. data/test/distributed_commands_on_hashes_test.rb +16 -5
  75. data/test/distributed_commands_on_hyper_log_log_test.rb +8 -15
  76. data/test/distributed_commands_on_lists_test.rb +4 -7
  77. data/test/distributed_commands_on_sets_test.rb +58 -36
  78. data/test/distributed_commands_on_sorted_sets_test.rb +51 -10
  79. data/test/distributed_commands_on_strings_test.rb +30 -10
  80. data/test/distributed_commands_on_value_types_test.rb +38 -4
  81. data/test/distributed_commands_requiring_clustering_test.rb +1 -3
  82. data/test/distributed_connection_handling_test.rb +1 -3
  83. data/test/distributed_internals_test.rb +8 -19
  84. data/test/distributed_key_tags_test.rb +4 -6
  85. data/test/distributed_persistence_control_commands_test.rb +1 -3
  86. data/test/distributed_publish_subscribe_test.rb +1 -3
  87. data/test/distributed_remote_server_control_commands_test.rb +1 -3
  88. data/test/distributed_scripting_test.rb +1 -3
  89. data/test/distributed_sorting_test.rb +1 -3
  90. data/test/distributed_test.rb +12 -14
  91. data/test/distributed_transactions_test.rb +1 -3
  92. data/test/encoding_test.rb +4 -8
  93. data/test/error_replies_test.rb +2 -4
  94. data/test/fork_safety_test.rb +1 -6
  95. data/test/helper.rb +179 -66
  96. data/test/helper_test.rb +1 -3
  97. data/test/internals_test.rb +47 -56
  98. data/test/lint/blocking_commands.rb +40 -16
  99. data/test/lint/hashes.rb +41 -0
  100. data/test/lint/hyper_log_log.rb +15 -1
  101. data/test/lint/lists.rb +16 -0
  102. data/test/lint/sets.rb +142 -0
  103. data/test/lint/sorted_sets.rb +183 -2
  104. data/test/lint/strings.rb +108 -20
  105. data/test/lint/value_types.rb +8 -0
  106. data/test/persistence_control_commands_test.rb +1 -3
  107. data/test/pipelining_commands_test.rb +12 -8
  108. data/test/publish_subscribe_test.rb +1 -3
  109. data/test/remote_server_control_commands_test.rb +60 -3
  110. data/test/scanning_test.rb +1 -7
  111. data/test/scripting_test.rb +1 -3
  112. data/test/sentinel_command_test.rb +1 -3
  113. data/test/sentinel_test.rb +1 -3
  114. data/test/sorting_test.rb +1 -3
  115. data/test/ssl_test.rb +45 -49
  116. data/test/support/cluster/orchestrator.rb +199 -0
  117. data/test/support/connection/hiredis.rb +1 -1
  118. data/test/support/connection/ruby.rb +1 -1
  119. data/test/support/connection/synchrony.rb +1 -1
  120. data/test/support/redis_mock.rb +1 -1
  121. data/test/synchrony_driver.rb +6 -9
  122. data/test/thread_safety_test.rb +1 -3
  123. data/test/transactions_test.rb +11 -3
  124. data/test/unknown_commands_test.rb +1 -3
  125. data/test/url_param_test.rb +44 -46
  126. metadata +109 -16
  127. data/Rakefile +0 -87
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+
5
+ # ruby -w -Itest test/cluster_client_transactions_test.rb
6
+ class TestClusterClientTransactions < Test::Unit::TestCase
7
+ include Helper::Cluster
8
+
9
+ def test_transaction_with_hash_tag
10
+ rc1 = redis
11
+ rc2 = build_another_client
12
+
13
+ rc1.multi do |cli|
14
+ 100.times { |i| cli.set("{key}#{i}", i) }
15
+ end
16
+
17
+ 100.times { |i| assert_equal i.to_s, rc1.get("{key}#{i}") }
18
+ 100.times { |i| assert_equal i.to_s, rc2.get("{key}#{i}") }
19
+ end
20
+
21
+ def test_transaction_without_hash_tag
22
+ rc1 = redis
23
+ rc2 = build_another_client
24
+
25
+ assert_raise(Redis::Cluster::CrossSlotPipeliningError) do
26
+ rc1.multi do |cli|
27
+ 100.times { |i| cli.set("key#{i}", i) }
28
+ end
29
+ end
30
+
31
+ 100.times { |i| assert_equal nil, rc1.get("key#{i}") }
32
+ 100.times { |i| assert_equal nil, rc2.get("key#{i}") }
33
+ end
34
+
35
+ def test_transaction_with_replicas
36
+ rc1 = build_another_client(replica: true)
37
+ rc2 = build_another_client(replica: true)
38
+
39
+ rc1.multi do |cli|
40
+ 100.times { |i| cli.set("{key}#{i}", i) }
41
+ end
42
+
43
+ sleep 0.1
44
+
45
+ 100.times { |i| assert_equal i.to_s, rc1.get("{key}#{i}") }
46
+ 100.times { |i| assert_equal i.to_s, rc2.get("{key}#{i}") }
47
+ end
48
+
49
+ def test_transaction_with_watch
50
+ rc1 = redis
51
+ rc2 = build_another_client
52
+
53
+ rc1.set('{key}1', 100)
54
+ rc1.watch('{key}1')
55
+
56
+ rc2.set('{key}1', 200)
57
+ val = rc1.get('{key}1').to_i
58
+ val += 1
59
+
60
+ rc1.multi do |cli|
61
+ cli.set('{key}1', val)
62
+ cli.set('{key}2', 300)
63
+ end
64
+
65
+ assert_equal '200', rc1.get('{key}1')
66
+ assert_equal '200', rc2.get('{key}1')
67
+
68
+ assert_equal nil, rc1.get('{key}2')
69
+ assert_equal nil, rc2.get('{key}2')
70
+ end
71
+ end
@@ -0,0 +1,165 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+
5
+ # ruby -w -Itest test/cluster_commands_on_cluster_test.rb
6
+ # @see https://redis.io/commands#cluster
7
+ class TestClusterCommandsOnCluster < Test::Unit::TestCase
8
+ include Helper::Cluster
9
+
10
+ def test_cluster_addslots
11
+ assert_raise(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER ADDSLOTS command should be...') do
12
+ redis.cluster(:addslots, 0, 1, 2)
13
+ end
14
+ end
15
+
16
+ def test_cluster_count_failure_reports
17
+ assert_raise(Redis::CommandError, 'ERR Unknown node unknown-node-id') do
18
+ redis.cluster('count-failure-reports', 'unknown-node-id')
19
+ end
20
+
21
+ node_id = redis.cluster(:nodes).first.fetch('node_id')
22
+ assert_true(redis.cluster('count-failure-reports', node_id) >= 0)
23
+ end
24
+
25
+ def test_cluster_countkeysinslot
26
+ assert_true(redis.cluster(:countkeysinslot, 0) >= 0)
27
+ assert_true(redis.cluster(:countkeysinslot, 16383) >= 0)
28
+
29
+ assert_raise(Redis::CommandError, 'ERR Invalid slot') do
30
+ redis.cluster(:countkeysinslot, -1)
31
+ end
32
+
33
+ assert_raise(Redis::CommandError, 'ERR Invalid slot') do
34
+ redis.cluster(:countkeysinslot, 16384)
35
+ end
36
+ end
37
+
38
+ def test_cluster_delslots
39
+ assert_raise(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER DELSLOTS command should be...') do
40
+ redis.cluster(:delslots, 0, 1, 2)
41
+ end
42
+ end
43
+
44
+ def test_cluster_failover
45
+ assert_raise(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER FAILOVER command should be...') do
46
+ redis.cluster(:failover, 'FORCE')
47
+ end
48
+ end
49
+
50
+ def test_cluster_forget
51
+ assert_raise(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER FORGET command should be...') do
52
+ redis.cluster(:forget, 'unknown-node-id')
53
+ end
54
+ end
55
+
56
+ def test_cluster_getkeysinslot
57
+ assert_instance_of Array, redis.cluster(:getkeysinslot, 0, 3)
58
+ end
59
+
60
+ def test_cluster_info
61
+ info = redis.cluster(:info)
62
+
63
+ assert_equal '3', info.fetch('cluster_size')
64
+ end
65
+
66
+ def test_cluster_keyslot
67
+ assert_equal Redis::Cluster::KeySlotConverter.convert('hogehoge'), redis.cluster(:keyslot, 'hogehoge')
68
+ assert_equal Redis::Cluster::KeySlotConverter.convert('12345'), redis.cluster(:keyslot, '12345')
69
+ assert_equal Redis::Cluster::KeySlotConverter.convert('foo'), redis.cluster(:keyslot, 'boo{foo}woo')
70
+ assert_equal Redis::Cluster::KeySlotConverter.convert('antirez.is.cool'), redis.cluster(:keyslot, 'antirez.is.cool')
71
+ assert_equal Redis::Cluster::KeySlotConverter.convert(''), redis.cluster(:keyslot, '')
72
+ end
73
+
74
+ def test_cluster_meet
75
+ assert_raise(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER MEET command should be...') do
76
+ redis.cluster(:meet, '127.0.0.1', 11211)
77
+ end
78
+ end
79
+
80
+ def test_cluster_nodes
81
+ cluster_nodes = redis.cluster(:nodes)
82
+ sample_node = cluster_nodes.first
83
+
84
+ assert_equal 6, cluster_nodes.length
85
+ assert_equal true, sample_node.key?('node_id')
86
+ assert_equal true, sample_node.key?('ip_port')
87
+ assert_equal true, sample_node.key?('flags')
88
+ assert_equal true, sample_node.key?('master_node_id')
89
+ assert_equal true, sample_node.key?('ping_sent')
90
+ assert_equal true, sample_node.key?('pong_recv')
91
+ assert_equal true, sample_node.key?('config_epoch')
92
+ assert_equal true, sample_node.key?('link_state')
93
+ assert_equal true, sample_node.key?('slots')
94
+ end
95
+
96
+ def test_cluster_replicate
97
+ assert_raise(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER REPLICATE command should be...') do
98
+ redis.cluster(:replicate)
99
+ end
100
+ end
101
+
102
+ def test_cluster_reset
103
+ assert_raise(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER RESET command should be...') do
104
+ redis.cluster(:reset)
105
+ end
106
+ end
107
+
108
+ def test_cluster_saveconfig
109
+ assert_equal 'OK', redis.cluster(:saveconfig)
110
+ end
111
+
112
+ def test_cluster_set_config_epoch
113
+ assert_raise(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER SET-CONFIG-EPOCH command should be...') do
114
+ redis.cluster('set-config-epoch')
115
+ end
116
+ end
117
+
118
+ def test_cluster_setslot
119
+ assert_raise(Redis::Cluster::OrchestrationCommandNotSupported, 'CLUSTER SETSLOT command should be...') do
120
+ redis.cluster(:setslot)
121
+ end
122
+ end
123
+
124
+ def test_cluster_slaves
125
+ cluster_nodes = redis.cluster(:nodes)
126
+
127
+ sample_master_node_id = cluster_nodes.find { |n| n.fetch('master_node_id') == '-' }.fetch('node_id')
128
+ sample_slave_node_id = cluster_nodes.find { |n| n.fetch('master_node_id') != '-' }.fetch('node_id')
129
+
130
+ assert_equal 'slave', redis.cluster(:slaves, sample_master_node_id).first.fetch('flags').first
131
+ assert_raise(Redis::CommandError, 'ERR The specified node is not a master') do
132
+ redis.cluster(:slaves, sample_slave_node_id)
133
+ end
134
+ end
135
+
136
+ def test_cluster_slots
137
+ slots = redis.cluster(:slots)
138
+ sample_slot = slots.first
139
+
140
+ assert_equal 3, slots.length
141
+ assert_equal true, sample_slot.key?('start_slot')
142
+ assert_equal true, sample_slot.key?('end_slot')
143
+ assert_equal true, sample_slot.key?('master')
144
+ assert_equal true, sample_slot.fetch('master').key?('ip')
145
+ assert_equal true, sample_slot.fetch('master').key?('port')
146
+ assert_equal true, sample_slot.fetch('master').key?('node_id')
147
+ assert_equal true, sample_slot.key?('replicas')
148
+ assert_equal true, sample_slot.fetch('replicas').is_a?(Array)
149
+ assert_equal true, sample_slot.fetch('replicas').first.key?('ip')
150
+ assert_equal true, sample_slot.fetch('replicas').first.key?('port')
151
+ assert_equal true, sample_slot.fetch('replicas').first.key?('node_id')
152
+ end
153
+
154
+ def test_readonly
155
+ assert_raise(Redis::Cluster::OrchestrationCommandNotSupported, 'READONLY command should be...') do
156
+ redis.readonly
157
+ end
158
+ end
159
+
160
+ def test_readwrite
161
+ assert_raise(Redis::Cluster::OrchestrationCommandNotSupported, 'READWRITE command should be...') do
162
+ redis.readwrite
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+
5
+ # ruby -w -Itest test/cluster_commands_on_connection_test.rb
6
+ # @see https://redis.io/commands#connection
7
+ class TestClusterCommandsOnConnection < Test::Unit::TestCase
8
+ include Helper::Cluster
9
+
10
+ def test_auth
11
+ redis_cluster_mock(auth: ->(*_) { '+OK' }) do |redis|
12
+ assert_equal 'OK', redis.auth('my-password-123')
13
+ end
14
+ end
15
+
16
+ def test_echo
17
+ assert_equal 'hogehoge', redis.echo('hogehoge')
18
+ end
19
+
20
+ def test_ping
21
+ assert_equal 'hogehoge', redis.ping('hogehoge')
22
+ end
23
+
24
+ def test_quit
25
+ redis2 = build_another_client
26
+ assert_equal 'OK', redis2.quit
27
+ end
28
+
29
+ def test_select
30
+ assert_raise(Redis::CommandError, 'ERR SELECT is not allowed in cluster mode') do
31
+ redis.select(1)
32
+ end
33
+ end
34
+
35
+ def test_swapdb
36
+ assert_raise(Redis::CommandError, 'ERR SWAPDB is not allowed in cluster mode') do
37
+ redis.swapdb(1, 2)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+
5
+ # ruby -w -Itest test/cluster_commands_on_geo_test.rb
6
+ # @see https://redis.io/commands#geo
7
+ class TestClusterCommandsOnGeo < Test::Unit::TestCase
8
+ include Helper::Cluster
9
+
10
+ MIN_REDIS_VERSION = '3.2.0'
11
+
12
+ def add_sicily
13
+ redis.geoadd('Sicily',
14
+ 13.361389, 38.115556, 'Palermo',
15
+ 15.087269, 37.502669, 'Catania')
16
+ end
17
+
18
+ def test_geoadd
19
+ target_version(MIN_REDIS_VERSION) do
20
+ assert_equal 2, add_sicily
21
+ end
22
+ end
23
+
24
+ def test_geohash
25
+ target_version(MIN_REDIS_VERSION) do
26
+ add_sicily
27
+ assert_equal %w[sqc8b49rny0 sqdtr74hyu0], redis.geohash('Sicily', %w[Palermo Catania])
28
+ end
29
+ end
30
+
31
+ def test_geopos
32
+ target_version(MIN_REDIS_VERSION) do
33
+ add_sicily
34
+ expected = [%w[13.36138933897018433 38.11555639549629859],
35
+ %w[15.08726745843887329 37.50266842333162032],
36
+ nil]
37
+ assert_equal expected, redis.geopos('Sicily', %w[Palermo Catania NonExisting])
38
+ end
39
+ end
40
+
41
+ def test_geodist
42
+ target_version(MIN_REDIS_VERSION) do
43
+ add_sicily
44
+ assert_equal '166274.1516', redis.geodist('Sicily', 'Palermo', 'Catania')
45
+ assert_equal '166.2742', redis.geodist('Sicily', 'Palermo', 'Catania', 'km')
46
+ assert_equal '103.3182', redis.geodist('Sicily', 'Palermo', 'Catania', 'mi')
47
+ end
48
+ end
49
+
50
+ def test_georadius
51
+ target_version(MIN_REDIS_VERSION) do
52
+ add_sicily
53
+
54
+ expected = [%w[Palermo 190.4424], %w[Catania 56.4413]]
55
+ assert_equal expected, redis.georadius('Sicily', 15, 37, 200, 'km', 'WITHDIST')
56
+
57
+ expected = [['Palermo', %w[13.36138933897018433 38.11555639549629859]],
58
+ ['Catania', %w[15.08726745843887329 37.50266842333162032]]]
59
+ assert_equal expected, redis.georadius('Sicily', 15, 37, 200, 'km', 'WITHCOORD')
60
+
61
+ expected = [['Palermo', '190.4424', %w[13.36138933897018433 38.11555639549629859]],
62
+ ['Catania', '56.4413', %w[15.08726745843887329 37.50266842333162032]]]
63
+ assert_equal expected, redis.georadius('Sicily', 15, 37, 200, 'km', 'WITHDIST', 'WITHCOORD')
64
+ end
65
+ end
66
+
67
+ def test_georadiusbymember
68
+ target_version(MIN_REDIS_VERSION) do
69
+ redis.geoadd('Sicily', 13.583333, 37.316667, 'Agrigento')
70
+ add_sicily
71
+ assert_equal %w[Agrigento Palermo], redis.georadiusbymember('Sicily', 'Agrigento', 100, 'km')
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+ require_relative 'lint/hashes'
5
+
6
+ # ruby -w -Itest test/cluster_commands_on_hashes_test.rb
7
+ # @see https://redis.io/commands#hash
8
+ class TestClusterCommandsOnHashes < Test::Unit::TestCase
9
+ include Helper::Cluster
10
+ include Lint::Hashes
11
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+ require_relative 'lint/hyper_log_log'
5
+
6
+ # ruby -w -Itest test/cluster_commands_on_hyper_log_log_test.rb
7
+ # @see https://redis.io/commands#hyperloglog
8
+ class TestClusterCommandsOnHyperLogLog < Test::Unit::TestCase
9
+ include Helper::Cluster
10
+ include Lint::HyperLogLog
11
+
12
+ def test_pfmerge
13
+ assert_raise Redis::CommandError do
14
+ super
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+
5
+ # ruby -w -Itest test/cluster_commands_on_keys_test.rb
6
+ # @see https://redis.io/commands#generic
7
+ class TestClusterCommandsOnKeys < Test::Unit::TestCase
8
+ include Helper::Cluster
9
+
10
+ def set_some_keys
11
+ redis.set('key1', 'Hello')
12
+ redis.set('key2', 'World')
13
+
14
+ redis.set('{key}1', 'Hello')
15
+ redis.set('{key}2', 'World')
16
+ end
17
+
18
+ def test_del
19
+ set_some_keys
20
+
21
+ assert_raise(Redis::CommandError, "CROSSSLOT Keys in request don't hash to the same slot") do
22
+ redis.del('key1', 'key2')
23
+ end
24
+
25
+ assert_equal 2, redis.del('{key}1', '{key}2')
26
+ end
27
+
28
+ def test_migrate
29
+ redis.set('mykey', 1)
30
+
31
+ assert_raise(Redis::CommandError, 'ERR Target instance replied with error: MOVED 14687 127.0.0.1:7002') do
32
+ # We cannot move between cluster nodes.
33
+ redis.migrate('mykey', host: '127.0.0.1', port: 7000)
34
+ end
35
+
36
+ redis_cluster_mock(migrate: ->(*_) { '-IOERR error or timeout writing to target instance' }) do |redis|
37
+ assert_raise(Redis::CommandError, 'IOERR error or timeout writing to target instance') do
38
+ redis.migrate('mykey', host: '127.0.0.1', port: 11211)
39
+ end
40
+ end
41
+
42
+ redis_cluster_mock(migrate: ->(*_) { '+OK' }) do |redis|
43
+ assert_equal 'OK', redis.migrate('mykey', host: '127.0.0.1', port: 6379)
44
+ end
45
+ end
46
+
47
+ def test_object
48
+ redis.lpush('mylist', 'Hello World')
49
+ assert_equal 1, redis.object('refcount', 'mylist')
50
+ expected_encoding = version < '3.2.0' ? 'ziplist' : 'quicklist'
51
+ assert_equal expected_encoding, redis.object('encoding', 'mylist')
52
+ expected_instance_type = RUBY_VERSION < '2.4.0' ? Fixnum : Integer
53
+ assert_instance_of expected_instance_type, redis.object('idletime', 'mylist')
54
+
55
+ redis.set('foo', 1000)
56
+ assert_equal 'int', redis.object('encoding', 'foo')
57
+
58
+ redis.set('bar', '1000bar')
59
+ assert_equal 'embstr', redis.object('encoding', 'bar')
60
+ end
61
+
62
+ def test_randomkey
63
+ set_some_keys
64
+ assert_true redis.randomkey.is_a?(String)
65
+ end
66
+
67
+ def test_rename
68
+ set_some_keys
69
+
70
+ assert_raise(Redis::CommandError, "CROSSSLOT Keys in request don't hash to the same slot") do
71
+ redis.rename('key1', 'key3')
72
+ end
73
+
74
+ assert_equal 'OK', redis.rename('{key}1', '{key}3')
75
+ end
76
+
77
+ def test_renamenx
78
+ set_some_keys
79
+
80
+ assert_raise(Redis::CommandError, "CROSSSLOT Keys in request don't hash to the same slot") do
81
+ redis.renamenx('key1', 'key2')
82
+ end
83
+
84
+ assert_equal false, redis.renamenx('{key}1', '{key}2')
85
+ end
86
+
87
+ def test_sort
88
+ redis.lpush('mylist', 3)
89
+ redis.lpush('mylist', 1)
90
+ redis.lpush('mylist', 5)
91
+ redis.lpush('mylist', 2)
92
+ redis.lpush('mylist', 4)
93
+ assert_equal %w[1 2 3 4 5], redis.sort('mylist')
94
+ end
95
+
96
+ def test_touch
97
+ target_version('3.2.1') do
98
+ set_some_keys
99
+ assert_equal 1, redis.touch('key1')
100
+ assert_equal 1, redis.touch('key2')
101
+ assert_equal 1, redis.touch('key1', 'key2')
102
+ assert_equal 2, redis.touch('{key}1', '{key}2')
103
+ end
104
+ end
105
+
106
+ def test_unlink
107
+ target_version('4.0.0') do
108
+ set_some_keys
109
+ assert_raise(Redis::CommandError, "CROSSSLOT Keys in request don't hash to the same slot") do
110
+ redis.unlink('key1', 'key2', 'key3')
111
+ end
112
+ assert_equal 2, redis.unlink('{key}1', '{key}2', '{key}3')
113
+ end
114
+ end
115
+
116
+ def test_wait
117
+ set_some_keys
118
+ assert_equal 1, redis.wait(1, 0)
119
+ end
120
+
121
+ def test_scan
122
+ set_some_keys
123
+
124
+ cursor = 0
125
+ all_keys = []
126
+ loop do
127
+ cursor, keys = redis.scan(cursor, match: '{key}*')
128
+ all_keys += keys
129
+ break if cursor == '0'
130
+ end
131
+
132
+ assert_equal 2, all_keys.uniq.size
133
+ end
134
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+ require_relative 'lint/lists'
5
+
6
+ # ruby -w -Itest test/cluster_commands_on_lists_test.rb
7
+ # @see https://redis.io/commands#list
8
+ class TestClusterCommandsOnLists < Test::Unit::TestCase
9
+ include Helper::Cluster
10
+ include Lint::Lists
11
+
12
+ def test_rpoplpush
13
+ assert_raise(Redis::CommandError) { super }
14
+ end
15
+ end
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+
5
+ # ruby -w -Itest test/cluster_commands_on_pub_sub_test.rb
6
+ # @see https://redis.io/commands#pubsub
7
+ class TestClusterCommandsOnPubSub < Test::Unit::TestCase
8
+ include Helper::Cluster
9
+
10
+ def test_publish_subscribe_unsubscribe_pubsub
11
+ sub_cnt = 0
12
+ messages = {}
13
+
14
+ wire = Wire.new do
15
+ redis.subscribe('channel1', 'channel2') do |on|
16
+ on.subscribe { |_c, t| sub_cnt = t }
17
+ on.unsubscribe { |_c, t| sub_cnt = t }
18
+ on.message do |c, msg|
19
+ messages[c] = msg
20
+ # FIXME: blocking occurs when `unsubscribe` method was called with channel arguments
21
+ redis.unsubscribe if messages.size == 2
22
+ end
23
+ end
24
+ end
25
+
26
+ Wire.pass until sub_cnt == 2
27
+
28
+ publisher = build_another_client
29
+
30
+ assert_equal %w[channel1 channel2], publisher.pubsub(:channels)
31
+ assert_equal %w[channel1 channel2], publisher.pubsub(:channels, 'cha*')
32
+ assert_equal [], publisher.pubsub(:channels, 'chachacha*')
33
+ assert_equal({}, publisher.pubsub(:numsub))
34
+ assert_equal({ 'channel1' => 1, 'channel2' => 1, 'channel3' => 0 },
35
+ publisher.pubsub(:numsub, 'channel1', 'channel2', 'channel3'))
36
+ assert_equal 0, publisher.pubsub(:numpat)
37
+
38
+ publisher.publish('channel1', 'one')
39
+ publisher.publish('channel2', 'two')
40
+
41
+ wire.join
42
+
43
+ assert_equal({ 'channel1' => 'one', 'channel2' => 'two' }, messages.sort.to_h)
44
+
45
+ assert_equal [], publisher.pubsub(:channels)
46
+ assert_equal [], publisher.pubsub(:channels, 'cha*')
47
+ assert_equal [], publisher.pubsub(:channels, 'chachacha*')
48
+ assert_equal({}, publisher.pubsub(:numsub))
49
+ assert_equal({ 'channel1' => 0, 'channel2' => 0, 'channel3' => 0 },
50
+ publisher.pubsub(:numsub, 'channel1', 'channel2', 'channel3'))
51
+ assert_equal 0, publisher.pubsub(:numpat)
52
+ end
53
+
54
+ def test_publish_psubscribe_punsubscribe_pubsub
55
+ sub_cnt = 0
56
+ messages = {}
57
+
58
+ wire = Wire.new do
59
+ redis.psubscribe('cha*', 'her*') do |on|
60
+ on.psubscribe { |_c, t| sub_cnt = t }
61
+ on.punsubscribe { |_c, t| sub_cnt = t }
62
+ on.pmessage do |_ptn, chn, msg|
63
+ messages[chn] = msg
64
+ # FIXME: blocking occurs when `unsubscribe` method was called with channel arguments
65
+ redis.punsubscribe if messages.size == 3
66
+ end
67
+ end
68
+ end
69
+
70
+ Wire.pass until sub_cnt == 2
71
+
72
+ publisher = build_another_client
73
+
74
+ assert_equal [], publisher.pubsub(:channels)
75
+ assert_equal [], publisher.pubsub(:channels, 'cha*')
76
+ assert_equal [], publisher.pubsub(:channels, 'her*')
77
+ assert_equal [], publisher.pubsub(:channels, 'guc*')
78
+ assert_equal({}, publisher.pubsub(:numsub))
79
+ assert_equal({ 'channel1' => 0, 'channel2' => 0, 'hermes3' => 0, 'gucci4' => 0 },
80
+ publisher.pubsub(:numsub, 'channel1', 'channel2', 'hermes3', 'gucci4'))
81
+ assert_equal 2, publisher.pubsub(:numpat)
82
+
83
+ publisher.publish('chanel1', 'one')
84
+ publisher.publish('chanel2', 'two')
85
+ publisher.publish('hermes3', 'three')
86
+ publisher.publish('gucci4', 'four')
87
+
88
+ wire.join
89
+
90
+ assert_equal({ 'chanel1' => 'one', 'chanel2' => 'two', 'hermes3' => 'three' }, messages.sort.to_h)
91
+
92
+ assert_equal [], publisher.pubsub(:channels)
93
+ assert_equal [], publisher.pubsub(:channels, 'cha*')
94
+ assert_equal [], publisher.pubsub(:channels, 'her*')
95
+ assert_equal [], publisher.pubsub(:channels, 'guc*')
96
+ assert_equal({}, publisher.pubsub(:numsub))
97
+ assert_equal({ 'channel1' => 0, 'channel2' => 0, 'hermes3' => 0, 'gucci4' => 0 },
98
+ publisher.pubsub(:numsub, 'channel1', 'channel2', 'hermes3', 'gucci4'))
99
+ assert_equal 0, publisher.pubsub(:numpat)
100
+ end
101
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+
5
+ # ruby -w -Itest test/cluster_commands_on_scripting_test.rb
6
+ # @see https://redis.io/commands#scripting
7
+ class TestClusterCommandsOnScripting < Test::Unit::TestCase
8
+ include Helper::Cluster
9
+
10
+ def test_eval
11
+ script = 'return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}'
12
+ argv = %w[first second]
13
+
14
+ keys = %w[key1 key2]
15
+ assert_raise(Redis::CommandError, "CROSSSLOT Keys in request don't hash to the same slot") do
16
+ redis.eval(script, keys: keys, argv: argv)
17
+ end
18
+
19
+ keys = %w[{key}1 {key}2]
20
+ expected = %w[{key}1 {key}2 first second]
21
+ assert_equal expected, redis.eval(script, keys: keys, argv: argv)
22
+ end
23
+
24
+ def test_evalsha
25
+ sha = redis.script(:load, 'return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}')
26
+ expected = %w[{key}1 {key}2 first second]
27
+ assert_equal expected, redis.evalsha(sha, keys: %w[{key}1 {key}2], argv: %w[first second])
28
+ end
29
+
30
+ def test_script_debug
31
+ target_version('3.2.0') do
32
+ assert_equal 'OK', redis.script(:debug, 'yes')
33
+ assert_equal 'OK', redis.script(:debug, 'no')
34
+ end
35
+ end
36
+
37
+ def test_script_exists
38
+ sha = redis.script(:load, 'return 1')
39
+ assert_equal true, redis.script(:exists, sha)
40
+ assert_equal false, redis.script(:exists, 'unknownsha')
41
+ end
42
+
43
+ def test_script_flush
44
+ assert_equal 'OK', redis.script(:flush)
45
+ end
46
+
47
+ def test_script_kill
48
+ redis_cluster_mock(kill: -> { '+OK' }) do |redis|
49
+ assert_equal 'OK', redis.script(:kill)
50
+ end
51
+ end
52
+
53
+ def test_script_load
54
+ assert_equal 'e0e1f9fabfc9d4800c877a703b823ac0578ff8db', redis.script(:load, 'return 1')
55
+ end
56
+ end