caching_test.rb 19.8 KB
Newer Older
1
require 'logger'
2
require 'abstract_unit'
J
Jeremy Kemper 已提交
3
require 'active_support/cache'
4

5
class CacheKeyTest < ActiveSupport::TestCase
6
  def test_expand_cache_key
B
Brian Durand 已提交
7
    assert_equal '1/2/true', ActiveSupport::Cache.expand_cache_key([1, '2', true])
8 9
    assert_equal 'name/1/2/true', ActiveSupport::Cache.expand_cache_key([1, '2', true], :name)
  end
10 11 12 13 14 15 16 17 18 19

  def test_expand_cache_key_with_rails_cache_id
    ENV['RAILS_CACHE_ID'] = 'c99'
    assert_equal 'c99/foo', ActiveSupport::Cache.expand_cache_key(:foo)
    assert_equal 'c99/foo', ActiveSupport::Cache.expand_cache_key([:foo])
    assert_equal 'c99/c99/foo/c99/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar])
    assert_equal 'nm/c99/foo', ActiveSupport::Cache.expand_cache_key(:foo, :nm)
    assert_equal 'nm/c99/foo', ActiveSupport::Cache.expand_cache_key([:foo], :nm)
    assert_equal 'nm/c99/c99/foo/c99/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar], :nm)
  end
20 21
end

22
class CacheStoreSettingTest < ActiveSupport::TestCase
23 24 25 26 27
  def test_file_fragment_cache_store
    store = ActiveSupport::Cache.lookup_store :file_store, "/path/to/cache/directory"
    assert_kind_of(ActiveSupport::Cache::FileStore, store)
    assert_equal "/path/to/cache/directory", store.cache_path
  end
28

29
  def test_mem_cache_fragment_cache_store
30
    MemCache.expects(:new).with(%w[localhost], {})
31 32
    store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost"
    assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
33 34 35 36 37 38 39
  end

  def test_mem_cache_fragment_cache_store_with_given_mem_cache
    mem_cache = MemCache.new
    MemCache.expects(:new).never
    store = ActiveSupport::Cache.lookup_store :mem_cache_store, mem_cache
    assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
40
  end
41

42 43
  def test_mem_cache_fragment_cache_store_with_given_mem_cache_like_object
    MemCache.expects(:new).never
44 45 46
    memcache = Object.new
    def memcache.get() true end
    store = ActiveSupport::Cache.lookup_store :mem_cache_store, memcache
47 48 49
    assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
  end

50
  def test_mem_cache_fragment_cache_store_with_multiple_servers
51
    MemCache.expects(:new).with(%w[localhost 192.168.1.1], {})
52 53 54
    store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1'
    assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
  end
55

56
  def test_mem_cache_fragment_cache_store_with_options
B
Brian Durand 已提交
57 58
    MemCache.expects(:new).with(%w[localhost 192.168.1.1], { :timeout => 10 })
    store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1', :namespace => 'foo', :timeout => 10
59
    assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
B
Brian Durand 已提交
60
    assert_equal 'foo', store.options[:namespace]
61
  end
62 63 64 65 66 67 68

  def test_object_assigned_fragment_cache_store
    store = ActiveSupport::Cache.lookup_store ActiveSupport::Cache::FileStore.new("/path/to/cache/directory")
    assert_kind_of(ActiveSupport::Cache::FileStore, store)
    assert_equal "/path/to/cache/directory", store.cache_path
  end
end
69

