caching_test.rb 23.5 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

  def test_expand_cache_key_with_rails_cache_id
12 13 14 15 16 17 18 19 20 21 22
    begin
      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)
    ensure
      ENV['RAILS_CACHE_ID'] = nil
    end
23
  end
24 25

  def test_expand_cache_key_with_rails_app_version
26 27 28 29 30 31
    begin
      ENV['RAILS_APP_VERSION'] = 'rails3'
      assert_equal 'rails3/foo', ActiveSupport::Cache.expand_cache_key(:foo)
    ensure
      ENV['RAILS_APP_VERSION'] = nil
    end
32 33 34
  end

  def test_expand_cache_key_rails_cache_id_should_win_over_rails_app_version
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    begin
      ENV['RAILS_CACHE_ID'] = 'c99'
      ENV['RAILS_APP_VERSION'] = 'rails3'
      assert_equal 'c99/foo', ActiveSupport::Cache.expand_cache_key(:foo)
    ensure
      ENV['RAILS_CACHE_ID'] = nil
      ENV['RAILS_APP_VERSION'] = nil
    end
  end

  def test_respond_to_cache_key
    key = 'foo'
    def key.cache_key
      :foo_key
    end
    assert_equal 'foo_key', ActiveSupport::Cache.expand_cache_key(key)
51 52
  end

53 54
end

55
class CacheStoreSettingTest < ActiveSupport::TestCase
56 57 58 59 60
  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
61

62
  def test_mem_cache_fragment_cache_store
63
    MemCache.expects(:new).with(%w[localhost], {})
64 65
    store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost"
    assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
66 67 68 69 70 71 72
  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)
73
  end
74

75 76
  def test_mem_cache_fragment_cache_store_with_given_mem_cache_like_object
    MemCache.expects(:new).never
77 78 79
    memcache = Object.new
    def memcache.get() true end
    store = ActiveSupport::Cache.lookup_store :mem_cache_store, memcache
80 81 82
    assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
  end

83
  def test_mem_cache_fragment_cache_store_with_multiple_servers
84
    MemCache.expects(:new).with(%w[localhost 192.168.1.1], {})
85 86 87
    store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1'
    assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
  end
88

89
  def test_mem_cache_fragment_cache_store_with_options
B
Brian Durand 已提交
90 91
    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
92
    assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
B
Brian Durand 已提交
93
    assert_equal 'foo', store.options[:namespace]
94
  end
95 96 97 98 99 100 101

  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
102

B
Brian Durand 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
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/)
125 126
    assert_equal false, cache.exist?("foo")
    assert_equal true, cache.exist?("fu")
B
Brian Durand 已提交
127 128 129 130 131 132 133
  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)
134 135
    assert_equal false, cache.exist?("foo")
    assert_equal true, cache.exist?("fu")
B
Brian Durand 已提交
136 137 138 139 140 141
  end
end

# Tests the base functionality that should be identical across all cache stores.
module CacheStoreBehavior
  def test_should_read_and_write_strings
142
    assert_equal true, @cache.write('foo', 'bar')
B
Brian Durand 已提交
143 144 145 146 147 148 149
    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')
150
  end
151

152
  def test_fetch_without_cache_miss
B
Brian Durand 已提交
153
    @cache.write('foo', 'bar')
154 155 156
    @cache.expects(:write).never
    assert_equal 'bar', @cache.fetch('foo') { 'baz' }
  end
157

158
  def test_fetch_with_cache_miss
B
Brian Durand 已提交
159
    @cache.expects(:write).with('foo', 'baz', @cache.options)
160 161
    assert_equal 'baz', @cache.fetch('foo') { 'baz' }
  end
162

163
  def test_fetch_with_forced_cache_miss
B
Brian Durand 已提交
164
    @cache.write('foo', 'bar')
165
    @cache.expects(:read).never
B
Brian Durand 已提交
166
    @cache.expects(:write).with('foo', 'bar', @cache.options.merge(:force => true))
