diff options
Diffstat (limited to 'activesupport/test/caching_test.rb')
-rw-r--r-- | activesupport/test/caching_test.rb | 171 |
1 files changed, 109 insertions, 62 deletions
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 402c6695aa..5d7464c623 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -13,10 +13,10 @@ class CacheKeyTest < ActiveSupport::TestCase 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 'c99/foo/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) + assert_equal 'nm/c99/foo/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar], :nm) ensure ENV['RAILS_CACHE_ID'] = nil end @@ -42,7 +42,7 @@ class CacheKeyTest < ActiveSupport::TestCase end end - def test_respond_to_cache_key + def test_expand_cache_key_respond_to_cache_key key = 'foo' def key.cache_key :foo_key @@ -50,6 +50,25 @@ class CacheKeyTest < ActiveSupport::TestCase assert_equal 'foo_key', ActiveSupport::Cache.expand_cache_key(key) end + def test_expand_cache_key_array_with_something_that_responds_to_cache_key + key = 'foo' + def key.cache_key + :foo_key + end + assert_equal 'foo_key', ActiveSupport::Cache.expand_cache_key([key]) + end + + def test_expand_cache_key_of_nil + assert_equal '', ActiveSupport::Cache.expand_cache_key(nil) + end + + def test_expand_cache_key_of_false + assert_equal 'false', ActiveSupport::Cache.expand_cache_key(false) + end + + def test_expand_cache_key_of_true + assert_equal 'true', ActiveSupport::Cache.expand_cache_key(true) + end end class CacheStoreSettingTest < ActiveSupport::TestCase @@ -122,8 +141,8 @@ class CacheStoreNamespaceTest < ActiveSupport::TestCase cache.write("foo", "bar") cache.write("fu", "baz") cache.delete_matched(/^fo/) - assert_equal false, cache.exist?("foo") - assert_equal true, cache.exist?("fu") + assert !cache.exist?("foo") + assert cache.exist?("fu") end def test_delete_matched_key @@ -131,15 +150,15 @@ class CacheStoreNamespaceTest < ActiveSupport::TestCase 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") + assert !cache.exist?("foo") + assert 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 @cache.write('foo', 'bar') assert_equal 'bar', @cache.read('foo') end @@ -174,22 +193,22 @@ module CacheStoreBehavior end def test_should_read_and_write_hash - assert_equal true, @cache.write('foo', {:a => "b"}) + assert @cache.write('foo', {:a => "b"}) assert_equal({:a => "b"}, @cache.read('foo')) end def test_should_read_and_write_integer - assert_equal true, @cache.write('foo', 1) + assert @cache.write('foo', 1) assert_equal 1, @cache.read('foo') end def test_should_read_and_write_nil - assert_equal true, @cache.write('foo', nil) + assert @cache.write('foo', nil) assert_equal nil, @cache.read('foo') end def test_should_read_and_write_false - assert_equal true, @cache.write('foo', false) + assert @cache.write('foo', false) assert_equal false, @cache.read('foo') end @@ -199,12 +218,20 @@ module CacheStoreBehavior @cache.write('fud', 'biz') assert_equal({"foo" => "bar", "fu" => "baz"}, @cache.read_multi('foo', 'fu')) end + + 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 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 + assert_equal 'bar', Marshal.load(raw_value) end def test_read_and_write_compressed_large_data @@ -254,26 +281,28 @@ module CacheStoreBehavior def test_exist @cache.write('foo', 'bar') - assert_equal true, @cache.exist?('foo') - assert_equal false, @cache.exist?('bar') + assert @cache.exist?('foo') + assert !@cache.exist?('bar') end def test_nil_exist @cache.write('foo', nil) - assert_equal true, @cache.exist?('foo') + assert @cache.exist?('foo') end def test_delete @cache.write('foo', 'bar') assert @cache.exist?('foo') - assert_equal true, @cache.delete('foo') + assert @cache.delete('foo') assert !@cache.exist?('foo') end - def test_store_objects_should_be_immutable + def test_read_should_return_a_different_object_id_each_time_it_is_called @cache.write('foo', 'bar') - assert_raise(ActiveSupport::FrozenObjectError) { @cache.read('foo').gsub!(/.*/, 'baz') } - assert_equal 'bar', @cache.read('foo') + 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') end def test_original_store_objects_should_not_be_immutable @@ -336,10 +365,10 @@ module CacheStoreBehavior def test_crazy_key_characters crazy_key = "#/:*(<+=> )&$%@?;'\"\'`~-" - assert_equal true, @cache.write(crazy_key, "1", :raw => true) + assert @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 @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) @@ -347,13 +376,13 @@ module CacheStoreBehavior def test_really_long_keys key = "" - 1000.times{key << "x"} - assert_equal true, @cache.write(key, "bar") + 900.times{key << "x"} + assert @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) + assert @cache.delete(key) end end @@ -365,10 +394,10 @@ module EncodedKeyCacheBehavior Encoding.list.each do |encoding| define_method "test_#{encoding.name.underscore}_encoded_values" do key = "foo".force_encoding(encoding) - assert_equal true, @cache.write(key, "1", :raw => true) + assert @cache.write(key, "1", :raw => true) assert_equal "1", @cache.read(key) assert_equal "1", @cache.fetch(key) - assert_equal true, @cache.delete(key) + assert @cache.delete(key) assert_equal "2", @cache.fetch(key, :raw => true) { "2" } assert_equal 3, @cache.increment(key) assert_equal 2, @cache.decrement(key) @@ -377,10 +406,10 @@ module EncodedKeyCacheBehavior def test_common_utf8_values key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8) - assert_equal true, @cache.write(key, "1", :raw => true) + assert @cache.write(key, "1", :raw => true) assert_equal "1", @cache.read(key) assert_equal "1", @cache.fetch(key) - assert_equal true, @cache.delete(key) + assert @cache.delete(key) assert_equal "2", @cache.fetch(key, :raw => true) { "2" } assert_equal 3, @cache.increment(key) assert_equal 2, @cache.decrement(key) @@ -388,7 +417,7 @@ module EncodedKeyCacheBehavior def test_retains_encoding key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8) - assert_equal true, @cache.write(key, "1", :raw => true) + assert @cache.write(key, "1", :raw => true) assert_equal Encoding::UTF_8, key.encoding end end @@ -401,10 +430,10 @@ module CacheDeleteMatchedBehavior @cache.write("foo/bar", "baz") @cache.write("fu/baz", "bar") @cache.delete_matched(/oo/) - 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") + assert !@cache.exist?("foo") + assert @cache.exist?("fu") + assert !@cache.exist?("foo/bar") + assert @cache.exist?("fu/baz") end end @@ -433,7 +462,7 @@ module LocalCacheBehavior retval = @cache.with_local_cache do @cache.write('foo', 'bar') end - assert_equal true, retval + assert retval assert_equal 'bar', @cache.read('foo') end @@ -547,11 +576,29 @@ class FileStoreTest < ActiveSupport::TestCase 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 + + # 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 + + # 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/) + end + end end class MemoryStoreTest < ActiveSupport::TestCase def setup - @cache = ActiveSupport::Cache.lookup_store(:memory_store, :expires_in => 60, :size => 100) + @record_size = Marshal.dump("aaaaaaaaaa").bytesize + @cache = ActiveSupport::Cache.lookup_store(:memory_store, :expires_in => 60, :size => @record_size * 10) end include CacheStoreBehavior @@ -566,12 +613,12 @@ class MemoryStoreTest < ActiveSupport::TestCase @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) + @cache.prune(@record_size * 3) + assert @cache.exist?(5) + assert @cache.exist?(4) + assert !@cache.exist?(3) + assert @cache.exist?(2) + assert !@cache.exist?(1) end def test_prune_size_on_write @@ -588,17 +635,17 @@ class MemoryStoreTest < ActiveSupport::TestCase @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) + assert @cache.exist?(11) + assert @cache.exist?(10) + assert @cache.exist?(9) + assert @cache.exist?(8) + assert @cache.exist?(7) + assert !@cache.exist?(6) + assert !@cache.exist?(5) + assert @cache.exist?(4) + assert !@cache.exist?(3) + assert @cache.exist?(2) + assert !@cache.exist?(1) end def test_pruning_is_capped_at_a_max_time @@ -612,11 +659,11 @@ class MemoryStoreTest < ActiveSupport::TestCase @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) + assert @cache.exist?(5) + assert @cache.exist?(4) + assert @cache.exist?(3) + assert @cache.exist?(2) + assert !@cache.exist?(1) end end @@ -695,7 +742,7 @@ class CacheEntryTest < ActiveSupport::TestCase 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 !entry.compressed? assert_equal 300, entry.expires_in end @@ -712,14 +759,14 @@ class CacheEntryTest < ActiveSupport::TestCase 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 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? + assert_equal "value", Marshal.load(entry.raw_value) + assert !entry.compressed? end end |