B
Brian Durand 已提交
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
class CacheStoreNamespaceTest < ActiveSupport::TestCase
  def test_static_namespace
    cache = ActiveSupport::Cache.lookup_store(:memory_store, :namespace => "tester")
    cache.write("foo", "bar")
    assert_equal "bar", cache.read("foo")
    assert_equal "bar", cache.instance_variable_get(:@data)["tester:foo"].value
  end

  def test_proc_namespace
    test_val = "tester"
    proc = lambda{test_val}
    cache = ActiveSupport::Cache.lookup_store(:memory_store, :namespace => proc)
    cache.write("foo", "bar")
    assert_equal "bar", cache.read("foo")
    assert_equal "bar", cache.instance_variable_get(:@data)["tester:foo"].value
  end

  def test_delete_matched_key_start
    cache = ActiveSupport::Cache.lookup_store(:memory_store, :namespace => "tester")
    cache.write("foo", "bar")
    cache.write("fu", "baz")
    cache.delete_matched(/^fo/)
    assert_equal false, cache.exist?("foo")
    assert_equal true, cache.exist?("fu")
  end

  def test_delete_matched_key
    cache = ActiveSupport::Cache.lookup_store(:memory_store, :namespace => "foo")
    cache.write("foo", "bar")
    cache.write("fu", "baz")
    cache.delete_matched(/OO/i)
    assert_equal false, cache.exist?("foo")
    assert_equal true, cache.exist?("fu")
  end
end

# Tests the base functionality that should be identical across all cache stores.
module CacheStoreBehavior
  def test_should_read_and_write_strings
    assert_equal true, @cache.write('foo', 'bar')
    assert_equal 'bar', @cache.read('foo')
  end

  def test_should_overwrite
    @cache.write('foo', 'bar')
    @cache.write('foo', 'baz')
    assert_equal 'baz', @cache.read('foo')
117
  end
118

119
  def test_fetch_without_cache_miss
B
Brian Durand 已提交
120
    @cache.write('foo', 'bar')
121 122 123
    @cache.expects(:write).never
    assert_equal 'bar', @cache.fetch('foo') { 'baz' }
  end
124

125
  def test_fetch_with_cache_miss
B
Brian Durand 已提交
126
    @cache.expects(:write).with('foo', 'baz', @cache.options)
127 128
    assert_equal 'baz', @cache.fetch('foo') { 'baz' }
  end
129

130
  def test_fetch_with_forced_cache_miss
B
Brian Durand 已提交
131
    @cache.write('foo', 'bar')
132
    @cache.expects(:read).never
B
Brian Durand 已提交
133
    @cache.expects(:write).with('foo', 'bar', @cache.options.merge(:force => true))
134
    @cache.fetch('foo', :force => true) { 'bar' }
135
  end
J
Joshua Peek 已提交
136

B
Brian Durand 已提交
137 138 139 140
  def test_fetch_with_cached_nil
    @cache.write('foo', nil)
    @cache.expects(:write).never
    assert_nil @cache.fetch('foo') { 'baz' }
J
Joshua Peek 已提交
141 142 143
  end

  def test_should_read_and_write_hash
B
Brian Durand 已提交
144
    assert_equal true, @cache.write('foo', {:a => "b"})
J
Joshua Peek 已提交
145 146 147
    assert_equal({:a => "b"}, @cache.read('foo'))
  end

148
  def test_should_read_and_write_integer
B
Brian Durand 已提交
149
    assert_equal true, @cache.write('foo', 1)
150 151 152
    assert_equal 1, @cache.read('foo')
  end

J
Joshua Peek 已提交
153
  def test_should_read_and_write_nil
B
Brian Durand 已提交
154
    assert_equal true, @cache.write('foo', nil)
J
Joshua Peek 已提交
155
    assert_equal nil, @cache.read('foo')
156 157
  end

B
Brian Durand 已提交
158
  def test_read_multi
159
    @cache.write('foo', 'bar')
B
Brian Durand 已提交
160 161 162
    @cache.write('fu', 'baz')
    @cache.write('fud', 'biz')
    assert_equal({"foo" => "bar", "fu" => "baz"}, @cache.read_multi('foo', 'fu'))
163 164
  end