167
    @cache.fetch('foo', :force => true) { 'bar' }
168
  end
J
Joshua Peek 已提交
169

B
Brian Durand 已提交
170 171 172 173
  def test_fetch_with_cached_nil
    @cache.write('foo', nil)
    @cache.expects(:write).never
    assert_nil @cache.fetch('foo') { 'baz' }
J
Joshua Peek 已提交
174 175 176
  end

  def test_should_read_and_write_hash
177
    assert_equal true, @cache.write('foo', {:a => "b"})
J
Joshua Peek 已提交
178 179 180
    assert_equal({:a => "b"}, @cache.read('foo'))
  end

181
  def test_should_read_and_write_integer
182
    assert_equal true, @cache.write('foo', 1)
183 184 185
    assert_equal 1, @cache.read('foo')
  end

J
Joshua Peek 已提交
186
  def test_should_read_and_write_nil
187
    assert_equal true, @cache.write('foo', nil)
J
Joshua Peek 已提交
188
    assert_equal nil, @cache.read('foo')
189 190
  end

191
  def test_should_read_and_write_false
192 193
    assert_equal true, @cache.write('foo', false)
    assert_equal false, @cache.read('foo')
194 195
  end

B
Brian Durand 已提交
196
  def test_read_multi
197
    @cache.write('foo', 'bar')
B
Brian Durand 已提交
198 199 200
    @cache.write('fu', 'baz')
    @cache.write('fud', 'biz')
    assert_equal({"foo" => "bar", "fu" => "baz"}, @cache.read_multi('foo', 'fu'))
201
  end
202 203 204 205 206 207 208 209
  
  def test_read_multi_with_expires
    @cache.write('foo', 'bar', :expires_in => 0.001)
    @cache.write('fu', 'baz')
    @cache.write('fud', 'biz')
    sleep(0.002)
    assert_equal({"fu" => "baz"}, @cache.read_multi('foo', 'fu'))
  end
210

B
Brian Durand 已提交
211 212 213 214
  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')
215
    assert_equal 'bar', Marshal.load(raw_value)
216 217
  end

B
Brian Durand 已提交
218 219 220 221 222
  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))
223
  end
224

B
Brian Durand 已提交
225 226 227
  def test_read_and_write_compressed_nil
    @cache.write('foo', nil, :compress => true)
    assert_nil @cache.read('foo')
228 229
  end

B
Brian Durand 已提交
230 231 232 233 234 235 236
  def test_cache_key
    obj = Object.new
    def obj.cache_key
      :foo
    end
    @cache.write(obj, "bar")
    assert_equal "bar", @cache.read("foo")
237
  end
238

B
Brian Durand 已提交
239 240 241 242 243 244 245
  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")
246
  end
247

B
Brian Durand 已提交
248 249 250
  def test_array_as_cache_key
    @cache.write([:fu, "foo"], "bar")
    assert_equal "bar", @cache.read("fu/foo")
251 252
  end

B
Brian Durand 已提交
253 254 255
  def test_hash_as_cache_key
    @cache.write({:foo => 1, :fu => 2}, "bar")
    assert_equal "bar", @cache.read("foo=1/fu=2")
256 257
  end

B
Brian Durand 已提交
258 259 260 261
  def test_keys_are_case_sensitive
    @cache.write("foo", "bar")
    assert_nil @cache.read("FOO")
  end
262

B
Brian Durand 已提交
263
  def test_exist
264
    @cache.write('foo', 'bar')
265 266
    assert_equal true, @cache.exist?('foo')
    assert_equal false, @cache.exist?('bar')
267
  end
268

B
Brian Durand 已提交
269 270
  def test_nil_exist
    @cache.write('foo', nil)
271
    assert_equal true, @cache.exist?('foo')
272 273
  end

B
Brian Durand 已提交
274 275 276
  def test_delete
    @cache.write('foo', 'bar')
    assert @cache.exist?('foo')
277
    assert_equal true, @cache.delete('foo')
