Commits

Jason R. Coombs  committed 8fb2517

Pulled some tests from original implementation

  • Participants
  • Parent commits 70fe082

Comments (0)

Files changed (1)

File jaraco/performance/tests/test_cache.py

 from __future__ import print_function
 
+import time
+import datetime
+
+import py.test
+from jaraco.util.timing import Stopwatch
+
 from jaraco.performance import cache
 
 def test_LRUCache():
 		r = f(choice(domain), choice(domain))
 
 	print(f.cache.hits, f.cache.misses)
+
+def setup_module(module):
+	"""
+	Create a couple of functions with caches to be exercised.
+	"""
+	@cache.LRUCache(2)
+	def cheap_function(arg1, arg2):
+		return ' + '.join(map(str, (arg1, arg2)))
+
+	@cache.LRUCache(50)
+	def expensive_function(arg1, arg2):
+		time.sleep(1)
+		return cheap_function.__wrapped__(arg1, arg2)
+
+	globals().update(cheap_function=cheap_function,
+		expensive_function=expensive_function)
+
+
+class TestCache(object):
+	def setup_method(self, method):
+		cheap_function.cache.clear()
+		expensive_function.cache.clear()
+
+	def test_simple_args(self):
+		w = Stopwatch()
+		res1 = expensive_function(1,2)
+		res2 = expensive_function(1,2)
+		elapsed = w.split()
+		assert res1 == res2
+		one_sec = datetime.timedelta(seconds=1)
+		assert one_sec <= elapsed < one_sec*2
+
+	def test_unhashable_args(self):
+		"""
+		The caching function can't take parameters that are not
+		hashable. Even if the caching handler fails, it should
+		allow subsequent calls to succeed.
+		See https://dev.yougov.com/ticket/15782
+		"""
+		py.test.raises(TypeError, cheap_function, ([], dict()))
+		# even though the above call failed, its value gets added to the
+		#  "queue", so causes errors later.
+		for n in range(1,4):
+			cheap_function('foo', n)