Permalink
Browse files

do a better job testing caching with arguments

  • Loading branch information...
1 parent 21faf0e commit 9999ca83587430ce5101e0493a034c71a9ada7dc @seamusabshere committed Feb 21, 2011
Showing with 102 additions and 7 deletions.
  1. +8 −0 lib/cache_method.rb
  2. +2 −2 lib/cache_method/cached_result.rb
  3. +15 −4 lib/cache_method/epoch.rb
  4. +1 −1 lib/cache_method/version.rb
  5. +33 −0 test/helper.rb
  6. +43 −0 test/test_cache_method.rb
View
@@ -21,6 +21,14 @@ def self.method_signature(obj, method_id) #:nodoc:
[ klass_name(obj), method_id ].join method_delimiter(obj)
end
+ # What gets called to determine the hashcode of an object.
+ #
+ # * If the object is a Class, then it just does Class.to_s (otherwise Class hash codes change too often)
+ # * Otherwise, call #hash
+ def self.hashcode(obj)
+ obj.is_a?(::Class) ? obj.to_s : obj.hash
+ end
+
# All Objects, including instances and Classes, get the <tt>#clear_method_cache</tt> method.
module InstanceMethods
# Clear the cache for a particular method.
@@ -34,15 +34,15 @@ def ttl
end
def cache_key
- [ method_signature, current_epoch, obj_hash, args_digest ].join ','
+ [ 'CacheMethod', 'CachedResult', method_signature, current_epoch, obj_hash, args_digest ].join ','
end
def method_signature
@method_signature ||= ::CacheMethod.method_signature(obj, method_id)
end
def obj_hash
- @obj_hash ||= obj.hash
+ @obj_hash ||= ::CacheMethod.hashcode(obj)
end
def args_digest
View
@@ -1,3 +1,4 @@
+require 'digest/md5'
module CacheMethod
class Epoch #:nodoc: all
class << self
@@ -10,6 +11,10 @@ def mark_passing(options = {})
epoch = new options
epoch.mark_passing
end
+
+ def random_name
+ ::Digest::MD5.hexdigest rand.to_s
+ end
end
def initialize(options = {})
@@ -26,19 +31,25 @@ def method_signature
end
def obj_hash
- @obj_hash ||= obj.hash
+ @obj_hash ||= ::CacheMethod.hashcode(obj)
end
def cache_key
- [ 'CacheMethod', method_signature, obj_hash ].join ','
+ [ 'CacheMethod', 'Epoch', method_signature, obj_hash ].join ','
end
def current
- Config.instance.storage.get(cache_key).to_i
+ if cached_v = Config.instance.storage.get(cache_key)
+ cached_v
+ else
+ v = Epoch.random_name
+ Config.instance.storage.set cache_key, v
+ v
+ end
end
def mark_passing
- Config.instance.storage.set cache_key, (current+1), 0
+ Config.instance.storage.delete cache_key
end
end
end
@@ -1,3 +1,3 @@
module CacheMethod
- VERSION = "0.0.2"
+ VERSION = "0.0.3"
end
View
@@ -10,6 +10,39 @@
class Test::Unit::TestCase
end
+class CopyCat1
+ attr_reader :name
+ def initialize(name)
+ @name = name
+ end
+ attr_writer :echo_count
+ def echo_count
+ @echo_count ||= 0
+ end
+ def echo(*args)
+ self.echo_count += 1
+ return *args
+ end
+ def hash
+ name.hash
+ end
+ cache_method :echo
+end
+
+class CopyCat2
+ class << self
+ attr_writer :echo_count
+ def echo_count
+ @echo_count ||= 0
+ end
+ def echo(*args)
+ self.echo_count += 1
+ return *args
+ end
+ cache_method :echo
+ end
+end
+
class Blog1
attr_reader :name
attr_reader :url
View
@@ -5,11 +5,54 @@
class TestCacheMethod < Test::Unit::TestCase
def setup
Blog2.request_count = 0
+ CopyCat2.echo_count = 0
my_cache = Memcached.new '127.0.0.1:11211'
CacheMethod.config.storage = my_cache
my_cache.flush
end
+ def test_cache_instance_method_with_args
+ a = CopyCat1.new 'mimo'
+
+ assert_equal ['hi'], a.echo(['hi'])
+ assert_equal 1, a.echo_count
+
+ assert_equal ['hi'], a.echo(['hi'])
+ assert_equal 1, a.echo_count
+
+ assert_equal ['bye'], a.echo(['bye'])
+ assert_equal 2, a.echo_count
+
+ assert_equal ['bye'], a.echo(['bye'])
+ assert_equal 2, a.echo_count
+
+ assert_equal nil, a.echo
+ assert_equal 3, a.echo_count
+
+ assert_equal nil, a.echo
+ assert_equal 3, a.echo_count
+ end
+
+ def test_cache_class_method_with_args
+ assert_equal ['hi'], CopyCat2.echo(['hi'])
+ assert_equal 1, CopyCat2.echo_count
+
+ assert_equal ['hi'], CopyCat2.echo(['hi'])
+ assert_equal 1, CopyCat2.echo_count
+
+ assert_equal ['bye'], CopyCat2.echo(['bye'])
+ assert_equal 2, CopyCat2.echo_count
+
+ assert_equal ['bye'], CopyCat2.echo(['bye'])
+ assert_equal 2, CopyCat2.echo_count
+
+ assert_equal nil, CopyCat2.echo
+ assert_equal 3, CopyCat2.echo_count
+
+ assert_equal nil, CopyCat2.echo
+ assert_equal 3, CopyCat2.echo_count
+ end
+
def test_cache_instance_method
a = new_instance_of_my_blog
assert_equal ["hello from #{a.name}"], a.get_latest_entries

0 comments on commit 9999ca8

Please sign in to comment.