B
Brian Durand 已提交
278 279
    assert !@cache.exist?('foo')
  end
280

281
  def test_read_should_return_a_different_object_id_each_time_it_is_called
282
    @cache.write('foo', 'bar')
283 284 285 286
    assert_not_equal @cache.read('foo').object_id, @cache.read('foo').object_id
    value = @cache.read('foo')
    value << 'bingo'
    assert_not_equal value, @cache.read('foo')
287
  end
288 289 290 291 292 293

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

B
Brian Durand 已提交
295 296 297 298 299 300 301 302 303 304 305 306
  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')
307
  end
308

B
Brian Durand 已提交
309 310 311 312 313 314 315
  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"
316
    end
B
Brian Durand 已提交
317 318
    assert_equal "baz", result
  end
319

B
Brian Durand 已提交
320 321 322 323 324 325 326 327 328 329
  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
330

B
Brian Durand 已提交
331 332 333 334 335 336
  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
337
        assert_equal 'bar', @cache.read('foo')
B
Brian Durand 已提交
338
        raise ArgumentError.new
339
      end
S
Santiago Pastorino 已提交
340
    rescue ArgumentError
341
    end
B
Brian Durand 已提交
342 343 344 345 346 347 348
    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 = "#/:*(<+=> )&$%@?;'\"\'`~-"
349
    assert_equal true, @cache.write(crazy_key, "1", :raw => true)
B
Brian Durand 已提交
350 351
    assert_equal "1", @cache.read(crazy_key)
    assert_equal "1", @cache.fetch(crazy_key)
352
    assert_equal true, @cache.delete(crazy_key)
B
Brian Durand 已提交
353 354 355 356 357 358 359
    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 = ""
360
    900.times{key << "x"}
361
    assert_equal true, @cache.write(key, "bar")
B
Brian Durand 已提交
362 363 364 365
    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))
366
    assert_equal true, @cache.delete(key)
B
Brian Durand 已提交
367 368
  end
end
369

370 371 372 373 374 375 376 377
# https://rails.lighthouseapp.com/projects/8994/tickets/6225-memcachestore-cant-deal-with-umlauts-and-special-characters
# The error is caused by charcter encodings that can't be compared with ASCII-8BIT regular expressions and by special
# characters like the umlaut in UTF-8.
module EncodedKeyCacheBehavior
  if defined?(Encoding)
    Encoding.list.each do |encoding|
      define_method "test_#{encoding.name.underscore}_encoded_values" do
        key = "foo".force_encoding(encoding)
378
        assert_equal true, @cache.write(key, "1", :raw => true)
379 380
        assert_equal "1", @cache.read(key)
        assert_equal "1", @cache.fetch(key)
381
        assert_equal true, @cache.delete(key)
382 383 384 385 386 387 388 389
        assert_equal "2", @cache.fetch(key, :raw => true) { "2" }
        assert_equal 3, @cache.increment(key)
        assert_equal 2, @cache.decrement(key)
      end
    end

    def test_common_utf8_values
      key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8)
390
      assert_equal true, @cache.write(key, "1", :raw => true)
391 392
      assert_equal "1", @cache.read(key)
      assert_equal "1", @cache.fetch(key)
393
      assert_equal true, @cache.delete(key)
394 395 396 397 398 399 400
      assert_equal "2", @cache.fetch(key, :raw => true) { "2" }
      assert_equal 3, @cache.increment(key)
      assert_equal 2, @cache.decrement(key)
    end

    def test_retains_encoding
      key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8)
401
      assert_equal true, @cache.write(key, "1", :raw => true)
402 403 404 405 406
      assert_equal Encoding::UTF_8, key.encoding
    end
  end
end

B
Brian Durand 已提交
407 408 409 410
module CacheDeleteMatchedBehavior
  def test_delete_matched
    @cache.write("foo", "bar")
    @cache.write("fu", "baz")
411 412
    @cache.write("foo/bar", "baz")
    @cache.write("fu/baz", "bar")
B
Brian Durand 已提交
413
    @cache.delete_matched(/oo/)
