-
Notifications
You must be signed in to change notification settings - Fork 0
/
rimc_spec.rb
102 lines (81 loc) · 2.93 KB
/
rimc_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
RSpec.describe Rimc do
it 'has a version number' do
expect(Rimc::VERSION).not_to be nil
end
def set_rimc_cache_entries(entries)
Rimc.instance_variable_set :@cache_entries, entries
end
def build_single_hash(key, value)
{ key => value }
end
def set_current_time(current_time)
allow(Time).to receive(:now).and_return current_time
end
describe '.set' do
let(:cache_key) { double('cache key') }
let(:cached_object) { double('cached_object') }
let(:ttl) { 60 }
let(:current_time) { Time.new(2018, 9, 26, 1, 1, 0) }
before do
set_current_time(current_time)
end
subject do
Rimc.set(cache_key, cached_object, ttl)
Rimc.send(:cache_entries)[cache_key]
end
it { expect(subject.cached_object).to eq cached_object }
it { expect(subject.expire_time - current_time).to eq ttl }
end
describe '.get' do
context 'expired' do
let(:expire_time) { Time.new(2018, 9, 26, 1, 1, 1) }
let(:current_time) { Time.new(2018, 9, 26, 1, 1, 2) }
let(:cache_entry) { build(:cache_entry, expire_time: expire_time) }
let(:cache_key) { double('cache key') }
before do
set_rimc_cache_entries build_single_hash(cache_key, cache_entry)
set_current_time(current_time)
end
subject { Rimc.get(cache_key) }
it { expect(subject).to be_nil }
end
context 'not expired' do
let(:expire_time) { Time.new(2018, 9, 26, 1, 1, 1) }
let(:current_time) { Time.new(2018, 9, 26, 1, 1, 0) }
let(:cache_entry) { build(:cache_entry, expire_time: expire_time) }
let(:cache_key) { double('cache key') }
before do
set_rimc_cache_entries build_single_hash(cache_key, cache_entry)
set_current_time(current_time)
end
subject { Rimc.get(cache_key) }
it { expect(subject).to eq cache_entry.cached_object }
end
end
describe '.cache' do
context 'expired' do
let(:cache_key) { double('cache key') }
let(:cache_entry) { build(:cache_entry) }
let(:cached_object) { double('cache object') }
let(:ttl) { 60 }
before do
allow(cache_entry).to receive(:expired?).and_return true
set_rimc_cache_entries build_single_hash(cache_key, cache_entry)
end
it { expect(Rimc.cache(cache_key, ttl)).not_to eq cache_entry.cached_object }
it { expect { |b| Rimc.cache(cache_key, ttl, &b) }.to yield_control }
end
context 'not expired' do
let(:cache_key) { double('cache key') }
let(:cache_entry) { build(:cache_entry) }
let(:cached_object) { double('cache object') }
let(:ttl) { 60 }
before do
allow(cache_entry).to receive(:expired?).and_return false
set_rimc_cache_entries build_single_hash(cache_key, cache_entry)
end
it { expect(Rimc.cache(cache_key, ttl)).to eq cache_entry.cached_object }
it { expect { |b| Rimc.cache(cache_key, ttl, &b) }.not_to yield_control }
end
end
end