B
Brian Durand 已提交
165 166 167 168 169
  def test_read_and_write_compressed_small_data
    @cache.write('foo', 'bar', :compress => true)
    raw_value = @cache.send(:read_entry, 'foo', {}).raw_value
    assert_equal 'bar', @cache.read('foo')
    assert_equal 'bar', raw_value
170 171
  end

B
Brian Durand 已提交
172 173 174 175 176
  def test_read_and_write_compressed_large_data
    @cache.write('foo', 'bar', :compress => true, :compress_threshold => 2)
    raw_value = @cache.send(:read_entry, 'foo', {}).raw_value
    assert_equal 'bar', @cache.read('foo')
    assert_equal 'bar', Marshal.load(Zlib::Inflate.inflate(raw_value))
177
  end
178

B
Brian Durand 已提交
179 180 181
  def test_read_and_write_compressed_nil
    @cache.write('foo', nil, :compress => true)
    assert_nil @cache.read('foo')
182 183
  end

B
Brian Durand 已提交
184 185 186 187 188 189 190
  def test_cache_key
    obj = Object.new
    def obj.cache_key
      :foo
    end
    @cache.write(obj, "bar")
    assert_equal "bar", @cache.read("foo")
191
  end
192

B
Brian Durand 已提交
193 194 195 196 197 198 199
  def test_param_as_cache_key
    obj = Object.new
    def obj.to_param
      "foo"
    end
    @cache.write(obj, "bar")
    assert_equal "bar", @cache.read("foo")
200
  end
201

B
Brian Durand 已提交
202 203 204
  def test_array_as_cache_key
    @cache.write([:fu, "foo"], "bar")
    assert_equal "bar", @cache.read("fu/foo")
205 206
  end

B
Brian Durand 已提交
207 208 209
  def test_hash_as_cache_key
    @cache.write({:foo => 1, :fu => 2}, "bar")
    assert_equal "bar", @cache.read("foo=1/fu=2")
210 211
  end

B
Brian Durand 已提交
212 213 214 215
  def test_keys_are_case_sensitive
    @cache.write("foo", "bar")
    assert_nil @cache.read("FOO")
  end
216

B
Brian Durand 已提交
217
  def test_exist
218
    @cache.write('foo', 'bar')
B
Brian Durand 已提交
219 220
    assert_equal true, @cache.exist?('foo')
    assert_equal false, @cache.exist?('bar')
221
  end
222

B
Brian Durand 已提交
223 224 225
  def test_nil_exist
    @cache.write('foo', nil)
    assert_equal true, @cache.exist?('foo')
226 227
  end

B
Brian Durand 已提交
228 229 230 231 232 233
  def test_delete
    @cache.write('foo', 'bar')
    assert @cache.exist?('foo')
    assert_equal true, @cache.delete('foo')
    assert !@cache.exist?('foo')
  end
234

235 236 237 238 239
  def test_store_objects_should_be_immutable
    @cache.write('foo', 'bar')
    assert_raise(ActiveSupport::FrozenObjectError) { @cache.read('foo').gsub!(/.*/, 'baz') }
    assert_equal 'bar', @cache.read('foo')
  end
240 241 242 243 244 245

  def test_original_store_objects_should_not_be_immutable
    bar = 'bar'
    @cache.write('foo', bar)
    assert_nothing_raised { bar.gsub!(/.*/, 'baz') }
  end
246

B
Brian Durand 已提交
247 248 249 250 251 252 253 254 255 256 257 258
  def test_expires_in
    time = Time.local(2008, 4, 24)
    Time.stubs(:now).returns(time)

    @cache.write('foo', 'bar')
    assert_equal 'bar', @cache.read('foo')

    Time.stubs(:now).returns(time + 30)
    assert_equal 'bar', @cache.read('foo')

    Time.stubs(:now).returns(time + 61)
    assert_nil @cache.read('foo')
259
  end
260