414 415 416 417
    assert_equal false, @cache.exist?("foo")
    assert_equal true, @cache.exist?("fu")
    assert_equal false, @cache.exist?("foo/bar")
    assert_equal true, @cache.exist?("fu/baz")
B
Brian Durand 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
  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')
445
    end
446
    assert_equal true, retval
B
Brian Durand 已提交
447 448
    assert_equal 'bar', @cache.read('foo')
  end
449

B
Brian Durand 已提交
450 451 452 453 454
  def test_clear_also_clears_local_cache
    @cache.with_local_cache do
      @cache.write('foo', 'bar')
      @cache.clear
      assert_nil @cache.read('foo')
455 456
    end

B
Brian Durand 已提交
457 458
    assert_nil @cache.read('foo')
  end
459

B
Brian Durand 已提交
460 461 462 463
  def test_local_cache_of_write
    @cache.with_local_cache do
      @cache.write('foo', 'bar')
      @peek.delete('foo')
464 465
      assert_equal 'bar', @cache.read('foo')
    end
B
Brian Durand 已提交
466
  end
467

B
Brian Durand 已提交
468 469 470 471
  def test_local_cache_of_read
    @cache.write('foo', 'bar')
    @cache.with_local_cache do
      assert_equal 'bar', @cache.read('foo')
472
    end
B
Brian Durand 已提交
473
  end
474

B
Brian Durand 已提交
475 476
  def test_local_cache_of_write_nil
    @cache.with_local_cache do
477
      assert @cache.write('foo', nil)
B
Brian Durand 已提交
478 479 480
      assert_nil @cache.read('foo')
      @peek.write('foo', 'bar')
      assert_nil @cache.read('foo')
481
    end
B
Brian Durand 已提交
482
  end
483

B
Brian Durand 已提交
484 485 486 487 488
  def test_local_cache_of_delete
    @cache.with_local_cache do
      @cache.write('foo', 'bar')
      @cache.delete('foo')
      assert_nil @cache.read('foo')
489
    end
B
Brian Durand 已提交
490
  end
491

B
Brian Durand 已提交
492 493 494 495
  def test_local_cache_of_exist
    @cache.with_local_cache do
      @cache.write('foo', 'bar')
      @peek.delete('foo')
496
      assert @cache.exist?('foo')
497
    end
B
Brian Durand 已提交
498
  end
499

B
Brian Durand 已提交
500 501 502 503 504 505
  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')
506
    end
B
Brian Durand 已提交
507
  end
508

B
Brian Durand 已提交
509 510 511 512 513 514
  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')
515
    end
B
Brian Durand 已提交
516
  end
517

B
Brian Durand 已提交
518 519 520 521 522 523 524 525 526 527
  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
528

B
Brian Durand 已提交
529 530 531 532 533
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)
534
    @cache_with_pathname = ActiveSupport::Cache.lookup_store(:file_store, Pathname.new(cache_dir), :expires_in => 60)
B
Brian Durand 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
  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

550 551 552 553
  def test_key_transformation
    key = @cache.send(:key_file_path, "views/index?id=1")
    assert_equal "views/index?id=1", @cache.send(:file_path_key, key)
  end
554 555 556 557 558 559

  def test_key_transformation_with_pathname
    FileUtils.touch(File.join(cache_dir, "foo"))
    key = @cache_with_pathname.send(:key_file_path, "views/index?id=1")
    assert_equal "views/index?id=1", @cache_with_pathname.send(:file_path_key, key)
  end
560 561 562 563 564 565 566 567 568
  
  # Because file systems have a maximum filename size, filenames > max size should be split in to directories
  # If filename is 'AAAAB', where max size is 4, the returned path should be AAAA/B
  def test_key_transformation_max_filename_size
    key = "#{'A' * ActiveSupport::Cache::FileStore::FILENAME_MAX_SIZE}B"
    path = @cache.send(:key_file_path, key)    
    assert path.split('/').all? { |dir_name| dir_name.size <= ActiveSupport::Cache::FileStore::FILENAME_MAX_SIZE}
    assert_equal 'B', File.basename(path)
  end
