rails-cache-tags 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,99 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__))
2
-
3
- require "test_helper"
4
- require "caching_test"
5
-
6
- module CacheTagsBehavior
7
- def test_read_and_write_with_tags
8
- @cache.write("foo", "bar", :tags => "baz")
9
- assert_equal 'bar', @cache.read('foo')
10
- end
11
-
12
- def test_read_after_tag_deleted
13
- @cache.write("foo", "bar", :tags => "baz")
14
- @cache.delete_tag("baz")
15
-
16
- assert_nil @cache.read("foo")
17
- end
18
-
19
- def test_read_after_another_tag_deleted
20
- @cache.write("foo", "bar", :tags => "baz")
21
- @cache.delete_tag("fu")
22
-
23
- assert_equal 'bar', @cache.read('foo')
24
- end
25
-
26
- def test_read_and_write_with_multiple_tags
27
- @cache.write("foo", "bar", :tags => [:baz, :kung])
28
- assert_equal 'bar', @cache.read('foo')
29
- end
30
-
31
- def test_read_after_one_of_tags_deleted
32
- @cache.write("foo", "bar", :tags => [:baz, :kung])
33
- @cache.delete_tag :kung
34
-
35
- assert_nil @cache.read("foo")
36
- end
37
-
38
- def test_read_after_another_of_multiple_tags_deleted
39
- @cache.write("foo", "bar", :tags => [:baz, :kung])
40
- @cache.delete_tag("fu")
41
-
42
- assert_equal 'bar', @cache.read('foo')
43
- end
44
-
45
- def test_read_with_small_default_expiration_time
46
- cache = if is_a?(FileStoreTest)
47
- @cache.class.new @cache.cache_path, :expires_in => 0.001
48
- else
49
- @cache.class.new :expires_in => 0.001
50
- end
51
-
52
- cache.write("foo", "bar", :tags => "baz", :expires_in => 2)
53
- sleep 0.02
54
-
55
- assert_equal 'bar', cache.read('foo')
56
- end
57
-
58
- def test_exists_with_tags
59
- @cache.write("foo", "bar", :tags => "baz")
60
- assert_equal @cache.exist?("foo"), true
61
-
62
- @cache.delete_tag("baz")
63
-
64
- assert_equal @cache.exist?("foo"), false
65
- end
66
-
67
- def test_read_and_write_with_tags_hash
68
- @cache.write("foo", "bar", :tags => {:baz => 1})
69
- assert_equal 'bar', @cache.read('foo')
70
- end
71
-
72
- def test_read_and_write_with_hash_of_tags
73
- @cache.write("foo", "bar", :tags => {:baz => 1})
74
- assert_equal 'bar', @cache.read('foo')
75
-
76
- @cache.delete_tag :baz => 2
77
- assert_equal 'bar', @cache.read('foo')
78
-
79
- @cache.delete_tag :baz => 1
80
-
81
- assert_nil @cache.read('foo')
82
- end
83
-
84
- def test_read_and_write_with_tags_array_of_objects
85
- tag1 = 1.day.ago
86
- tag2 = 2.days.ago
87
-
88
- @cache.write("foo", "bar", :tags => [tag1, tag2])
89
- assert_equal 'bar', @cache.read('foo')
90
-
91
- @cache.delete_tag tag2
92
-
93
- assert_nil @cache.read('foo')
94
- end
95
- end
96
-
97
- [FileStoreTest, MemoryStoreTest, MemCacheStoreTest, DalliStoreTest].each do |klass|
98
- klass.send :include, CacheTagsBehavior
99
- end
@@ -1,497 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__))
2
-
3
- require 'test_helper'
4
- require 'logger'
5
-
6
- # Tests the base functionality that should be identical across all cache stores.
7
- module CacheStoreBehavior
8
- def test_should_read_and_write_strings
9
- assert_equal true, @cache.write('foo', 'bar')
10
- assert_equal 'bar', @cache.read('foo')
11
- end
12
-
13
- def test_should_overwrite
14
- @cache.write('foo', 'bar')
15
- @cache.write('foo', 'baz')
16
- assert_equal 'baz', @cache.read('foo')
17
- end
18
-
19
- def test_fetch_without_cache_miss
20
- @cache.write('foo', 'bar')
21
- @cache.expects(:write).never
22
- assert_equal 'bar', @cache.fetch('foo') { 'baz' }
23
- end
24
-
25
- def test_fetch_with_cache_miss
26
- @cache.expects(:write).with('foo', 'baz', @cache.options)
27
- assert_equal 'baz', @cache.fetch('foo') { 'baz' }
28
- end
29
-
30
- def test_fetch_with_forced_cache_miss
31
- @cache.write('foo', 'bar')
32
- @cache.expects(:read).never
33
- @cache.expects(:write).with('foo', 'bar', @cache.options.merge(:force => true))
34
- @cache.fetch('foo', :force => true) { 'bar' }
35
- end
36
-
37
- def test_fetch_with_cached_nil
38
- @cache.write('foo', nil)
39
- @cache.expects(:write).never
40
- assert_nil @cache.fetch('foo') { 'baz' }
41
- end
42
-
43
- def test_should_read_and_write_hash
44
- assert_equal true, @cache.write('foo', {:a => "b"})
45
- assert_equal({:a => "b"}, @cache.read('foo'))
46
- end
47
-
48
- def test_should_read_and_write_integer
49
- assert_equal true, @cache.write('foo', 1)
50
- assert_equal 1, @cache.read('foo')
51
- end
52
-
53
- def test_should_read_and_write_nil
54
- assert_equal true, @cache.write('foo', nil)
55
- assert_equal nil, @cache.read('foo')
56
- end
57
-
58
- def test_read_multi
59
- @cache.write('foo', 'bar')
60
- @cache.write('fu', 'baz')
61
- @cache.write('fud', 'biz')
62
- assert_equal({"foo" => "bar", "fu" => "baz"}, @cache.read_multi('foo', 'fu'))
63
- end
64
-
65
- def test_read_multi_with_expires
66
- @cache.write('foo', 'bar', :expires_in => 0.001)
67
- @cache.write('fu', 'baz')
68
- @cache.write('fud', 'biz')
69
- sleep(0.002)
70
- assert_equal({"fu" => "baz"}, @cache.read_multi('foo', 'fu'))
71
- end
72
-
73
- def test_read_and_write_compressed_large_data
74
- @cache.write('foo', 'bar', :compress => true, :compress_threshold => 2)
75
- raw_value = @cache.send(:read_entry, 'foo', {}).raw_value
76
- assert_equal 'bar', @cache.read('foo')
77
- assert_equal 'bar', Marshal.load(Zlib::Inflate.inflate(raw_value))
78
- end
79
-
80
- def test_read_and_write_compressed_nil
81
- @cache.write('foo', nil, :compress => true)
82
- assert_nil @cache.read('foo')
83
- end
84
-
85
- def test_cache_key
86
- obj = Object.new
87
- def obj.cache_key
88
- :foo
89
- end
90
- @cache.write(obj, "bar")
91
- assert_equal "bar", @cache.read("foo")
92
- end
93
-
94
- def test_param_as_cache_key
95
- obj = Object.new
96
- def obj.to_param
97
- "foo"
98
- end
99
- @cache.write(obj, "bar")
100
- assert_equal "bar", @cache.read("foo")
101
- end
102
-
103
- def test_array_as_cache_key
104
- @cache.write([:fu, "foo"], "bar")
105
- assert_equal "bar", @cache.read("fu/foo")
106
- end
107
-
108
- def test_hash_as_cache_key
109
- @cache.write({:foo => 1, :fu => 2}, "bar")
110
- assert_equal "bar", @cache.read("foo=1/fu=2")
111
- end
112
-
113
- def test_keys_are_case_sensitive
114
- @cache.write("foo", "bar")
115
- assert_nil @cache.read("FOO")
116
- end
117
-
118
- def test_exist
119
- @cache.write('foo', 'bar')
120
- assert_equal true, @cache.exist?('foo')
121
- assert_equal false, @cache.exist?('bar')
122
- end
123
-
124
- def test_nil_exist
125
- @cache.write('foo', nil)
126
- assert_equal true, @cache.exist?('foo')
127
- end
128
-
129
- def test_delete
130
- @cache.write('foo', 'bar')
131
- assert @cache.exist?('foo')
132
- assert_equal true, @cache.delete('foo')
133
- assert !@cache.exist?('foo')
134
- end
135
-
136
- def test_original_store_objects_should_not_be_immutable
137
- bar = 'bar'
138
- @cache.write('foo', bar)
139
- assert_nothing_raised { bar.gsub!(/.*/, 'baz') }
140
- end
141
-
142
- def test_expires_in
143
- time = Time.local(2008, 4, 24)
144
- Time.stubs(:now).returns(time)
145
-
146
- @cache.write('foo', 'bar')
147
- assert_equal 'bar', @cache.read('foo')
148
-
149
- Time.stubs(:now).returns(time + 30)
150
- assert_equal 'bar', @cache.read('foo')
151
-
152
- Time.stubs(:now).returns(time + 61)
153
- assert_nil @cache.read('foo')
154
- end
155
-
156
- def test_race_condition_protection
157
- time = Time.now
158
- @cache.write('foo', 'bar', :expires_in => 60)
159
- Time.stubs(:now).returns(time + 61)
160
- result = @cache.fetch('foo', :race_condition_ttl => 10) do
161
- assert_equal 'bar', @cache.read('foo')
162
- "baz"
163
- end
164
- assert_equal "baz", result
165
- end
166
-
167
- def test_race_condition_protection_is_limited
168
- time = Time.now
169
- @cache.write('foo', 'bar', :expires_in => 60)
170
- Time.stubs(:now).returns(time + 71)
171
- result = @cache.fetch('foo', :race_condition_ttl => 10) do
172
- assert_equal nil, @cache.read('foo')
173
- "baz"
174
- end
175
- assert_equal "baz", result
176
- end
177
-
178
- def test_race_condition_protection_is_safe
179
- time = Time.now
180
- @cache.write('foo', 'bar', :expires_in => 60)
181
- Time.stubs(:now).returns(time + 61)
182
- begin
183
- @cache.fetch('foo', :race_condition_ttl => 10) do
184
- assert_equal 'bar', @cache.read('foo')
185
- raise ArgumentError.new
186
- end
187
- rescue ArgumentError
188
- end
189
- assert_equal "bar", @cache.read('foo')
190
- Time.stubs(:now).returns(time + 71)
191
- assert_nil @cache.read('foo')
192
- end
193
-
194
- def test_crazy_key_characters
195
- crazy_key = "#/:*(<+=> )&$%@?;'\"\'`~-"
196
- assert_equal true, @cache.write(crazy_key, "1", :raw => true)
197
- assert_equal "1", @cache.read(crazy_key)
198
- assert_equal "1", @cache.fetch(crazy_key)
199
- assert_equal true, @cache.delete(crazy_key)
200
- assert_equal "2", @cache.fetch(crazy_key, :raw => true) { "2" }
201
- assert_equal 3, @cache.increment(crazy_key)
202
- assert_equal 2, @cache.decrement(crazy_key)
203
- end
204
-
205
- def test_really_long_keys
206
- key = ""
207
- 1000.times{key << "x"}
208
- assert_equal true, @cache.write(key, "bar")
209
- assert_equal "bar", @cache.read(key)
210
- assert_equal "bar", @cache.fetch(key)
211
- assert_nil @cache.read("#{key}x")
212
- assert_equal({key => "bar"}, @cache.read_multi(key))
213
- assert_equal true, @cache.delete(key)
214
- end
215
- end
216
-
217
- module CacheDeleteMatchedBehavior
218
- def test_delete_matched
219
- @cache.write("foo", "bar")
220
- @cache.write("fu", "baz")
221
- @cache.write("foo/bar", "baz")
222
- @cache.write("fu/baz", "bar")
223
- @cache.delete_matched(/oo/)
224
- assert_equal false, @cache.exist?("foo")
225
- assert_equal true, @cache.exist?("fu")
226
- assert_equal false, @cache.exist?("foo/bar")
227
- assert_equal true, @cache.exist?("fu/baz")
228
- end
229
- end
230
-
231
- module CacheIncrementDecrementBehavior
232
- def test_increment
233
- @cache.write('foo', 1, :raw => true)
234
- assert_equal 1, @cache.read('foo').to_i
235
- assert_equal 2, @cache.increment('foo')
236
- assert_equal 2, @cache.read('foo').to_i
237
- assert_equal 3, @cache.increment('foo')
238
- assert_equal 3, @cache.read('foo').to_i
239
- end
240
-
241
- def test_decrement
242
- @cache.write('foo', 3, :raw => true)
243
- assert_equal 3, @cache.read('foo').to_i
244
- assert_equal 2, @cache.decrement('foo')
245
- assert_equal 2, @cache.read('foo').to_i
246
- assert_equal 1, @cache.decrement('foo')
247
- assert_equal 1, @cache.read('foo').to_i
248
- end
249
- end
250
-
251
- module LocalCacheBehavior
252
- def test_local_writes_are_persistent_on_the_remote_cache
253
- retval = @cache.with_local_cache do
254
- @cache.write('foo', 'bar')
255
- end
256
- assert_equal true, retval
257
- assert_equal 'bar', @cache.read('foo')
258
- end
259
-
260
- def test_clear_also_clears_local_cache
261
- @cache.with_local_cache do
262
- @cache.write('foo', 'bar')
263
- @cache.clear
264
- assert_nil @cache.read('foo')
265
- end
266
-
267
- assert_nil @cache.read('foo')
268
- end
269
-
270
- def test_local_cache_of_write
271
- @cache.with_local_cache do
272
- @cache.write('foo', 'bar')
273
- @peek.delete('foo')
274
- assert_equal 'bar', @cache.read('foo')
275
- end
276
- end
277
-
278
- def test_local_cache_of_read
279
- @cache.write('foo', 'bar')
280
- @cache.with_local_cache do
281
- assert_equal 'bar', @cache.read('foo')
282
- end
283
- end
284
-
285
- def test_local_cache_of_write_nil
286
- @cache.with_local_cache do
287
- assert @cache.write('foo', nil)
288
- assert_nil @cache.read('foo')
289
- @peek.write('foo', 'bar')
290
- assert_nil @cache.read('foo')
291
- end
292
- end
293
-
294
- def test_local_cache_of_delete
295
- @cache.with_local_cache do
296
- @cache.write('foo', 'bar')
297
- @cache.delete('foo')
298
- assert_nil @cache.read('foo')
299
- end
300
- end
301
-
302
- def test_local_cache_of_exist
303
- @cache.with_local_cache do
304
- @cache.write('foo', 'bar')
305
- @peek.delete('foo')
306
- assert @cache.exist?('foo')
307
- end
308
- end
309
-
310
- def test_local_cache_of_increment
311
- @cache.with_local_cache do
312
- @cache.write('foo', 1, :raw => true)
313
- @peek.write('foo', 2, :raw => true)
314
- @cache.increment('foo')
315
- assert_equal 3, @cache.read('foo')
316
- end
317
- end
318
-
319
- def test_local_cache_of_decrement
320
- @cache.with_local_cache do
321
- @cache.write('foo', 1, :raw => true)
322
- @peek.write('foo', 3, :raw => true)
323
- @cache.decrement('foo')
324
- assert_equal 2, @cache.read('foo')
325
- end
326
- end
327
-
328
- def test_middleware
329
- app = lambda { |env|
330
- result = @cache.write('foo', 'bar')
331
- assert_equal 'bar', @cache.read('foo') # make sure 'foo' was written
332
- assert result
333
- }
334
- app = @cache.middleware.new(app)
335
- app.call({})
336
- end
337
- end
338
-
339
- class FileStoreTest < ActiveSupport::TestCase
340
- def setup
341
- Dir.mkdir(cache_dir) unless File.exist?(cache_dir)
342
- @cache = ActiveSupport::Cache.lookup_store(:file_store, cache_dir, :expires_in => 60)
343
- @peek = ActiveSupport::Cache.lookup_store(:file_store, cache_dir, :expires_in => 60)
344
- end
345
-
346
- def teardown
347
- FileUtils.rm_r(cache_dir)
348
- end
349
-
350
- def cache_dir
351
- File.join(Dir.pwd, 'tmp_cache')
352
- end
353
-
354
- include CacheStoreBehavior
355
- include LocalCacheBehavior
356
- include CacheDeleteMatchedBehavior
357
- include CacheIncrementDecrementBehavior
358
-
359
- def test_key_transformation
360
- key = @cache.send(:key_file_path, "views/index?id=1")
361
- assert_equal "views/index?id=1", @cache.send(:file_path_key, key)
362
- end
363
- end
364
-
365
- class MemoryStoreTest < ActiveSupport::TestCase
366
- def setup
367
- @cache = ActiveSupport::Cache.lookup_store(:memory_store, :expires_in => 60, :size => 100)
368
- end
369
-
370
- include CacheStoreBehavior
371
- include CacheDeleteMatchedBehavior
372
- include CacheIncrementDecrementBehavior
373
-
374
-
375
- def test_pruning_is_capped_at_a_max_time
376
- def @cache.delete_entry (*args)
377
- sleep(0.01)
378
- super
379
- end
380
- @cache.write(1, "aaaaaaaaaa") && sleep(0.001)
381
- @cache.write(2, "bbbbbbbbbb") && sleep(0.001)
382
- @cache.write(3, "cccccccccc") && sleep(0.001)
383
- @cache.write(4, "dddddddddd") && sleep(0.001)
384
- @cache.write(5, "eeeeeeeeee") && sleep(0.001)
385
- @cache.prune(30, 0.001)
386
- assert_equal true, @cache.exist?(5)
387
- assert_equal true, @cache.exist?(4)
388
- assert_equal true, @cache.exist?(3)
389
- assert_equal true, @cache.exist?(2)
390
- assert_equal false, @cache.exist?(1)
391
- end
392
- end
393
-
394
- class SynchronizedStoreTest < ActiveSupport::TestCase
395
- def setup
396
- ActiveSupport::Deprecation.silence do
397
- @cache = ActiveSupport::Cache.lookup_store(:memory_store, :expires_in => 60)
398
- end
399
- end
400
-
401
- include CacheStoreBehavior
402
- include CacheDeleteMatchedBehavior
403
- include CacheIncrementDecrementBehavior
404
- end
405
-
406
- uses_memcached 'memcached backed store' do
407
- class MemCacheStoreTest < ActiveSupport::TestCase
408
- def setup
409
- @cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :expires_in => 60)
410
- @peek = ActiveSupport::Cache.lookup_store(:mem_cache_store)
411
- @data = @cache.instance_variable_get(:@data)
412
- @cache.clear
413
- @cache.silence!
414
- @cache.logger = Logger.new("/dev/null")
415
- end
416
-
417
- include CacheStoreBehavior
418
- include LocalCacheBehavior
419
- include CacheIncrementDecrementBehavior
420
-
421
- def test_raw_values
422
- cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
423
- cache.clear
424
- cache.write("foo", 2)
425
- assert_equal "2", cache.read("foo")
426
- end
427
-
428
- def test_local_cache_raw_values
429
- cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
430
- cache.clear
431
- cache.with_local_cache do
432
- cache.write("foo", 2)
433
- assert_equal "2", cache.read("foo")
434
- end
435
- end
436
- end
437
-
438
- class DalliStoreTest < ActiveSupport::TestCase
439
- def setup
440
- @cache = ActiveSupport::Cache.lookup_store(:dalli_store, :expires_in => 60)
441
- @peek = ActiveSupport::Cache.lookup_store(:dalli_store)
442
- @data = @cache.instance_variable_get(:@data)
443
- @cache.clear
444
- @cache.silence!
445
- end
446
-
447
- def test_should_read_and_write_strings
448
- assert_equal true, @cache.write('foo', 'bar')
449
- assert_equal 'bar', @cache.read('foo')
450
- end
451
-
452
- def test_should_read_and_write_hash
453
- assert_equal true, @cache.write('foo', {:a => "b"})
454
- assert_equal({:a => "b"}, @cache.read('foo'))
455
- end
456
-
457
- def test_should_read_and_write_integer
458
- assert_equal true, @cache.write('foo', 1)
459
- assert_equal 1, @cache.read('foo')
460
- end
461
-
462
- def test_read_multi
463
- @cache.write('foo', 'bar')
464
- @cache.write('fu', 'baz')
465
- @cache.write('fud', 'biz')
466
- assert_equal({"foo" => "bar", "fu" => "baz"}, @cache.read_multi('foo', 'fu'))
467
- end
468
- end
469
- end
470
-
471
- class CacheEntryTest < ActiveSupport::TestCase
472
- def test_create_raw_entry
473
- time = Time.now
474
- entry = ActiveSupport::Cache::Entry.create("raw", time, :compress => false, :expires_in => 300)
475
- assert_equal "raw", entry.raw_value
476
- assert_equal time.to_f, entry.created_at
477
- assert_nil entry.compressed?.presence
478
- assert_equal 300, entry.expires_in
479
- end
480
-
481
- def test_expired
482
- entry = ActiveSupport::Cache::Entry.new("value")
483
- assert_nil entry.expired?.presence
484
- entry = ActiveSupport::Cache::Entry.new("value", :expires_in => 60)
485
- assert_nil entry.expired?.presence
486
- time = Time.now + 61
487
- Time.stubs(:now).returns(time)
488
- assert_equal true, entry.expired?
489
- end
490
-
491
- def test_compress_values
492
- entry = ActiveSupport::Cache::Entry.new("value", :compress => true, :compress_threshold => 1)
493
- assert_equal "value", entry.value
494
- assert_equal true, entry.compressed?
495
- assert_equal "value", Marshal.load(Zlib::Inflate.inflate(entry.raw_value))
496
- end
497
- end