B
Brian Durand 已提交
261 262 263 264 265 266 267
  def test_race_condition_protection
    time = Time.now
    @cache.write('foo', 'bar', :expires_in => 60)
    Time.stubs(:now).returns(time + 61)
    result = @cache.fetch('foo', :race_condition_ttl => 10) do
      assert_equal 'bar', @cache.read('foo')
      "baz"
268
    end
B
Brian Durand 已提交
269 270
    assert_equal "baz", result
  end
271

B
Brian Durand 已提交
272 273 274 275 276 277 278 279 280 281
  def test_race_condition_protection_is_limited
    time = Time.now
    @cache.write('foo', 'bar', :expires_in => 60)
    Time.stubs(:now).returns(time + 71)
    result = @cache.fetch('foo', :race_condition_ttl => 10) do
      assert_equal nil, @cache.read('foo')
      "baz"
    end
    assert_equal "baz", result
  end
282

B
Brian Durand 已提交
283 284 285 286 287 288
  def test_race_condition_protection_is_safe
    time = Time.now
    @cache.write('foo', 'bar', :expires_in => 60)
    Time.stubs(:now).returns(time + 61)
    begin
      @cache.fetch('foo', :race_condition_ttl => 10) do
289
        assert_equal 'bar', @cache.read('foo')
B
Brian Durand 已提交
290
        raise ArgumentError.new
291
      end
S
Santiago Pastorino 已提交
292
    rescue ArgumentError
293
    end
B
Brian Durand 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    assert_equal "bar", @cache.read('foo')
    Time.stubs(:now).returns(time + 71)
    assert_nil @cache.read('foo')
  end

  def test_crazy_key_characters
    crazy_key = "#/:*(<+=> )&$%@?;'\"\'`~-"
    assert_equal true, @cache.write(crazy_key, "1", :raw => true)
    assert_equal "1", @cache.read(crazy_key)
    assert_equal "1", @cache.fetch(crazy_key)
    assert_equal true, @cache.delete(crazy_key)
    assert_equal "2", @cache.fetch(crazy_key, :raw => true) { "2" }
    assert_equal 3, @cache.increment(crazy_key)
    assert_equal 2, @cache.decrement(crazy_key)
  end

  def test_really_long_keys
    key = ""
    1000.times{key << "x"}
    assert_equal true, @cache.write(key, "bar")
    assert_equal "bar", @cache.read(key)
    assert_equal "bar", @cache.fetch(key)
    assert_nil @cache.read("#{key}x")
    assert_equal({key => "bar"}, @cache.read_multi(key))
    assert_equal true, @cache.delete(key)
  end
end
321

B
Brian Durand 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
module CacheDeleteMatchedBehavior
  def test_delete_matched
    @cache.write("foo", "bar")
    @cache.write("fu", "baz")
    @cache.delete_matched(/oo/)
    assert_equal false, @cache.exist?("foo")
    assert_equal true, @cache.exist?("fu")
  end
end

module CacheIncrementDecrementBehavior
  def test_increment
    @cache.write('foo', 1, :raw => true)
    assert_equal 1, @cache.read('foo').to_i
    assert_equal 2, @cache.increment('foo')
    assert_equal 2, @cache.read('foo').to_i
    assert_equal 3, @cache.increment('foo')
    assert_equal 3, @cache.read('foo').to_i
  end

  def test_decrement
    @cache.write('foo', 3, :raw => true)
    assert_equal 3, @cache.read('foo').to_i
    assert_equal 2, @cache.decrement('foo')
    assert_equal 2, @cache.read('foo').to_i
    assert_equal 1, @cache.decrement('foo')
    assert_equal 1, @cache.read('foo').to_i
  end
end

module LocalCacheBehavior
  def test_local_writes_are_persistent_on_the_remote_cache
    retval = @cache.with_local_cache do
      @cache.write('foo', 'bar')
356
    end
B
Brian Durand 已提交
357 358 359
    assert_equal true, retval
    assert_equal 'bar', @cache.read('foo')
  end
360

