Skip to content

Commit

Permalink
timecop rather than sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
akerl committed May 16, 2014
1 parent 34a542b commit 072f1b8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions basiccache.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'coveralls', '~> 0.7.0'
s.add_development_dependency 'rspec', '~> 2.14.1'
s.add_development_dependency 'fuubar', '~> 1.3.2'
s.add_development_dependency 'timecop', '~> 0.7.1'
end
29 changes: 17 additions & 12 deletions spec/caches/timecache_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'spec_helper'
require 'timecop'

describe BasicCache::TimeCache do
let(:cache) do
cache = BasicCache::TimeCache.new(lifetime: 1)
cache = BasicCache::TimeCache.new(lifetime: 10)
cache.cache('a') { 3 }
cache.cache(:b) { 5 }
cache.cache { 9 }
Expand All @@ -17,7 +18,7 @@
expect(subject.store).to be_an_instance_of BasicCache::Store
end
it 'has a set lifetime' do
expect(cache.lifetime).to eq 1
expect(cache.lifetime).to eq 10
expect(BasicCache::TimeCache.new.lifetime).to eq 60
end
describe '#size' do
Expand All @@ -27,8 +28,9 @@
end
it 'does not include expired items in size' do
expect(cache.size).to eql 3
sleep 2
expect(cache.size).to eql 0
Timecop.freeze(Time.now + 60) do
expect(cache.size).to eql 0
end
end
end
describe '#cache' do
Expand All @@ -44,8 +46,9 @@
end
it 'expires values after their lifetime' do
expect(cache.include? 'a').to be_true
sleep 2
expect(cache.include? 'a').to be_false
Timecop.freeze(Time.now + 60) do
expect(cache.include? 'a').to be_false
end
end
end
describe '#include?' do
Expand All @@ -64,8 +67,9 @@
end
it 'will not return an expired key' do
expect(cache['a']).to eql 3
sleep 2
expect { cache['a'] }.to raise_error KeyError
Timecop.freeze(Time.now + 60) do
expect { cache['a'] }.to raise_error KeyError
end
end
end
describe '#clear!' do
Expand Down Expand Up @@ -93,10 +97,11 @@
describe '#prune' do
it 'prunes invalid cache entries' do
expect(cache.store.size).to eql 3
sleep 2
expect(cache.store.size).to eql 3
expect(cache.prune).to eql [:a, :b, names[2]]
expect(cache.store.size).to eql 0
Timecop.freeze(Time.now + 60) do
expect(cache.store.size).to eql 3
expect(cache.prune).to eql [:a, :b, names[2]]
expect(cache.store.size).to eql 0
end
end
end
end
8 changes: 5 additions & 3 deletions spec/methodcacher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'timecop'

##
# Example class for testing method caching
Expand All @@ -8,7 +9,7 @@ class Example
def initialize(skip_cache = false)
return if skip_cache
enable_caching [:repeat]
enable_caching [:time_repeat], BasicCache::TimeCache.new(lifetime: 1)
enable_caching [:time_repeat], BasicCache::TimeCache.new(lifetime: 10)
end

def repeat(input)
Expand Down Expand Up @@ -44,8 +45,9 @@ def not_cached(input)
it 'allows a user-supplied cache object' do
expect(test_object.time_repeat 2).to eql 2
expect(test_object.time_repeat 3).to eql 2
sleep 2
expect(test_object.time_repeat 4).to eql 4
Timecop.freeze(Time.now + 60) do
expect(test_object.time_repeat 4).to eql 4
end
end
it 'does not override other methods' do
expect(test_object.not_cached 7).to eql 7
Expand Down

0 comments on commit 072f1b8

Please sign in to comment.