mock_redis 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ ### 0.8.0
2
+ * Fixed `watch` to return OK when passed no block
3
+ * Implemented `pexpire`, `pexpireat`
4
+ * Fixed `expire` to use millisecond precision
5
+
1
6
  ### 0.7.0
2
7
  * Implemented `mapped_mget`, `mapped_mset`, `mapped_msetnx`
3
8
  * Fixed `rpoplpush` when the `rpop` is nil
@@ -66,7 +66,12 @@ class MockRedis
66
66
  end
67
67
 
68
68
  def expire(key, seconds)
69
- expireat(key, @base.now.to_i + seconds.to_i)
69
+ pexpire(key, seconds.to_i * 1000)
70
+ end
71
+
72
+ def pexpire(key, ms)
73
+ now_ms = (@base.now.to_f * 1000).to_i
74
+ pexpireat(key, now_ms + ms.to_i)
70
75
  end
71
76
 
72
77
  def expireat(key, timestamp)
@@ -74,8 +79,17 @@ class MockRedis
74
79
  raise Redis::CommandError, "ERR value is not an integer or out of range"
75
80
  end
76
81
 
82
+ pexpireat(key, timestamp.to_i * 1000)
83
+ end
84
+
85
+ def pexpireat(key, timestamp_ms)
86
+ unless looks_like_integer?(timestamp_ms.to_s)
87
+ raise Redis::CommandError, "ERR value is not an integer or out of range"
88
+ end
89
+
77
90
  if exists(key)
78
- set_expiration(key, @base.time_at(timestamp.to_i))
91
+ timestamp = timestamp_ms.to_i / 1000.0
92
+ set_expiration(key, @base.time_at(timestamp))
79
93
  true
80
94
  else
81
95
  false
@@ -78,7 +78,11 @@ class MockRedis
78
78
  end
79
79
 
80
80
  def watch(_)
81
- yield self if block_given?
81
+ if block_given?
82
+ yield self
83
+ else
84
+ 'OK'
85
+ end
82
86
  end
83
87
 
84
88
  end
@@ -1,3 +1,3 @@
1
1
  class MockRedis
2
- VERSION = '0.7.0'
2
+ VERSION = '0.8.0'
3
3
  end
@@ -52,7 +52,15 @@ describe "#expire(key, seconds)" do
52
52
  @mock.expire(@key, 5)
53
53
  @mock.expire(@key, 6)
54
54
 
55
- Time.stub!(:now).and_return(@now + 5)
55
+ Time.stub!(:now).and_return(@now + 5)
56
+ @mock.get(@key).should_not be_nil
57
+ end
58
+
59
+ it "has millisecond precision" do
60
+ @now = Time.at(@now.to_i + 0.5)
61
+ Time.stub!(:now).and_return(@now)
62
+ @mock.expire(@key, 5)
63
+ Time.stub!(:now).and_return(@now + 4.9)
56
64
  @mock.get(@key).should_not be_nil
57
65
  end
