Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support returning time in pipelines #179

Merged
merged 1 commit into from Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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