redis 4.0.0.rc1 → 4.4.0

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 (101) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +143 -3
  3. data/README.md +127 -18
  4. data/lib/redis/client.rb +150 -93
  5. data/lib/redis/cluster/command.rb +81 -0
  6. data/lib/redis/cluster/command_loader.rb +34 -0
  7. data/lib/redis/cluster/key_slot_converter.rb +72 -0
  8. data/lib/redis/cluster/node.rb +108 -0
  9. data/lib/redis/cluster/node_key.rb +31 -0
  10. data/lib/redis/cluster/node_loader.rb +37 -0
  11. data/lib/redis/cluster/option.rb +93 -0
  12. data/lib/redis/cluster/slot.rb +86 -0
  13. data/lib/redis/cluster/slot_loader.rb +49 -0
  14. data/lib/redis/cluster.rb +291 -0
  15. data/lib/redis/connection/command_helper.rb +3 -2
  16. data/lib/redis/connection/hiredis.rb +4 -3
  17. data/lib/redis/connection/registry.rb +2 -1
  18. data/lib/redis/connection/ruby.rb +123 -105
  19. data/lib/redis/connection/synchrony.rb +18 -5
  20. data/lib/redis/connection.rb +2 -0
  21. data/lib/redis/distributed.rb +955 -0
  22. data/lib/redis/errors.rb +48 -0
  23. data/lib/redis/hash_ring.rb +89 -0
  24. data/lib/redis/pipeline.rb +55 -9
  25. data/lib/redis/subscribe.rb +11 -12
  26. data/lib/redis/version.rb +3 -1
  27. data/lib/redis.rb +1242 -381
  28. metadata +34 -141
  29. data/.gitignore +0 -16
  30. data/.travis/Gemfile +0 -11
  31. data/.travis.yml +0 -71
  32. data/.yardopts +0 -3
  33. data/Gemfile +0 -3
  34. data/benchmarking/logging.rb +0 -71
  35. data/benchmarking/pipeline.rb +0 -51
  36. data/benchmarking/speed.rb +0 -21
  37. data/benchmarking/suite.rb +0 -24
  38. data/benchmarking/worker.rb +0 -71
  39. data/examples/basic.rb +0 -15
  40. data/examples/consistency.rb +0 -114
  41. data/examples/incr-decr.rb +0 -17
  42. data/examples/list.rb +0 -26
  43. data/examples/pubsub.rb +0 -37
  44. data/examples/sentinel/sentinel.conf +0 -9
  45. data/examples/sentinel/start +0 -49
  46. data/examples/sentinel.rb +0 -41
  47. data/examples/sets.rb +0 -36
  48. data/examples/unicorn/config.ru +0 -3
  49. data/examples/unicorn/unicorn.rb +0 -20
  50. data/makefile +0 -42
  51. data/redis.gemspec +0 -40
  52. data/test/bitpos_test.rb +0 -63
  53. data/test/blocking_commands_test.rb +0 -183
  54. data/test/client_test.rb +0 -59
  55. data/test/command_map_test.rb +0 -28
  56. data/test/commands_on_hashes_test.rb +0 -174
  57. data/test/commands_on_hyper_log_log_test.rb +0 -70
  58. data/test/commands_on_lists_test.rb +0 -154
  59. data/test/commands_on_sets_test.rb +0 -208
  60. data/test/commands_on_sorted_sets_test.rb +0 -444
  61. data/test/commands_on_strings_test.rb +0 -338
  62. data/test/commands_on_value_types_test.rb +0 -246
  63. data/test/connection_handling_test.rb +0 -275
  64. data/test/db/.gitkeep +0 -0
  65. data/test/encoding_test.rb +0 -14
  66. data/test/error_replies_test.rb +0 -57
  67. data/test/fork_safety_test.rb +0 -60
  68. data/test/helper.rb +0 -179
  69. data/test/helper_test.rb +0 -22
  70. data/test/internals_test.rb +0 -435
  71. data/test/persistence_control_commands_test.rb +0 -24
  72. data/test/pipelining_commands_test.rb +0 -238
  73. data/test/publish_subscribe_test.rb +0 -280
  74. data/test/remote_server_control_commands_test.rb +0 -175
  75. data/test/scanning_test.rb +0 -407
  76. data/test/scripting_test.rb +0 -76
  77. data/test/sentinel_command_test.rb +0 -78
  78. data/test/sentinel_test.rb +0 -253
  79. data/test/sorting_test.rb +0 -57
  80. data/test/ssl_test.rb +0 -69
  81. data/test/support/connection/hiredis.rb +0 -1
  82. data/test/support/connection/ruby.rb +0 -1
  83. data/test/support/connection/synchrony.rb +0 -17
  84. data/test/support/redis_mock.rb +0 -130
  85. data/test/support/ssl/gen_certs.sh +0 -31
  86. data/test/support/ssl/trusted-ca.crt +0 -25
  87. data/test/support/ssl/trusted-ca.key +0 -27
  88. data/test/support/ssl/trusted-cert.crt +0 -81
  89. data/test/support/ssl/trusted-cert.key +0 -28
  90. data/test/support/ssl/untrusted-ca.crt +0 -26
  91. data/test/support/ssl/untrusted-ca.key +0 -27
  92. data/test/support/ssl/untrusted-cert.crt +0 -82
  93. data/test/support/ssl/untrusted-cert.key +0 -28
  94. data/test/support/wire/synchrony.rb +0 -24
  95. data/test/support/wire/thread.rb +0 -5
  96. data/test/synchrony_driver.rb +0 -85
  97. data/test/test.conf.erb +0 -9
  98. data/test/thread_safety_test.rb +0 -60
  99. data/test/transactions_test.rb +0 -262
  100. data/test/unknown_commands_test.rb +0 -12
  101. data/test/url_param_test.rb +0 -136
