test_lrucache.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from .. import unittest
  16. from synapse.util.lrucache import LruCache
  17. class LruCacheTestCase(unittest.TestCase):
  18. def test_get_set(self):
  19. cache = LruCache(1)
  20. cache["key"] = "value"
  21. self.assertEquals(cache.get("key"), "value")
  22. self.assertEquals(cache["key"], "value")
  23. def test_eviction(self):
  24. cache = LruCache(2)
  25. cache[1] = 1
  26. cache[2] = 2
  27. self.assertEquals(cache.get(1), 1)
  28. self.assertEquals(cache.get(2), 2)
  29. cache[3] = 3
  30. self.assertEquals(cache.get(1), None)
  31. self.assertEquals(cache.get(2), 2)
  32. self.assertEquals(cache.get(3), 3)
  33. def test_setdefault(self):
  34. cache = LruCache(1)
  35. self.assertEquals(cache.setdefault("key", 1), 1)
  36. self.assertEquals(cache.get("key"), 1)
  37. self.assertEquals(cache.setdefault("key", 2), 1)
  38. self.assertEquals(cache.get("key"), 1)
  39. def test_pop(self):
  40. cache = LruCache(1)
  41. cache["key"] = 1
  42. self.assertEquals(cache.pop("key"), 1)
  43. self.assertEquals(cache.pop("key"), None)