forked from rails/sprockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cache_store.rb
202 lines (167 loc) · 4.79 KB
/
test_cache_store.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# frozen_string_literal: true
require 'minitest/autorun'
require 'tmpdir'
require 'fileutils'
require 'sprockets/cache'
require 'sprockets/cache/file_store'
require 'sprockets/cache/memory_store'
require 'sprockets/cache/null_store'
module CacheStoreNullTests
def test_read
refute @store.get("foo")
end
def test_write
result = @store.set("foo", "bar")
assert_equal "bar", result
end
def test_write_and_read_miss
@store.set("foo", "bar")
refute @store.get("foo")
end
def test_fetch
result = @store.fetch("foo") { "bar" }
assert_equal "bar", result
end
def test_clear
result = @store.clear
assert result, "@store.clear returned #{result} instead of true"
end
end
module CacheStoreTests
def test_read_miss
refute @store.get("missing")
end
def test_write
result = @store.set("foo", "bar")
assert_equal "bar", result
end
def test_write_and_read_hit
@store.set("foo", "bar")
assert_equal "bar", @store.get("foo")
end
def test_multiple_write_and_read_hit
@store.set("foo", "1")
@store.set("bar", "2")
@store.set("baz", "3")
assert_equal "1", @store.get("foo")
assert_equal "2", @store.get("bar")
assert_equal "3", @store.get("baz")
end
def test_large_write_and_read_hit
data = ("a"..."zzz").to_a.join
@store.set("foo", data)
assert_equal data, @store.get("foo")
end
def test_delete
@store.set("foo", "bar")
assert_equal "bar", @store.get("foo")
@store.set("foo", nil)
assert_nil @store.get("foo")
end
def test_fetch
result = @store.fetch("user") { "josh" }
assert_equal "josh", result
end
def test_clear
@store.set("foo", "bar")
assert_equal "bar", @store.get("foo")
result = @store.clear
assert result, "@store.clear returned #{result} instead of true"
assert_nil @store.get("foo")
end
end
class TestNullStore < MiniTest::Test
def setup
@_store = Sprockets::Cache::NullStore.new
@store = Sprockets::Cache.new(Sprockets::Cache::NullStore.new)
end
def test_inspect
assert_equal "#<Sprockets::Cache local=#<Sprockets::Cache::MemoryStore size=0/1024> store=#<Sprockets::Cache::NullStore>>", @store.inspect
assert_equal "#<Sprockets::Cache::NullStore>", @_store.inspect
end
include CacheStoreNullTests
end
class TestMemoryStore < MiniTest::Test
def setup
@_store = Sprockets::Cache::MemoryStore.new
@store = Sprockets::Cache.new(@_store)
end
def test_inspect
assert_equal "#<Sprockets::Cache::MemoryStore size=0/1000>", @_store.inspect
end
include CacheStoreTests
def test_get_with_lru
@_store.set(:a, 1)
@_store.set(:b, 2)
@_store.set(:c, 3)
assert_equal [1, 2, 3], @_store.instance_variable_get(:@cache).values
@_store.get(:a)
@_store.set(:d, 4)
assert_equal [2, 3, 1, 4], @_store.instance_variable_get(:@cache).values
end
def test_set_with_lru
@_store.set(:a, 1)
@_store.set(:b, 2)
@_store.set(:c, 3)
assert_equal [1, 2, 3], @_store.instance_variable_get(:@cache).values
@_store.set(:a, 1)
@_store.set(:d, 4)
assert_equal [2, 3, 1, 4], @_store.instance_variable_get(:@cache).values
end
end
class TestZeroMemoryStore < MiniTest::Test
def setup
@_store = Sprockets::Cache::MemoryStore.new(0)
@store = Sprockets::Cache.new(@_store)
end
def test_inspect
assert_equal "#<Sprockets::Cache::MemoryStore size=0/0>", @_store.inspect
end
include CacheStoreNullTests
end
class TestFileStore < MiniTest::Test
def setup
@root = Dir::mktmpdir "sprockets-file-store"
@_store = Sprockets::Cache::FileStore.new(@root)
@store = Sprockets::Cache.new(@_store)
end
def teardown
FileUtils.rm_rf(@root)
end
def test_inspect
Dir::mktmpdir "sprockets-file-store-inspect" do |dir|
store = Sprockets::Cache::FileStore.new(dir)
assert_equal "#<Sprockets::Cache::FileStore size=0/26214400>", store.inspect
end
end
def test_corrupted_read
File.write(File.join(@root, "corrupt.cache"), "w") do |f|
f.write("corrupt")
end
refute @_store.get("corrupt")
end
def test_clear_store_dir_not_exist
@cache_dir = File.join(Dir::tmpdir, 'sprockets')
refute File.exist?(@cache_dir)
@store = Sprockets::Cache::FileStore.new(@cache_dir)
assert @store.clear
end
include CacheStoreTests
end
class TestZeroFileStore < MiniTest::Test
def setup
@tmpdir = Dir::mktmpdir "sprockets-file-store-zero"
@_store = Sprockets::Cache::FileStore.new(@tmpdir, 0)
@store = Sprockets::Cache.new(@_store)
end
def teardown
FileUtils.rm_rf @tmpdir
end
def test_inspect
Dir.mktmpdir "sprockets-file-store-inspect" do |dir|
store = Sprockets::Cache::FileStore.new(dir, 0)
assert_equal "#<Sprockets::Cache::FileStore size=0/0>", store.inspect
end
end
include CacheStoreNullTests
end