@@ -1,114 +0,0 @@
1
- # This file implements a simple consistency test for Redis-rb (or any other
2
- # Redis environment if you pass a different client object) where a client
3
- # writes to the database using INCR in order to increment keys, but actively
4
- # remember the value the key should have. Before every write a read is performed
5
- # to check if the value in the database matches the value expected.
6
- #
7
- # In this way this program can check for lost writes, or acknowledged writes
8
- # that were executed.
9
- #
10
- # Copyright (C) 2013-2014 Salvatore Sanfilippo <antirez@gmail.com>
11
- #
12
- # Permission is hereby granted, free of charge, to any person obtaining
13
- # a copy of this software and associated documentation files (the
14
- # "Software"), to deal in the Software without restriction, including
15
- # without limitation the rights to use, copy, modify, merge, publish,
16
- # distribute, sublicense, and/or sell copies of the Software, and to
17
- # permit persons to whom the Software is furnished to do so, subject to
18
- # the following conditions:
19
- #
20
- # The above copyright notice and this permission notice shall be
21
- # included in all copies or substantial portions of the Software.
22
- #
23
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
-
31
- require 'redis'
32
-
33
- class ConsistencyTester
34
- def initialize(redis)
35
- @r = redis
36
- @working_set = 10000
37
- @keyspace = 100000
38
- @writes = 0
39
- @reads = 0
40
- @failed_writes = 0
41
- @failed_reads = 0
42
- @lost_writes = 0
43
- @not_ack_writes = 0
44
- @delay = 0
45
- @cached = {} # We take our view of data stored in the DB.
46
- @prefix = [Process.pid.to_s,Time.now.usec,@r.object_id,""].join("|")
47
- @errtime = {}
48
- end
49
-
50
- def genkey
51
- # Write more often to a small subset of keys
52
- ks = rand() > 0.5 ? @keyspace : @working_set
53
- @prefix+"key_"+rand(ks).to_s
54
- end
55
-
56
- def check_consistency(key,value)
57
- expected = @cached[key]
58
- return if !expected # We lack info about previous state.
59
- if expected > value
60
- @lost_writes += expected-value
61
- elsif expected < value
62
- @not_ack_writes += value-expected
63
- end
64
- end
65
-
66
- def puterr(msg)
67
- if !@errtime[msg] || Time.now.to_i != @errtime[msg]
68
- puts msg
69
- end
70
- @errtime[msg] = Time.now.to_i
71
- end
72
-
73
- def test
74
- last_report = Time.now.to_i
75
- while true
76
- # Read
77
- key = genkey
78
- begin
79
- val = @r.get(key)
80
- check_consistency(key,val.to_i)
81
- @reads += 1
82
- rescue => e
83
- puterr "Reading: #{e.class}: #{e.message} (#{e.backtrace.first})"
84
- @failed_reads += 1
85
- end
86
-
87
- # Write
88
- begin
89
- @cached[key] = @r.incr(key).to_i
90
- @writes += 1
91
- rescue => e
92
- puterr "Writing: #{e.class}: #{e.message} (#{e.backtrace.first})"
93
- @failed_writes += 1
94
- end
95
-
96
- # Report
97
- sleep @delay
98
- if Time.now.to_i != last_report
99
- report = "#{@reads} R (#{@failed_reads} err) | " +
100
- "#{@writes} W (#{@failed_writes} err) | "
101
- report += "#{@lost_writes} lost | " if @lost_writes > 0
102
- report += "#{@not_ack_writes} noack | " if @not_ack_writes > 0
103
- last_report = Time.now.to_i
104
- puts report
105
- end
106
- end
107
- end
108
- end
109
-
110
- Sentinels = [{:host => "127.0.0.1", :port => 26379},
111
- {:host => "127.0.0.1", :port => 26380}]
112
- r = Redis.new(:url => "redis://master1", :sentinels => Sentinels, :role => :master)
113
- tester = ConsistencyTester.new(r)
114
- tester.test
@@ -1,17 +0,0 @@
1
- require 'redis'
2
-
3
- r = Redis.new
4
-
5
- puts
6
- p 'incr'
7
- r.del 'counter'
8
-
9
- p r.incr('counter')
10
- p r.incr('counter')
11
- p r.incr('counter')
12
-
13
- puts
14
- p 'decr'
15
- p r.decr('counter')
16
- p r.decr('counter')
17
- p r.decr('counter')
data/examples/list.rb DELETED
@@ -1,26 +0,0 @@
1
- require 'rubygems'
2
- require 'redis'
3
-
4
- r = Redis.new
5
-
6
- r.del 'logs'
7
-
8
- puts
9
-
10
- p "pushing log messages into a LIST"
11
- r.rpush 'logs', 'some log message'
12
- r.rpush 'logs', 'another log message'
13
- r.rpush 'logs', 'yet another log message'
14
- r.rpush 'logs', 'also another log message'
15
-
16
- puts
17
- p 'contents of logs LIST'
18
-
19
- p r.lrange('logs', 0, -1)
20
-
21
- puts
22
- p 'Trim logs LIST to last 2 elements(easy circular buffer)'
23
-
24
- r.ltrim('logs', -2, -1)
25
-
26
- p r.lrange('logs', 0, -1)
data/examples/pubsub.rb DELETED
@@ -1,37 +0,0 @@
1
- require "redis"
2
-
3
- puts <<-EOS
4
- To play with this example use redis-cli from another terminal, like this:
5
-
6
- $ redis-cli publish one hello
7
-
8
- Finally force the example to exit sending the 'exit' message with:
9
-
10
- $ redis-cli publish two exit
11
-
12
- EOS
13
-
14
- redis = Redis.new
15
-
16
- trap(:INT) { puts; exit }
17
-
18
- begin
19
- redis.subscribe(:one, :two) do |on|
20
- on.subscribe do |channel, subscriptions|
21
- puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)"
22
- end
23
-
24
- on.message do |channel, message|
25
- puts "##{channel}: #{message}"
26
- redis.unsubscribe if message == "exit"
27
- end
28
-
29
- on.unsubscribe do |channel, subscriptions|
30
- puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)"
31
- end
32
- end
33
- rescue Redis::BaseConnectionError => error
34
- puts "#{error}, retrying in 1s"
35
- sleep 1
36
- retry
37
- end
@@ -1,9 +0,0 @@
1
- sentinel monitor master1 127.0.0.1 6380 2
2
- sentinel down-after-milliseconds master1 5000
3
- sentinel failover-timeout master1 15000
4
- sentinel parallel-syncs master1 1
5
-
6
- sentinel monitor master2 127.0.0.1 6381 2
7
- sentinel down-after-milliseconds master2 5000
8
- sentinel failover-timeout master2 15000
9
- sentinel parallel-syncs master2 1
@@ -1,49 +0,0 @@
1
- #! /usr/bin/env ruby
2
-
3
- # This is a helper script used together with examples/sentinel.rb
4
- # It runs two Redis masters, two slaves for each of them, and two sentinels.
5
- # After 30 seconds, the first master dies.
6
- #
7
- # You don't need to run this script yourself. Rather, use examples/sentinel.rb.
8
-
9
- require "fileutils"
10
-
11
- $pids = []
12
-
13
- at_exit do
14
- $pids.each do |pid|
15
- begin
16
- Process.kill(:INT, pid)
17
- rescue Errno::ESRCH
18
- end
19
- end
20
-
21
- Process.waitall
22
- end
23
-
24
- base = File.expand_path(File.dirname(__FILE__))
25
-
26
- # Masters
27
- $pids << spawn("redis-server --port 6380 --loglevel warning")
28
- $pids << spawn("redis-server --port 6381 --loglevel warning")
29
-
30
- # Slaves of Master 1
31
- $pids << spawn("redis-server --port 63800 --slaveof 127.0.0.1 6380 --loglevel warning")
32
- $pids << spawn("redis-server --port 63801 --slaveof 127.0.0.1 6380 --loglevel warning")
33
-
34
- # Slaves of Master 2
35
- $pids << spawn("redis-server --port 63810 --slaveof 127.0.0.1 6381 --loglevel warning")
36
- $pids << spawn("redis-server --port 63811 --slaveof 127.0.0.1 6381 --loglevel warning")
37
-
38
- FileUtils.cp(File.join(base, "sentinel.conf"), "tmp/sentinel1.conf")
39
- FileUtils.cp(File.join(base, "sentinel.conf"), "tmp/sentinel2.conf")
40
-
41
- # Sentinels
42
- $pids << spawn("redis-server tmp/sentinel1.conf --sentinel --port 26379")
43
- $pids << spawn("redis-server tmp/sentinel2.conf --sentinel --port 26380")
44
-
45
- sleep 30
46
-
47
- Process.kill(:KILL, $pids[0])
48
-
49
- Process.waitall
data/examples/sentinel.rb DELETED
@@ -1,41 +0,0 @@
1
- require 'redis'
2
-
3
- # This example creates a master-slave setup with a sentinel, then connects to
4
- # it and sends write commands in a loop.
5
- #
6
- # After 30 seconds, the master dies. You will be able to see how a new master
7
- # is elected and things continue to work as if nothing happened.
8
- #
9
- # To run this example:
10
- #
11
- # $ ruby -I./lib examples/sentinel.rb
12
- #
13
-
14
- at_exit do
15
- begin
16
- Process.kill(:INT, $redises)
17
- rescue Errno::ESRCH
18
- end
19
-
20
- Process.waitall
21
- end
22
-
23
- $redises = spawn("examples/sentinel/start")
24
-
25
- Sentinels = [{:host => "127.0.0.1", :port => 26379},
26
- {:host => "127.0.0.1", :port => 26380}]
27
- r = Redis.new(:url => "redis://master1", :sentinels => Sentinels, :role => :master)
28
-
29
- # Set keys into a loop.
30
- #
31
- # The example traps errors so that you can actually try to failover while
32
- # running the script to see redis-rb reconfiguring.
33
- (0..1000000).each{|i|
34
- begin
35
- r.set(i,i)
36
- $stdout.write("SET (#{i} times)\n") if i % 100 == 0
37
- rescue => e
38
- $stdout.write("E")
39
- end
40
- sleep(0.01)
41
- }
data/examples/sets.rb DELETED
@@ -1,36 +0,0 @@
1
- require 'rubygems'
2
- require 'redis'
3
-
4
- r = Redis.new
5
-
6
- r.del 'foo-tags'
7
- r.del 'bar-tags'
8
-
9
- puts
10
- p "create a set of tags on foo-tags"
11
-
12
- r.sadd 'foo-tags', 'one'
13
- r.sadd 'foo-tags', 'two'
14
- r.sadd 'foo-tags', 'three'
15
-
16
- puts
17
- p "create a set of tags on bar-tags"
18
-
19
- r.sadd 'bar-tags', 'three'
20
- r.sadd 'bar-tags', 'four'
21
- r.sadd 'bar-tags', 'five'
22
-
23
- puts
24
- p 'foo-tags'
25
-
26
- p r.smembers('foo-tags')
27
-
28
- puts
29
- p 'bar-tags'
30
-
31
- p r.smembers('bar-tags')
32
-
33
- puts
34
- p 'intersection of foo-tags and bar-tags'
35
-
36
- p r.sinter('foo-tags', 'bar-tags')
@@ -1,3 +0,0 @@
1
- run lambda { |env|
2
- [200, {"Content-Type" => "text/plain"}, [Redis.current.randomkey]]
3
- }
@@ -1,20 +0,0 @@
1
- require "redis"
2
-
3
- worker_processes 3
4
-
5
- # If you set the connection to Redis *before* forking,
6
- # you will cause forks to share a file descriptor.
7
- #
8
- # This causes a concurrency problem by which one fork
9
- # can read or write to the socket while others are
10
- # performing other operations.
11
- #
12
- # Most likely you'll be getting ProtocolError exceptions
13
- # mentioning a wrong initial byte in the reply.
14
- #
15
- # Thus we need to connect to Redis after forking the
16
- # worker processes.
17
-
18
- after_fork do |server, worker|
19
- Redis.current.disconnect!
20
- end
data/makefile DELETED
@@ -1,42 +0,0 @@
1
- TEST_FILES := $(shell find test -name *_test.rb -type f)
2
- REDIS_BRANCH := unstable
3
- TMP := tmp
4
- BUILD_DIR := ${TMP}/redis-${REDIS_BRANCH}
5
- TARBALL := ${TMP}/redis-${REDIS_BRANCH}.tar.gz
6
- BINARY := ${BUILD_DIR}/src/redis-server
7
- PID_PATH := ${BUILD_DIR}/redis.pid
8
- SOCKET_PATH := ${BUILD_DIR}/redis.sock
9
- PORT := 6381
10
-
11
- test: ${TEST_FILES}
12
- make start
13
- env SOCKET_PATH=${SOCKET_PATH} \
14
- ruby -v $$(echo $? | tr ' ' '\n' | awk '{ print "-r./" $$0 }') -e ''
15
- make stop
16
-
17
- ${TMP}:
18
- mkdir $@
19
-
20
- ${TARBALL}: ${TMP}
21
- wget https://github.com/antirez/redis/archive/${REDIS_BRANCH}.tar.gz -O $@
22
-
23
- ${BINARY}: ${TARBALL} ${TMP}
24
- rm -rf ${BUILD_DIR}
25
- mkdir -p ${BUILD_DIR}
26
- tar xf ${TARBALL} -C ${TMP}
27
- cd ${BUILD_DIR} && make
28
-
29
- stop:
30
- (test -f ${PID_PATH} && (kill $$(cat ${PID_PATH}) || true) && rm -f ${PID_PATH}) || true
31
-
32
- start: ${BINARY}
33
- ${BINARY} \
34
- --daemonize yes \
35
- --pidfile ${PID_PATH} \
36
- --port ${PORT} \
37
- --unixsocket ${SOCKET_PATH}
38
-
39
- clean:
40
- (test -d ${BUILD_DIR} && cd ${BUILD_DIR}/src && make clean distclean) || true
41
-
42
- .PHONY: test start stop
data/redis.gemspec DELETED
@@ -1,40 +0,0 @@
1
- require "./lib/redis/version"
2
-
3
- Gem::Specification.new do |s|
4
- s.name = "redis"
5
-
6
- s.version = Redis::VERSION
7
-
8
- s.homepage = "https://github.com/redis/redis-rb"
9
-
10
- s.summary = "A Ruby client library for Redis"
11
-
12
- s.description = <<-EOS
13
- A Ruby client that tries to match Redis' API one-to-one, while still
14
- providing an idiomatic interface.
15
- EOS
16
-
17
- s.license = "MIT"
18
-
19
- s.authors = [
20
- "Ezra Zygmuntowicz",
21
- "Taylor Weibley",
22
- "Matthew Clark",
23
- "Brian McKinney",
24
- "Salvatore Sanfilippo",
25
- "Luca Guidi",
26
- "Michel Martens",
27
- "Damian Janowski",
28
- "Pieter Noordhuis"
29
- ]
30
-
31
- s.email = ["redis-db@googlegroups.com"]
32
-
33
- s.files = `git ls-files`.split("\n")
34
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
35
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
36
-
37
- s.add_development_dependency("test-unit", "3.1.5")
38
- s.add_development_dependency("hiredis")
39
- s.add_development_dependency("em-synchrony")
40
- end
data/test/bitpos_test.rb DELETED
@@ -1,63 +0,0 @@
1
- require_relative "helper"
2
-
3
- class TestBitpos < Test::Unit::TestCase
4
-
5
- include Helper::Client
6
-
7
- def test_bitpos_empty_zero
8
- target_version "2.9.11" do
9
- r.del "foo"
10
- assert_equal(0, r.bitpos("foo", 0))
11
- end
12
- end
13
-
14
- def test_bitpos_empty_one
15
- target_version "2.9.11" do
16
- r.del "foo"
17
- assert_equal(-1, r.bitpos("foo", 1))
18
- end
19
- end
20
-
21
- def test_bitpos_zero
22
- target_version "2.9.11" do
23
- r.set "foo", "\xff\xf0\x00"
24
- assert_equal(12, r.bitpos("foo", 0))
25
- end
26
- end
27
-
28
- def test_bitpos_one
29
- target_version "2.9.11" do
30
- r.set "foo", "\x00\x0f\x00"
31
- assert_equal(12, r.bitpos("foo", 1))
32
- end
33
- end
34
-
35
- def test_bitpos_zero_end_is_given
36
- target_version "2.9.11" do
37
- r.set "foo", "\xff\xff\xff"
38
- assert_equal(24, r.bitpos("foo", 0))
39
- assert_equal(24, r.bitpos("foo", 0, 0))
40
- assert_equal(-1, r.bitpos("foo", 0, 0, -1))
41
- end
42
- end
43
-
44
- def test_bitpos_one_intervals
45
- target_version "2.9.11" do
46
- r.set "foo", "\x00\xff\x00"
47
- assert_equal(8, r.bitpos("foo", 1, 0, -1))
48
- assert_equal(8, r.bitpos("foo", 1, 1, -1))
49
- assert_equal(-1, r.bitpos("foo", 1, 2, -1))
50
- assert_equal(-1, r.bitpos("foo", 1, 2, 200))
51
- assert_equal(8, r.bitpos("foo", 1, 1, 1))
52
- end
53
- end
54
-
55
- def test_bitpos_raise_exception_if_stop_not_start
56
- target_version "2.9.11" do
57
- assert_raises(ArgumentError) do
58
- r.bitpos("foo", 0, nil, 2)
59
- end
60
- end
61
- end
62
-
63
- end
@@ -1,183 +0,0 @@
1
- require_relative "helper"
2
-
3
- class TestBlockingCommands < Test::Unit::TestCase
4
-
5
- include Helper::Client
6
-
7
- def setup
8
- super
9
-
10
- r.rpush("{zap}foo", "s1")
11
- r.rpush("{zap}foo", "s2")
12
- r.rpush("{zap}bar", "s1")
13
- r.rpush("{zap}bar", "s2")
14
- end
15
-
16
- def to_protocol(obj)
17
- case obj
18
- when String
19
- "$#{obj.length}\r\n#{obj}\r\n"
20
- when Array
21
- "*#{obj.length}\r\n" + obj.map { |e| to_protocol(e) }.join
22
- else
23
- fail
24
- end
25
- end
26
-
27
- def mock(options = {}, &blk)
28
- commands = {
29
- :blpop => lambda do |*args|
30
- sleep options[:delay] if options.has_key?(:delay)
31
- to_protocol([args.first, args.last])
32
- end,
33
- :brpop => lambda do |*args|
34
- sleep options[:delay] if options.has_key?(:delay)
35
- to_protocol([args.first, args.last])
36
- end,
37
- :brpoplpush => lambda do |*args|
38
- sleep options[:delay] if options.has_key?(:delay)
39
- to_protocol(args.last)
40
- end
41
- }
42
-
43
- redis_mock(commands, &blk)
44
- end
45
-
46
- def test_blpop
47
- assert_equal ["{zap}foo", "s1"], r.blpop("{zap}foo")
48
- assert_equal ["{zap}foo", "s2"], r.blpop(["{zap}foo"])
49
- assert_equal ["{zap}bar", "s1"], r.blpop(["{zap}bar", "{zap}foo"])
50
- assert_equal ["{zap}bar", "s2"], r.blpop(["{zap}foo", "{zap}bar"])
51
- end
52
-
53
- def test_blpop_timeout
54
- mock do |r|
55
- assert_equal ["{zap}foo", "0"], r.blpop("{zap}foo")
56
- assert_equal ["{zap}foo", "1"], r.blpop("{zap}foo", :timeout => 1)
57
- end
58
- end
59
-
60
- def test_blpop_with_old_prototype
61
- assert_equal ["{zap}foo", "s1"], r.blpop("{zap}foo", 0)
62
- assert_equal ["{zap}foo", "s2"], r.blpop("{zap}foo", 0)
63
- assert_equal ["{zap}bar", "s1"], r.blpop("{zap}bar", "{zap}foo", 0)
64
- assert_equal ["{zap}bar", "s2"], r.blpop("{zap}foo", "{zap}bar", 0)
65
- end
66
-
67
- def test_blpop_timeout_with_old_prototype
68
- mock do |r|
69
- assert_equal ["{zap}foo", "0"], r.blpop("{zap}foo", 0)
70
- assert_equal ["{zap}foo", "1"], r.blpop("{zap}foo", 1)
71
- end
72
- end
73
-
74
- def test_brpop
75
- assert_equal ["{zap}foo", "s2"], r.brpop("{zap}foo")
76
- assert_equal ["{zap}foo", "s1"], r.brpop(["{zap}foo"])
77
- assert_equal ["{zap}bar", "s2"], r.brpop(["{zap}bar", "{zap}foo"])
78
- assert_equal ["{zap}bar", "s1"], r.brpop(["{zap}foo", "{zap}bar"])
79
- end
80
-
81
- def test_brpop_timeout
82
- mock do |r|
83
- assert_equal ["{zap}foo", "0"], r.brpop("{zap}foo")
84
- assert_equal ["{zap}foo", "1"], r.brpop("{zap}foo", :timeout => 1)
85
- end
86
- end
87
-
88
- def test_brpop_with_old_prototype
89
- assert_equal ["{zap}foo", "s2"], r.brpop("{zap}foo", 0)
90
- assert_equal ["{zap}foo", "s1"], r.brpop("{zap}foo", 0)
91
- assert_equal ["{zap}bar", "s2"], r.brpop("{zap}bar", "{zap}foo", 0)
92
- assert_equal ["{zap}bar", "s1"], r.brpop("{zap}foo", "{zap}bar", 0)
93
- end
94
-
95
- def test_brpop_timeout_with_old_prototype
96
- mock do |r|
97
- assert_equal ["{zap}foo", "0"], r.brpop("{zap}foo", 0)
98
- assert_equal ["{zap}foo", "1"], r.brpop("{zap}foo", 1)
99
- end
100
- end
101
-
102
- def test_brpoplpush
103
- assert_equal "s2", r.brpoplpush("{zap}foo", "{zap}qux")
104
- assert_equal ["s2"], r.lrange("{zap}qux", 0, -1)
105
- end
106
-
107
- def test_brpoplpush_timeout
108
- mock do |r|
109
- assert_equal "0", r.brpoplpush("{zap}foo", "{zap}bar")
110
- assert_equal "1", r.brpoplpush("{zap}foo", "{zap}bar", :timeout => 1)
111
- end
112
- end
113
-
114
- def test_brpoplpush_with_old_prototype
115
- assert_equal "s2", r.brpoplpush("{zap}foo", "{zap}qux", 0)
116
- assert_equal ["s2"], r.lrange("{zap}qux", 0, -1)
117
- end
118
-
119
- def test_brpoplpush_timeout_with_old_prototype
120
- mock do |r|
121
- assert_equal "0", r.brpoplpush("{zap}foo", "{zap}bar", 0)
122
- assert_equal "1", r.brpoplpush("{zap}foo", "{zap}bar", 1)
123
- end
124
- end
125
-
126
- driver(:ruby, :hiredis) do
127
- def test_blpop_socket_timeout
128
- mock(:delay => 1 + OPTIONS[:timeout] * 2) do |r|
129
- assert_raises(Redis::TimeoutError) do
130
- r.blpop("{zap}foo", :timeout => 1)
131
- end
132
- end
133
- end
134
-
135
- def test_brpop_socket_timeout
136
- mock(:delay => 1 + OPTIONS[:timeout] * 2) do |r|
137
- assert_raises(Redis::TimeoutError) do
138
- r.brpop("{zap}foo", :timeout => 1)
139
- end
140
- end
141
- end
142
-
143
- def test_brpoplpush_socket_timeout
144
- mock(:delay => 1 + OPTIONS[:timeout] * 2) do |r|
145
- assert_raises(Redis::TimeoutError) do
146
- r.brpoplpush("{zap}foo", "{zap}bar", :timeout => 1)
147
- end
148
- end
149
- end
150
- end
151
-
152
- def assert_takes_longer_than_client_timeout
153
- timeout = OPTIONS[:timeout]
154
- delay = timeout * 2
155
-
156
- mock(:delay => delay) do |r|
157
- t1 = Time.now
158
- yield(r)
159
- t2 = Time.now
160
-
161
- assert timeout == r._client.timeout
162
- assert delay <= (t2 - t1)
163
- end
164
- end
165
-
166
- def test_blpop_disable_client_timeout
167
- assert_takes_longer_than_client_timeout do |r|
168
- assert_equal ["foo", "0"], r.blpop("foo")
169
- end
170
- end
171
-
172
- def test_brpop_disable_client_timeout
173
- assert_takes_longer_than_client_timeout do |r|
174
- assert_equal ["foo", "0"], r.brpop("foo")
175
- end
176
- end
177
-
178
- def test_brpoplpush_disable_client_timeout
179
- assert_takes_longer_than_client_timeout do |r|
180
- assert_equal "0", r.brpoplpush("foo", "bar")
181
- end
182
- end
183
- end