Source

dogpile.cache / tests / cache / test_region.py

Diff from to

File tests/cache/test_region.py

         eq_(reg.get("some key"), "some value 2")
 
 
-    def test_invalidate_get(self):
+    def test_hard_invalidate_get(self):
         reg = self._region()
         reg.set("some key", "some value")
         reg.invalidate()
         is_(reg.get("some key"), NO_VALUE)
 
-    def test_invalidate_get_or_create(self):
+    def test_hard_invalidate_get_or_create(self):
         reg = self._region()
         counter = itertools.count(1)
         def creator():
         eq_(reg.get_or_create("some key", creator),
                     "some value 2")
 
+    def test_soft_invalidate_get(self):
+        reg = self._region(config_args={"expiration_time": 1})
+        reg.set("some key", "some value")
+        reg.invalidate(hard=False)
+        is_(reg.get("some key"), NO_VALUE)
+
+    def test_soft_invalidate_get_or_create(self):
+        reg = self._region(config_args={"expiration_time": 1})
+        counter = itertools.count(1)
+        def creator():
+            return "some value %d" % next(counter)
+        eq_(reg.get_or_create("some key", creator),
+                    "some value 1")
+
+        reg.invalidate(hard=False)
+        eq_(reg.get_or_create("some key", creator),
+                    "some value 2")
+
+    def test_soft_invalidate_get_or_create_multi(self):
+        reg = self._region(config_args={"expiration_time": 5})
+        values = [1, 2, 3]
+        def creator(*keys):
+            v = values.pop(0)
+            return [v for k in keys]
+        ret = reg.get_or_create_multi(
+                    [1, 2], creator)
+        eq_(ret, [1, 1])
+        reg.invalidate(hard=False)
+        ret = reg.get_or_create_multi(
+                    [1, 2], creator)
+        eq_(ret, [2, 2])
+
+    def test_soft_invalidate_requires_expire_time_get(self):
+        reg = self._region()
+        reg.invalidate(hard=False)
+        assert_raises_message(
+            exception.DogpileCacheException,
+            "Non-None expiration time required for soft invalidation",
+            reg.get_or_create, "some key", lambda: "x"
+        )
+
+    def test_soft_invalidate_requires_expire_time_get_multi(self):
+        reg = self._region()
+        reg.invalidate(hard=False)
+        assert_raises_message(
+            exception.DogpileCacheException,
+            "Non-None expiration time required for soft invalidation",
+            reg.get_or_create_multi, ["k1", "k2"], lambda k: "x"
+        )
+
     def test_should_cache_fn(self):
         reg = self._region()
         values = [1, 2, 3]
         eq_(ret, 3)
         eq_(reg.backend._cache['some key'][0], 3)
 
+
     def test_should_cache_fn_multi(self):
         reg = self._region()
         values = [1, 2, 3]