58
66
 
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#pexpire(key, ms)" do
4
+ before do
5
+ @key = 'mock-redis-test:pexpire'
6
+ @redises.set(@key, 'spork')
7
+ end
8
+
9
+ it "returns true for a key that exists" do
10
+ @redises.pexpire(@key, 1).should be_true
11
+ end
12
+
13
+ it "returns false for a key that does not exist" do
14
+ @redises.pexpire('mock-redis-test:nonesuch', 1).should be_false
15
+ end
16
+
17
+ it "removes a key immediately when ms==0" do
18
+ @redises.pexpire(@key, 0)
19
+ @redises.get(@key).should be_nil
20
+ end
21
+
22
+ it "raises an error if ms is bogus" do
23
+ lambda do
24
+ @redises.pexpireat(@key, 'a couple minutes or so')
25
+ end.should raise_error(RuntimeError)
26
+ end
27
+
28
+ it "stringifies key" do
29
+ @redises.pexpire(@key.to_sym, 9).should == true
30
+ end
31
+
32
+ context "[mock only]" do
33
+ # These are mock-only since we can't actually manipulate time in
34
+ # the real Redis.
35
+
36
+ before(:all) do
37
+ @mock = @redises.mock
38
+ end
39
+
40
+ before do
41
+ @now = Time.now
42
+ Time.stub!(:now).and_return(@now)
43
+ end
44
+
45
+ it "removes keys after enough time has passed" do
46
+ @mock.pexpire(@key, 5)
47
+ Time.stub!(:now).and_return(@now + 0.006)
48
+ @mock.get(@key).should be_nil
49
+ end
50
+
51
+ it "updates an existing pexpire time" do
52
+ @mock.pexpire(@key, 5)
53
+ @mock.pexpire(@key, 6)
54
+
55
+ Time.stub!(:now).and_return(@now + 0.005)
56
+ @mock.get(@key).should_not be_nil
57
+ end
58
+
59
+ context "expirations on a deleted key" do
60
+ before { @mock.del(@key) }
61
+
62
+ it "cleans up the expiration once the key is gone (string)" do
63
+ @mock.set(@key, 'string')
64
+ @mock.pexpire(@key, 2)
65
+ @mock.del(@key)
66
+ @mock.set(@key, 'string')
67
+
68
+ Time.stub!(:now).and_return(@now + 0.003)
69
+
70
+ @mock.get(@key).should_not be_nil
71
+ end
72
+
73
+ it "cleans up the expiration once the key is gone (list)" do
74
+ @mock.rpush(@key, 'coconuts')
75
+ @mock.pexpire(@key, 2)
76
+ @mock.rpop(@key)
77
+
78
+ @mock.rpush(@key, 'coconuts')
79
+
80
+ Time.stub!(:now).and_return(@now + 0.003)
81
+
82
+ @mock.lindex(@key, 0).should_not be_nil
83
+ end
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#pexpireat(key, timestamp_ms)" do
4
+ before do
5
+ @key = 'mock-redis-test:pexpireat'
6
+ @redises.set(@key, 'spork')
7
+ end
8
+
9
+ it "returns true for a key that exists" do
10
+ @redises.pexpireat(@key, (Time.now.to_f * 1000).to_i + 1).should be_true
11
+ end
12
+
13
+ it "returns false for a key that does not exist" do
14
+ @redises.pexpireat('mock-redis-test:nonesuch',
15
+ (Time.now.to_f * 1000).to_i + 1).should be_false
16
+ end
17
+
18
+ it "removes a key immediately when timestamp is now" do
19
+ @redises.pexpireat(@key, (Time.now.to_f * 1000).to_i)
20
+ @redises.get(@key).should be_nil
21
+ end
22
+
23
+ it "raises an error if you don't give it a Unix timestamp" do
24
+ lambda do
25
+ @redises.pexpireat(@key, Time.now) # oops, forgot .to_i
26
+ end.should raise_error(RuntimeError)
27
+ end
28
+
29
+ context "[mock only]" do
30
+ # These are mock-only since we can't actually manipulate time in
31
+ # the real Redis.
32
+
33
+ before(:all) do
34
+ @mock = @redises.mock
35
+ end
36
+
37
+ before do
38
+ @now = Time.now
39
+ Time.stub!(:now).and_return(@now)
40
+ end
41
+
42
+ it "removes keys after enough time has passed" do
43
+ @mock.pexpireat(@key, (@now.to_f * 1000).to_i + 5)
44
+ Time.stub!(:now).and_return(@now + 0.006)
45
+ @mock.get(@key).should be_nil
46
+ end
47
+ end
48
+ end
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe '#watch(key)' do
4
- it "responds with nil" do
5
- @redises.watch('mock-redis-test').should be_nil
4
+ it "returns 'OK'" do
5
+ @redises.watch('mock-redis-test').should == 'OK'
6
6
  end
7
7
 
8
8
  it 'EXECs its MULTI on successes' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock_redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-09 00:00:00.000000000 Z
13
+ date: 2013-06-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -161,6 +161,8 @@ files:
161
161
  - spec/commands/mset_spec.rb
162
162
  - spec/commands/msetnx_spec.rb
163
163
  - spec/commands/persist_spec.rb
164
+ - spec/commands/pexpire_spec.rb
165
+ - spec/commands/pexpireat_spec.rb
164
166
  - spec/commands/ping_spec.rb
165
167
  - spec/commands/pipelined_spec.rb
166
168
  - spec/commands/quit_spec.rb
@@ -315,6 +317,8 @@ test_files:
315
317
  - spec/commands/mset_spec.rb
316
318
  - spec/commands/msetnx_spec.rb
317
319
  - spec/commands/persist_spec.rb
320
+ - spec/commands/pexpire_spec.rb
321
+ - spec/commands/pexpireat_spec.rb
318
322
  - spec/commands/ping_spec.rb
319
323
  - spec/commands/pipelined_spec.rb
320
324
  - spec/commands/quit_spec.rb