summary refs log tree commit diff
path: root/tests/util
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2016-02-10 14:52:06 +0000
committerErik Johnston <erik@matrix.org>2016-02-10 14:52:06 +0000
commit907c1faf1e4655659d9a9db1615e911640b9f383 (patch)
treee9f18d4f3044cab76ab5ae119505ea651194db5e /tests/util
parentMerge pull request #476 from koobs/patch-1 (diff)
parentUpdate CHANGES (diff)
downloadsynapse-907c1faf1e4655659d9a9db1615e911640b9f383.tar.xz
Merge branch 'release-v0.13.0' of github.com:matrix-org/synapse v0.13.0
Diffstat (limited to 'tests/util')
-rw-r--r--tests/util/__init__.py2
-rw-r--r--tests/util/test_dict_cache.py2
-rw-r--r--tests/util/test_log_context.py10
-rw-r--r--tests/util/test_lrucache.py29
-rw-r--r--tests/util/test_snapshot_cache.py2
-rw-r--r--tests/util/test_treecache.py78
6 files changed, 110 insertions, 13 deletions
diff --git a/tests/util/__init__.py b/tests/util/__init__.py
index 9bff9ec169..d0e9399dda 100644
--- a/tests/util/__init__.py
+++ b/tests/util/__init__.py
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright 2014 OpenMarket Ltd
+# Copyright 2014-2016 OpenMarket Ltd
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
diff --git a/tests/util/test_dict_cache.py b/tests/util/test_dict_cache.py
index 54ff26cd97..7bbe795622 100644
--- a/tests/util/test_dict_cache.py
+++ b/tests/util/test_dict_cache.py
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright 2015 OpenMarket Ltd
+# Copyright 2015, 2016 OpenMarket Ltd
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
diff --git a/tests/util/test_log_context.py b/tests/util/test_log_context.py
index efa0f28bad..65a330a0e9 100644
--- a/tests/util/test_log_context.py
+++ b/tests/util/test_log_context.py
@@ -5,6 +5,7 @@ from .. import unittest
 from synapse.util.async import sleep
 from synapse.util.logcontext import LoggingContext
 
+
 class LoggingContextTestCase(unittest.TestCase):
 
     def _check_test_key(self, value):
@@ -17,15 +18,6 @@ class LoggingContextTestCase(unittest.TestCase):
             context_one.test_key = "test"
             self._check_test_key("test")
 
-    def test_chaining(self):
-        with LoggingContext() as context_one:
-            context_one.test_key = "one"
-            with LoggingContext() as context_two:
-                self._check_test_key("one")
-                context_two.test_key = "two"
-                self._check_test_key("two")
-            self._check_test_key("one")
-
     @defer.inlineCallbacks
     def test_sleep(self):
         @defer.inlineCallbacks
diff --git a/tests/util/test_lrucache.py b/tests/util/test_lrucache.py
index fc5a904323..bab366fb7f 100644
--- a/tests/util/test_lrucache.py
+++ b/tests/util/test_lrucache.py
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright 2015 OpenMarket Ltd
+# Copyright 2015, 2016 OpenMarket Ltd
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
 from .. import unittest
 
 from synapse.util.caches.lrucache import LruCache
+from synapse.util.caches.treecache import TreeCache
+
 
 class LruCacheTestCase(unittest.TestCase):
 
@@ -52,3 +54,28 @@ class LruCacheTestCase(unittest.TestCase):
         cache["key"] = 1
         self.assertEquals(cache.pop("key"), 1)
         self.assertEquals(cache.pop("key"), None)
