redis 4.0.1 → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +17 -29
  4. data/.travis/Gemfile +5 -0
  5. data/CHANGELOG.md +29 -0
  6. data/Gemfile +5 -0
  7. data/README.md +1 -1
  8. data/bin/build +71 -0
  9. data/lib/redis.rb +198 -12
  10. data/lib/redis/client.rb +26 -12
  11. data/lib/redis/cluster.rb +285 -0
  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/connection/ruby.rb +5 -2
  22. data/lib/redis/distributed.rb +10 -2
  23. data/lib/redis/errors.rb +46 -0
  24. data/lib/redis/pipeline.rb +9 -1
  25. data/lib/redis/version.rb +1 -1
  26. data/makefile +54 -22
  27. data/redis.gemspec +2 -1
  28. data/test/client_test.rb +17 -0
  29. data/test/cluster_abnormal_state_test.rb +38 -0
  30. data/test/cluster_blocking_commands_test.rb +15 -0
  31. data/test/cluster_client_internals_test.rb +77 -0
  32. data/test/cluster_client_key_hash_tags_test.rb +88 -0
  33. data/test/cluster_client_options_test.rb +147 -0
  34. data/test/cluster_client_pipelining_test.rb +59 -0
  35. data/test/cluster_client_replicas_test.rb +36 -0
  36. data/test/cluster_client_slots_test.rb +94 -0
  37. data/test/cluster_client_transactions_test.rb +71 -0
  38. data/test/cluster_commands_on_cluster_test.rb +165 -0
  39. data/test/cluster_commands_on_connection_test.rb +40 -0
  40. data/test/cluster_commands_on_geo_test.rb +74 -0
  41. data/test/cluster_commands_on_hashes_test.rb +11 -0
  42. data/test/cluster_commands_on_hyper_log_log_test.rb +17 -0
  43. data/test/cluster_commands_on_keys_test.rb +134 -0
  44. data/test/cluster_commands_on_lists_test.rb +15 -0
  45. data/test/cluster_commands_on_pub_sub_test.rb +101 -0
  46. data/test/cluster_commands_on_scripting_test.rb +56 -0
  47. data/test/cluster_commands_on_server_test.rb +221 -0
  48. data/test/cluster_commands_on_sets_test.rb +39 -0
  49. data/test/cluster_commands_on_sorted_sets_test.rb +35 -0
  50. data/test/cluster_commands_on_streams_test.rb +196 -0
  51. data/test/cluster_commands_on_strings_test.rb +15 -0
  52. data/test/cluster_commands_on_transactions_test.rb +41 -0
  53. data/test/cluster_commands_on_value_types_test.rb +14 -0
  54. data/test/commands_on_geo_test.rb +116 -0
  55. data/test/commands_on_hashes_test.rb +2 -14
  56. data/test/commands_on_hyper_log_log_test.rb +2 -14
  57. data/test/commands_on_lists_test.rb +2 -13
  58. data/test/commands_on_sets_test.rb +2 -70
  59. data/test/commands_on_sorted_sets_test.rb +2 -145
  60. data/test/commands_on_strings_test.rb +2 -94
  61. data/test/commands_on_value_types_test.rb +36 -0
  62. data/test/distributed_blocking_commands_test.rb +8 -0
  63. data/test/distributed_commands_on_hashes_test.rb +16 -3
  64. data/test/distributed_commands_on_hyper_log_log_test.rb +8 -13
  65. data/test/distributed_commands_on_lists_test.rb +4 -5
  66. data/test/distributed_commands_on_sets_test.rb +45 -46
  67. data/test/distributed_commands_on_sorted_sets_test.rb +51 -8
  68. data/test/distributed_commands_on_strings_test.rb +10 -0
  69. data/test/distributed_commands_on_value_types_test.rb +36 -0
  70. data/test/helper.rb +176 -32
  71. data/test/internals_test.rb +20 -1
  72. data/test/lint/blocking_commands.rb +40 -16
  73. data/test/lint/hashes.rb +41 -0
  74. data/test/lint/hyper_log_log.rb +15 -1
  75. data/test/lint/lists.rb +16 -0
  76. data/test/lint/sets.rb +142 -0
  77. data/test/lint/sorted_sets.rb +183 -2
  78. data/test/lint/strings.rb +102 -0
  79. data/test/pipelining_commands_test.rb +8 -0
  80. data/test/support/cluster/orchestrator.rb +199 -0
  81. data/test/support/redis_mock.rb +1 -1
  82. data/test/transactions_test.rb +10 -0
  83. metadata +81 -2
@@ -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