B
Brian Durand 已提交
361 362 363 364 365
  def test_clear_also_clears_local_cache
    @cache.with_local_cache do
      @cache.write('foo', 'bar')
      @cache.clear
      assert_nil @cache.read('foo')
366 367
    end

B
Brian Durand 已提交
368 369
    assert_nil @cache.read('foo')
  end
370

B
Brian Durand 已提交
371 372 373 374
  def test_local_cache_of_write
    @cache.with_local_cache do
      @cache.write('foo', 'bar')
      @peek.delete('foo')
375 376
      assert_equal 'bar', @cache.read('foo')
    end
B
Brian Durand 已提交
377
  end
378

B
Brian Durand 已提交
379 380 381 382
  def test_local_cache_of_read
    @cache.write('foo', 'bar')
    @cache.with_local_cache do
      assert_equal 'bar', @cache.read('foo')
383
    end
B
Brian Durand 已提交
384
  end
385

B
Brian Durand 已提交
386 387
  def test_local_cache_of_write_nil
    @cache.with_local_cache do
388
      assert @cache.write('foo', nil)
B
Brian Durand 已提交
389 390 391
      assert_nil @cache.read('foo')
      @peek.write('foo', 'bar')
      assert_nil @cache.read('foo')
392
    end
B
Brian Durand 已提交
393
  end
394

B
Brian Durand 已提交
395 396 397 398 399
  def test_local_cache_of_delete
    @cache.with_local_cache do
      @cache.write('foo', 'bar')
      @cache.delete('foo')
      assert_nil @cache.read('foo')
400
    end
B
Brian Durand 已提交
401
  end
402

B
Brian Durand 已提交
403 404 405 406
  def test_local_cache_of_exist
    @cache.with_local_cache do
      @cache.write('foo', 'bar')
      @peek.delete('foo')
407
      assert @cache.exist?('foo')
408
    end
B
Brian Durand 已提交
409
  end
410

B
Brian Durand 已提交
411 412 413 414 415 416
  def test_local_cache_of_increment
    @cache.with_local_cache do
      @cache.write('foo', 1, :raw => true)
      @peek.write('foo', 2, :raw => true)
      @cache.increment('foo')
      assert_equal 3, @cache.read('foo')
417
    end
B
Brian Durand 已提交
418
  end
419

B
Brian Durand 已提交
420 421 422 423 424 425
  def test_local_cache_of_decrement
    @cache.with_local_cache do
      @cache.write('foo', 1, :raw => true)
      @peek.write('foo', 3, :raw => true)
      @cache.decrement('foo')
      assert_equal 2, @cache.read('foo')
426
    end
B
Brian Durand 已提交
427
  end
428

B
Brian Durand 已提交
429 430 431 432 433 434 435 436 437 438
  def test_middleware
    app = lambda { |env|
      result = @cache.write('foo', 'bar')
      assert_equal 'bar', @cache.read('foo') # make sure 'foo' was written
      assert result
    }
    app = @cache.middleware.new(app)
    app.call({})
  end
end
439

B
Brian Durand 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
class FileStoreTest < ActiveSupport::TestCase
  def setup
    Dir.mkdir(cache_dir) unless File.exist?(cache_dir)
    @cache = ActiveSupport::Cache.lookup_store(:file_store, cache_dir, :expires_in => 60)
    @peek = ActiveSupport::Cache.lookup_store(:file_store, cache_dir, :expires_in => 60)
  end

  def teardown
    FileUtils.rm_r(cache_dir)
  end

  def cache_dir
    File.join(Dir.pwd, 'tmp_cache')
  end

  include CacheStoreBehavior
  include LocalCacheBehavior
  include CacheDeleteMatchedBehavior
  include CacheIncrementDecrementBehavior

  def test_deprecated_expires_in_on_read
    ActiveSupport::Deprecation.silence do
      old_cache = ActiveSupport::Cache.lookup_store(:file_store, cache_dir)

      time = Time.local(2008, 4, 24)
      Time.stubs(:now).returns(time)

      old_cache.write("foo", "bar")
      assert_equal 'bar', old_cache.read('foo', :expires_in => 60)

      Time.stubs(:now).returns(time + 30)
      assert_equal 'bar', old_cache.read('foo', :expires_in => 60)

      Time.stubs(:now).returns(time + 61)
      assert_equal 'bar', old_cache.read('foo')
      assert_nil old_cache.read('foo', :expires_in => 60)
      assert_nil old_cache.read('foo')
