guilleiguaran/fakeredis

SETNX returns 0 instead of false

Closed this issue · 1 comments

Using redis-rb 4.1.2, when I issue a SET command with NX, it returns false if the key is already set. My understanding is that Redis itself returns zero (https://redis.io/commands/setnx), but the library is handling converting 1/0 to true/false. Using version 0.7.0 of this library, when I issue the same command, I receive 0 instead of false when the key is already set.

This is true on both MRI (ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin17]):

2.6.0 :001 > require 'redis'
 => true
2.6.0 :002 > r = Redis.new
 => #<Redis client v4.1.3 for redis://127.0.0.1:6379/0>
2.6.0 :003 > r.set('a', 'test', ex: 3, nx: true)
 => true
2.6.0 :004 > r.set('a', 'test', ex: 3, nx: true)
 => false
2.6.0 :005 > require 'fakeredis'
 => true
2.6.0 :006 > r = Redis.new
 => #<Redis client v4.1.3 for redis://127.0.0.1:6379/0>
2.6.0 :007 > r.set('a', 'test', ex: 3, nx: true)
 => true
2.6.0 :008 > r.set('a', 'test', ex: 3, nx: true)
 => 0

And JRuby (jruby 9.2.7.0 (2.5.3) 2019-04-09 8a269e3 Java HotSpot(TM) 64-Bit Server VM 25.151-b12 on 1.8.0_151-b12 +jit [darwin-x86_64]):

jruby-9.2.7.0 :001 > require 'redis'
 => true
jruby-9.2.7.0 :002 > r = Redis.new
 => #<Redis client v4.1.3 for redis://127.0.0.1:6379/0>
jruby-9.2.7.0 :003 > r.set('a', 'test', ex: 3, nx: true)
 => true
jruby-9.2.7.0 :004 > r.set('a', 'test', ex: 3, nx: true)
 => false
jruby-9.2.7.0 :005 > require 'fakeredis'
 => true
jruby-9.2.7.0 :006 > r = Redis.new
 => #<Redis client v4.1.3 for redis://127.0.0.1:6379/0>
jruby-9.2.7.0 :007 > r.set('a', 'test', ex: 3, nx: true)
 => true
jruby-9.2.7.0 :008 > r.set('a', 'test', ex: 3, nx: true)
 => 0

My mistake, this appears to be resolved on master:

require "bundler/inline"

gemfile do
  source 'https://rubygems.org'

  git 'https://github.com/guilleiguaran/fakeredis.git', branch: 'master' do
    gem 'fakeredis'
  end

  gem "redis", "= 4.1.2"
end

require "redis"

r = Redis.new
r.set('key', 'test', nx: true)
puts r.set('key', 'test', nx: true)
=> false

require "fakeredis"

r = Redis.new
r.set('key', 'test', nx: true)
puts r.set('key', 'test', nx: true)
=> false

The above has both of the second set statements return zero with:

gem 'fakeredis', '= 0.7.0'

But returns false as expected with the latest master.