Skip to content

Commit

Permalink
Merge pull request #783 from byroot/fix-bpop-timeout
Browse files Browse the repository at this point in the history
Handle integer like objects as timeout of blpop and brpop
  • Loading branch information
byroot committed Dec 13, 2018
1 parent 7ff90f1 commit cc51a08
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
7 changes: 3 additions & 4 deletions lib/redis.rb
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions lib/redis/distributed.rb
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/lint/blocking_commands.rb
Expand Up @@ -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)
Expand Down

0 comments on commit cc51a08

Please sign in to comment.