-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathcache_spec.rb
80 lines (67 loc) · 1.53 KB
/
cache_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
require_relative '../spec_helper'
describe RDF::Util::Cache do
subject(:cache) do
described_class.new(10)
end
describe '#capacity' do
it 'returns the cache size' do
expect(cache.capacity).to eq 10
end
end
describe '#size' do
it 'returns the cache size' do
cache[:key] = {}
expect(cache.size).to eq 1
end
end
describe '#[]' do
it 'returns the value' do
cache[:key] = {}
expect(cache[:key]).to eq({})
end
end
describe '#[]=' do
context 'when the cache is not full' do
it 'stores the value' do
expect {
cache[:key] = {}
}.to change(cache, :size).by(1)
end
it 'returns the value' do
expect(cache[:key] = {}).to eq({})
end
end
context 'when the cache is full' do
before do
10.times { |i| cache[i] = {} }
end
it 'does not store the value' do
expect {
cache[:key] = {}
}.not_to change(cache, :size)
end
it 'returns the value' do
expect(cache[:key] = {}).to eq({})
end
end
end
context 'when the GC starts' do
before do
100.times { |i| cache[i] = {}; nil }
end
# Sometimes the last reference is not gc
it 'cleans the unused references' do
expect {
GC.start
}.to change(cache, :size).by_at_most(-9)
end
end
describe '#delete' do
before do
cache[:key] = {}
end
it 'delete the value' do
expect { cache.delete(:key) }.to change(cache, :size).to(0)
end
end
end