477
    end
B
Brian Durand 已提交
478 479
  end
end
480

B
Brian Durand 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
class MemoryStoreTest < ActiveSupport::TestCase
  def setup
    @cache = ActiveSupport::Cache.lookup_store(:memory_store, :expires_in => 60, :size => 100)
  end

  include CacheStoreBehavior
  include CacheDeleteMatchedBehavior
  include CacheIncrementDecrementBehavior

  def test_prune_size
    @cache.write(1, "aaaaaaaaaa") && sleep(0.001)
    @cache.write(2, "bbbbbbbbbb") && sleep(0.001)
    @cache.write(3, "cccccccccc") && sleep(0.001)
    @cache.write(4, "dddddddddd") && sleep(0.001)
    @cache.write(5, "eeeeeeeeee") && sleep(0.001)
    @cache.read(2) && sleep(0.001)
    @cache.read(4)
    @cache.prune(30)
    assert_equal true, @cache.exist?(5)
    assert_equal true, @cache.exist?(4)
    assert_equal false, @cache.exist?(3)
    assert_equal true, @cache.exist?(2)
    assert_equal false, @cache.exist?(1)
  end

  def test_prune_size_on_write
    @cache.write(1, "aaaaaaaaaa") && sleep(0.001)
    @cache.write(2, "bbbbbbbbbb") && sleep(0.001)
    @cache.write(3, "cccccccccc") && sleep(0.001)
    @cache.write(4, "dddddddddd") && sleep(0.001)
    @cache.write(5, "eeeeeeeeee") && sleep(0.001)
    @cache.write(6, "ffffffffff") && sleep(0.001)
    @cache.write(7, "gggggggggg") && sleep(0.001)
    @cache.write(8, "hhhhhhhhhh") && sleep(0.001)
    @cache.write(9, "iiiiiiiiii") && sleep(0.001)
    @cache.write(10, "kkkkkkkkkk") && sleep(0.001)
    @cache.read(2) && sleep(0.001)
    @cache.read(4) && sleep(0.001)
    @cache.write(11, "llllllllll")
    assert_equal true, @cache.exist?(11)
    assert_equal true, @cache.exist?(10)
    assert_equal true, @cache.exist?(9)
    assert_equal true, @cache.exist?(8)
    assert_equal true, @cache.exist?(7)
    assert_equal false, @cache.exist?(6)
    assert_equal false, @cache.exist?(5)
    assert_equal true, @cache.exist?(4)
    assert_equal false, @cache.exist?(3)
    assert_equal true, @cache.exist?(2)
    assert_equal false, @cache.exist?(1)
  end

  def test_pruning_is_capped_at_a_max_time
    def @cache.delete_entry (*args)
      sleep(0.01)
      super
537
    end
B
Brian Durand 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550
    @cache.write(1, "aaaaaaaaaa") && sleep(0.001)
    @cache.write(2, "bbbbbbbbbb") && sleep(0.001)
    @cache.write(3, "cccccccccc") && sleep(0.001)
    @cache.write(4, "dddddddddd") && sleep(0.001)
    @cache.write(5, "eeeeeeeeee") && sleep(0.001)
    @cache.prune(30, 0.001)
    assert_equal true, @cache.exist?(5)
    assert_equal true, @cache.exist?(4)
    assert_equal true, @cache.exist?(3)
    assert_equal true, @cache.exist?(2)
    assert_equal false, @cache.exist?(1)
  end
