Skip to content

Commit

Permalink
feat: support returning time in pipelines (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanMertz committed Apr 6, 2020
1 parent 1a1f716 commit 5ef8f8b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
7 changes: 0 additions & 7 deletions lib/mock_redis.rb
Expand Up @@ -64,13 +64,6 @@ def db
options[:db]
end

def now
current_time = options[:time_class].now
miliseconds = (current_time.to_r - current_time.to_i) * 1_000
[current_time.to_i, miliseconds.to_i]
end
alias time now

def time_at(timestamp)
options[:time_class].at(timestamp)
end
Expand Down
11 changes: 9 additions & 2 deletions lib/mock_redis/database.rb
Expand Up @@ -159,7 +159,7 @@ def scan_each(opts = {}, &block)
end

def lastsave
@base.now.first
now.first
end

def persist(key)
Expand Down Expand Up @@ -240,6 +240,13 @@ def pttl(key)
end
end

def now
current_time = @base.options[:time_class].now
miliseconds = (current_time.to_r - current_time.to_i) * 1_000
[current_time.to_i, miliseconds.to_i]
end
alias time now

def type(key)
if !exists(key)
'none'
Expand Down Expand Up @@ -344,7 +351,7 @@ def zero_pad(string, desired_length)
# This method isn't private, but it also isn't a Redis command, so
# it doesn't belong up above with all the Redis commands.
def expire_keys
now, miliseconds = @base.now
now, miliseconds = self.now
now_ms = now * 1_000 + miliseconds

to_delete = expire_times.take_while do |(time, _key)|
Expand Down
20 changes: 20 additions & 0 deletions spec/commands/pipelined_spec.rb
Expand Up @@ -66,6 +66,26 @@
end
end

context 'with redis time return value' do
let(:time_stub) { double 'Time', :now => Time.new(2019, 1, 2, 3, 4, 6, '+00:00') }
let(:options) { { :time_class => time_stub } }

subject { MockRedis.new(options) }

it 'returns the time value' do
subject.set('foo', 'bar')

results = subject.pipelined do
subject.get('foo')
subject.host # defined on MockRedis, so not captured
subject.time
subject.echo('baz')
end

expect(results).to eq(['bar', [1_546_398_246, 0], 'baz'])
end
end

context 'with nested pipelines' do
let(:key1) { 'hello' }
let(:key2) { 'world' }
Expand Down

0 comments on commit 5ef8f8b

Please sign in to comment.