From cc51a087927ef65eccfa2e6a0b30a199ff8a4bb9 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 13 Dec 2018 15:20:26 +0100 Subject: [PATCH] Merge pull request #783 from byroot/fix-bpop-timeout Handle integer like objects as timeout of blpop and brpop --- lib/redis.rb | 7 +++---- lib/redis/distributed.rb | 7 +++---- test/lint/blocking_commands.rb | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/redis.rb b/lib/redis.rb index 1ac8ab9f2..264c436fe 100644 --- a/lib/redis.rb +++ b/lib/redis.rb @@ -1159,12 +1159,11 @@ def rpoplpush(source, destination) def _bpop(cmd, args, &blk) options = {} - case args.last - when Hash + if args.last.is_a?(Hash) options = args.pop - when Integer + elsif args.last.respond_to?(:to_int) # Issue deprecation notice in obnoxious mode... - options[:timeout] = args.pop + options[:timeout] = args.pop.to_int end if args.size > 1 diff --git a/lib/redis/distributed.rb b/lib/redis/distributed.rb index ba3a3fae1..7093fb00a 100644 --- a/lib/redis/distributed.rb +++ b/lib/redis/distributed.rb @@ -403,12 +403,11 @@ def rpoplpush(source, destination) def _bpop(cmd, args) options = {} - case args.last - when Hash + if args.last.is_a?(Hash) options = args.pop - when Integer + elsif args.last.respond_to?(:to_int) # Issue deprecation notice in obnoxious mode... - options[:timeout] = args.pop + options[:timeout] = args.pop.to_int end if args.size > 1 diff --git a/test/lint/blocking_commands.rb b/test/lint/blocking_commands.rb index c20343ba8..9f7382036 100644 --- a/test/lint/blocking_commands.rb +++ b/test/lint/blocking_commands.rb @@ -67,6 +67,22 @@ def test_blpop_timeout end end + class FakeDuration + def initialize(int) + @int = int + end + + def to_int + @int + end + end + + def test_blpop_integer_like_timeout + mock do |r| + assert_equal ["{zap}foo", "1"], r.blpop("{zap}foo", FakeDuration.new(1)) + end + end + def test_blpop_with_old_prototype assert_equal ['{zap}foo', 's1'], r.blpop('{zap}foo', 0) assert_equal ['{zap}foo', 's2'], r.blpop('{zap}foo', 0)