Skip to content

Commit 562e2e4

Browse files
authored
Fix Database.call with expire methods (#334)
# What? * It fixes calls to `expire*` methods when the calling happens through the `.call` method of MockRedis::Database * It fixes a test that will inconsistently fail when we run tests one after another, within a few seconds of each run # Why? `expire` values are not respected when we set these via the Database object. So tests that depend on testing that we set the ttl correctly will fail. This happens because the `.call` method fails to call successfully `public_send` due to the need to parse out the kwarg arguments. # How? The fix was to identify if we have an `expire` call in the command. If that is the case, we call a `send_expire` method that will correctly transform the call so that we can call those methods with the correct format. For the test, I noticed that adding a slight wait for 1 millisecond was enough to consistently get that test passed # Notes I didn't add a version number because I didn't want to be be arrogant and assume that the PR will be passed. I am happy to modify things or try some other solution if you prefer that. Thanks so much for such a useful gem
1 parent 1e720f2 commit 562e2e4

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/mock_redis/database.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ def initialize_copy(_source)
5151
def call(*command, &_block)
5252
# allow for single array argument or multiple arguments
5353
command = command[0] if command.length == 1
54-
public_send(command[0].downcase, *command[1..])
54+
55+
if command[0].downcase.to_s.include?('expire')
56+
send_expires(command)
57+
else
58+
public_send(command[0].downcase, *command[1..])
59+
end
5560
end
5661

5762
def auth(_)
@@ -351,6 +356,11 @@ def looks_like_float?(str)
351356
!!Float(str) rescue false
352357
end
353358

359+
def send_expires(command)
360+
command, key, ttl, option = *command
361+
public_send(command, key, ttl, option.downcase.to_sym => option)
362+
end
363+
354364
def should_update_expiration?(expiry, new_expiry, nx:, xx:, lt:, gt:) # rubocop:disable Metrics/ParameterLists
355365
return false if nx && expiry || xx && !expiry
356366
return false if lt && expiry && new_expiry > expiry

spec/commands/pexpireat_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def now_ms
2020
end
2121

2222
it 'removes a key immediately when timestamp is now' do
23+
# Solves inconsistent failures when running test one after another
24+
sleep 0.001
2325
@redises.pexpireat(@key, now_ms)
2426
expect(@redises.get(@key)).to be_nil
2527
end

0 commit comments

Comments
 (0)