+
+    def test_del_multi(self):
+        cache = LruCache(4, 2, cache_type=TreeCache)
+        cache[("animal", "cat")] = "mew"
+        cache[("animal", "dog")] = "woof"
+        cache[("vehicles", "car")] = "vroom"
+        cache[("vehicles", "train")] = "chuff"
+
+        self.assertEquals(len(cache), 4)
+
+        self.assertEquals(cache.get(("animal", "cat")), "mew")
+        self.assertEquals(cache.get(("vehicles", "car")), "vroom")
+        cache.del_multi(("animal",))
+        self.assertEquals(len(cache), 2)
+        self.assertEquals(cache.get(("animal", "cat")), None)
+        self.assertEquals(cache.get(("animal", "dog")), None)
+        self.assertEquals(cache.get(("vehicles", "car")), "vroom")
+        self.assertEquals(cache.get(("vehicles", "train")), "chuff")
+        # Man from del_multi say "Yes".
+
+    def test_clear(self):
+        cache = LruCache(1)
+        cache["key"] = 1
+        cache.clear()
+        self.assertEquals(len(cache), 0)
diff --git a/tests/util/test_snapshot_cache.py b/tests/util/test_snapshot_cache.py
index f58576c941..4ee0d49673 100644
--- a/tests/util/test_snapshot_cache.py
+++ b/tests/util/test_snapshot_cache.py
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright 2015 OpenMarket Ltd
+# Copyright 2015, 2016 OpenMarket Ltd
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
diff --git a/tests/util/test_treecache.py b/tests/util/test_treecache.py
new file mode 100644
index 0000000000..1efbeb6b33
--- /dev/null
+++ b/tests/util/test_treecache.py
@@ -0,0 +1,78 @@
+# -*- coding: utf-8 -*-
+# Copyright 2015, 2016 OpenMarket Ltd
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+from .. import unittest
+
+from synapse.util.caches.treecache import TreeCache
+
+class TreeCacheTestCase(unittest.TestCase):
+    def test_get_set_onelevel(self):
+        cache = TreeCache()
+        cache[("a",)] = "A"
+        cache[("b",)] = "B"
+        self.assertEquals(cache.get(("a",)), "A")
+        self.assertEquals(cache.get(("b",)), "B")
+        self.assertEquals(len(cache), 2)
+
+    def test_pop_onelevel(self):
+        cache = TreeCache()
+        cache[("a",)] = "A"
+        cache[("b",)] = "B"
+        self.assertEquals(cache.pop(("a",)), "A")
+        self.assertEquals(cache.pop(("a",)), None)
+        self.assertEquals(cache.get(("b",)), "B")
+        self.assertEquals(len(cache), 1)
+
+    def test_get_set_twolevel(self):
+        cache = TreeCache()
+        cache[("a", "a")] = "AA"
+        cache[("a", "b")] = "AB"
+        cache[("b", "a")] = "BA"
+        self.assertEquals(cache.get(("a", "a")), "AA")
+        self.assertEquals(cache.get(("a", "b")), "AB")
+        self.assertEquals(cache.get(("b", "a")), "BA")
+        self.assertEquals(len(cache), 3)
+
+    def test_pop_twolevel(self):
+        cache = TreeCache()
+        cache[("a", "a")] = "AA"
+        cache[("a", "b")] = "AB"
+        cache[("b", "a")] = "BA"
+        self.assertEquals(cache.pop(("a", "a")), "AA")
+        self.assertEquals(cache.get(("a", "a")), None)
+        self.assertEquals(cache.get(("a", "b")), "AB")
+        self.assertEquals(cache.pop(("b", "a")), "BA")
+        self.assertEquals(cache.pop(("b", "a")), None)
+        self.assertEquals(len(cache), 1)
+
+    def test_pop_mixedlevel(self):
+        cache = TreeCache()
+        cache[("a", "a")] = "AA"
+        cache[("a", "b")] = "AB"
+        cache[("b", "a")] = "BA"
+        self.assertEquals(cache.get(("a", "a")), "AA")
+        cache.pop(("a",))
+        self.assertEquals(cache.get(("a", "a")), None)
+        self.assertEquals(cache.get(("a", "b")), None)
+        self.assertEquals(cache.get(("b", "a")), "BA")
+        self.assertEquals(len(cache), 1)
+
+    def test_clear(self):
+        cache = TreeCache()
+        cache[("a",)] = "A"
+        cache[("b",)] = "B"
+        cache.clear()
+        self.assertEquals(len(cache), 0)