569

570 571 572 573 574
  # If nothing has been stored in the cache, there is a chance the cache directory does not yet exist
  # Ensure delete_matched gracefully handles this case
  def test_delete_matched_when_cache_directory_does_not_exist
    assert_nothing_raised(Exception) do
      ActiveSupport::Cache::FileStore.new('/test/cache/directory').delete_matched(/does_not_exist/)
575 576
    end
  end
B
Brian Durand 已提交
577
end
578

B
Brian Durand 已提交
579 580
class MemoryStoreTest < ActiveSupport::TestCase
  def setup
581 582
    @record_size = Marshal.dump("aaaaaaaaaa").bytesize
    @cache = ActiveSupport::Cache.lookup_store(:memory_store, :expires_in => 60, :size => @record_size * 10)
B
Brian Durand 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596
  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)
597
    @cache.prune(@record_size * 3)
598 599 600 601 602
    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)
B
Brian Durand 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  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")
619 620 621 622 623 624 625 626 627 628 629
    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)
B
Brian Durand 已提交
630 631 632 633 634 635
  end

  def test_pruning_is_capped_at_a_max_time
    def @cache.delete_entry (*args)
      sleep(0.01)
      super
636
    end
B
Brian Durand 已提交
637 638 639 640 641 642
    @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)
643 644 645 646 647
    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)
B
Brian Durand 已提交
648 649
  end
end
650

B
Brian Durand 已提交
651 652 653 654 655 656 657 658 659
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")
660 661
    end

B
Brian Durand 已提交
662 663 664
    include CacheStoreBehavior
    include LocalCacheBehavior
    include CacheIncrementDecrementBehavior
665
    include EncodedKeyCacheBehavior
B
Brian Durand 已提交
666 667 668 669 670 671

    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")
672
    end
673 674 675 676 677 678 679 680
    
    def test_raw_values_with_marshal
      cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
      cache.clear
      cache.write("foo", Marshal.dump([]))
      assert_equal [], cache.read("foo")      
    end
    
B
Brian Durand 已提交
681 682 683 684 685 686 687
    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
688
    end
689 690 691 692 693 694 695 696 697

    def test_local_cache_raw_values_with_marshal
      cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true)
      cache.clear
      cache.with_local_cache do
        cache.write("foo", Marshal.dump([]))
        assert_equal [], cache.read("foo")
      end
    end
698 699
  end
end
700 701 702 703 704 705 706 707 708 709 710

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' }
711
    assert_present @buffer.string
712 713 714 715
  end

  def test_mute_logging
    @cache.mute { @cache.fetch('foo') { 'bar' } }
716
    assert_blank @buffer.string
717 718
  end
end
B
Brian Durand 已提交
719 720 721 722 723 724 725

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
726
    assert !entry.compressed?
B
Brian Durand 已提交
727 728 729 730 731
    assert_equal 300, entry.expires_in
  end

  def test_expired
    entry = ActiveSupport::Cache::Entry.new("value")
732
    assert !entry.expired?, 'entry not expired'
B
Brian Durand 已提交
733
    entry = ActiveSupport::Cache::Entry.new("value", :expires_in => 60)
734
    assert !entry.expired?, 'entry not expired'
B
Brian Durand 已提交
735 736
    time = Time.now + 61
    Time.stubs(:now).returns(time)
737
    assert entry.expired?, 'entry is expired'
B
Brian Durand 已提交
738 739 740 741 742
  end

  def test_compress_values
    entry = ActiveSupport::Cache::Entry.new("value", :compress => true, :compress_threshold => 1)
    assert_equal "value", entry.value
743
    assert entry.compressed?
B
Brian Durand 已提交
744 745 746 747 748 749
    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
750
    assert_equal "value", Marshal.load(entry.raw_value)
751
    assert !entry.compressed?
B
Brian Durand 已提交
752 753
  end
end