aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorDoug Barth <dougbarth@gmail.com>2008-10-09 10:37:28 -0500
committerMichael Koziarski <michael@koziarski.com>2008-10-17 18:09:26 +0200
commitc3d6205a4ba92e25f1092ac9c1f4e72ee3c796ed (patch)
tree3e00512e55886d0bb281a58fb087657f39d57b87 /activesupport
parent95c609357e78106e9673931d3f60d8ff3cc0a0cd (diff)
downloadrails-c3d6205a4ba92e25f1092ac9c1f4e72ee3c796ed.tar.gz
rails-c3d6205a4ba92e25f1092ac9c1f4e72ee3c796ed.tar.bz2
rails-c3d6205a4ba92e25f1092ac9c1f4e72ee3c796ed.zip
Fix cache counter semantics for MemoryCache, FileStoreCache, and (presumably) the DRbStore.
Signed-off-by: Michael Koziarski <michael@koziarski.com>
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/cache/file_store.rb1
-rw-r--r--activesupport/test/caching_test.rb96
2 files changed, 75 insertions, 22 deletions
diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb
index ef533633b2..ffe0baba99 100644
--- a/activesupport/lib/active_support/cache/file_store.rb
+++ b/activesupport/lib/active_support/cache/file_store.rb
@@ -16,6 +16,7 @@ module ActiveSupport
super
ensure_cache_path(File.dirname(real_file_path(name)))
File.atomic_write(real_file_path(name), cache_path) { |f| Marshal.dump(value, f) }
+ value
rescue => e
logger.error "Couldn't create cache directory: #{name} (#{e.message})" if logger
end
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index 9ea9389448..e2da2796eb 100644
--- a/activesupport/test/caching_test.rb
+++ b/activesupport/test/caching_test.rb
@@ -71,41 +71,21 @@ uses_mocha 'high-level cache store tests' do
end
end
-class FileStoreTest < Test::Unit::TestCase
- def setup
- @cache = ActiveSupport::Cache.lookup_store(:file_store, Dir.pwd)
- end
-
+# Tests the base functionality that should be identical across all cache stores.
+module CacheStoreBehavior
def test_should_read_and_write_strings
@cache.write('foo', 'bar')
assert_equal 'bar', @cache.read('foo')
- ensure
- File.delete("foo.cache")
end
def test_should_read_and_write_hash
@cache.write('foo', {:a => "b"})
assert_equal({:a => "b"}, @cache.read('foo'))
- ensure
- File.delete("foo.cache")
end
def test_should_read_and_write_nil
@cache.write('foo', nil)
assert_equal nil, @cache.read('foo')
- ensure
- File.delete("foo.cache")
- end
-end
-
-class MemoryStoreTest < Test::Unit::TestCase
- def setup
- @cache = ActiveSupport::Cache.lookup_store(:memory_store)
- end
-
- def test_should_read_and_write
- @cache.write('foo', 'bar')
- assert_equal 'bar', @cache.read('foo')
end
def test_fetch_without_cache_miss
@@ -121,9 +101,81 @@ class MemoryStoreTest < Test::Unit::TestCase
@cache.fetch('foo', :force => true) { 'bar' }
end
+ def test_increment
+ @cache.write('foo', 1, :raw => true)
+ assert_equal 1, @cache.read('foo', :raw => true).to_i
+ assert_equal 2, @cache.increment('foo')
+ assert_equal 2, @cache.read('foo', :raw => true).to_i
+ assert_equal 3, @cache.increment('foo')
+ assert_equal 3, @cache.read('foo', :raw => true).to_i
+ end
+
+ def test_decrement
+ @cache.write('foo', 3, :raw => true)
+ assert_equal 3, @cache.read('foo', :raw => true).to_i
+ assert_equal 2, @cache.decrement('foo')
+ assert_equal 2, @cache.read('foo', :raw => true).to_i
+ assert_equal 1, @cache.decrement('foo')
+ assert_equal 1, @cache.read('foo', :raw => true).to_i
+ end
+end
+
+class FileStoreTest < Test::Unit::TestCase
+ def setup
+ @cache = ActiveSupport::Cache.lookup_store(:file_store, Dir.pwd)
+ end
+
+ def teardown
+ File.delete("foo.cache")
+ end
+
+ include CacheStoreBehavior
+end
+
+class MemoryStoreTest < Test::Unit::TestCase
+ def setup
+ @cache = ActiveSupport::Cache.lookup_store(:memory_store)
+ end
+
+ include CacheStoreBehavior
+
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
end
+
+class MemCacheStoreTest < Test::Unit::TestCase
+ def setup
+ @cache = ActiveSupport::Cache.lookup_store(:mem_cache_store)
+ @cache.clear
+ end
+
+ include CacheStoreBehavior
+
+ def test_store_objects_should_be_immutable
+ @cache.write('foo', 'bar')
+ @cache.read('foo').gsub!(/.*/, 'baz')
+ assert_equal 'bar', @cache.read('foo')
+ end
+
+ # Disabling increment and decrement tests until issues can be addressed in the
+ # upstream codebase.
+ def test_increment; end
+ def test_decrement; end
+end
+
+class CompressedMemCacheStore < Test::Unit::TestCase
+ def setup
+ @cache = ActiveSupport::Cache.lookup_store(:compressed_mem_cache_store)
+ @cache.clear
+ end
+
+ include CacheStoreBehavior
+
+ # Disabling increment and decrement tests until issues can be addressed in the
+ # upstream codebase.
+ def test_increment; end
+ def test_decrement; end
+end