end
551

B
Brian Durand 已提交
552 553 554 555
class SynchronizedStoreTest < ActiveSupport::TestCase
  def setup
    ActiveSupport::Deprecation.silence do
      @cache = ActiveSupport::Cache.lookup_store(:memory_store, :expires_in => 60)
556
    end
B
Brian Durand 已提交
557
  end
558

B
Brian Durand 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572
  include CacheStoreBehavior
  include CacheDeleteMatchedBehavior
  include CacheIncrementDecrementBehavior
end

uses_memcached 'memcached backed store' do
  class MemCacheStoreTest < ActiveSupport::TestCase
    def setup
      @cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :expires_in => 60)
      @peek = ActiveSupport::Cache.lookup_store(:mem_cache_store)
      @data = @cache.instance_variable_get(:@data)
      @cache.clear
      @cache.silence!
      @cache.logger = Logger.new("/dev/null")
573 574
    end

B
Brian Durand 已提交
575 576 577 578 579 580 581 582 583
    include CacheStoreBehavior
    include LocalCacheBehavior
    include CacheIncrementDecrementBehavior

    def test_raw_values
      cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
      cache.clear
      cache.write("foo", 2)
      assert_equal "2", cache.read("foo")
584
    end
585

B
Brian Durand 已提交
586 587 588 589 590 591 592
    def test_local_cache_raw_values
      cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
      cache.clear
      cache.with_local_cache do
        cache.write("foo", 2)
        assert_equal "2", cache.read("foo")
      end
593
    end
594 595
  end

596
  class CompressedMemCacheStore < ActiveSupport::TestCase
597
    def setup
B
Brian Durand 已提交
598 599 600 601
      ActiveSupport::Deprecation.silence do
        @cache = ActiveSupport::Cache.lookup_store(:compressed_mem_cache_store, :expires_in => 60)
        @cache.clear
      end
602
    end
603

604
    include CacheStoreBehavior
B
Brian Durand 已提交
605
    include CacheIncrementDecrementBehavior
606
  end
607
end
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

class CacheStoreLoggerTest < ActiveSupport::TestCase
  def setup
    @cache = ActiveSupport::Cache.lookup_store(:memory_store)

    @buffer = StringIO.new
    @cache.logger = Logger.new(@buffer)
  end

  def test_logging
    @cache.fetch('foo') { 'bar' }
    assert @buffer.string.present?
  end

  def test_mute_logging
    @cache.mute { @cache.fetch('foo') { 'bar' } }
    assert @buffer.string.blank?
  end
end
B
Brian Durand 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

class CacheEntryTest < ActiveSupport::TestCase
  def test_create_raw_entry
    time = Time.now
    entry = ActiveSupport::Cache::Entry.create("raw", time, :compress => false, :expires_in => 300)
    assert_equal "raw", entry.raw_value
    assert_equal time.to_f, entry.created_at
    assert_equal false, entry.compressed?
    assert_equal 300, entry.expires_in
  end

  def test_expired
    entry = ActiveSupport::Cache::Entry.new("value")
    assert_equal false, entry.expired?
    entry = ActiveSupport::Cache::Entry.new("value", :expires_in => 60)
    assert_equal false, entry.expired?
    time = Time.now + 61
    Time.stubs(:now).returns(time)
    assert_equal true, entry.expired?
  end

  def test_compress_values
    entry = ActiveSupport::Cache::Entry.new("value", :compress => true, :compress_threshold => 1)
    assert_equal "value", entry.value
    assert_equal true, entry.compressed?
    assert_equal "value", Marshal.load(Zlib::Inflate.inflate(entry.raw_value))
  end

  def test_non_compress_values
    entry = ActiveSupport::Cache::Entry.new("value")
    assert_equal "value", entry.value
    assert_equal "value", entry.raw_value
    assert_equal false, entry.compressed?
  end
end