1
0

test_stream_change_cache.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. from mock import patch
  2. from synapse.util.caches.stream_change_cache import StreamChangeCache
  3. from tests import unittest
  4. class StreamChangeCacheTests(unittest.TestCase):
  5. """
  6. Tests for StreamChangeCache.
  7. """
  8. def test_prefilled_cache(self):
  9. """
  10. Providing a prefilled cache to StreamChangeCache will result in a cache
  11. with the prefilled-cache entered in.
  12. """
  13. cache = StreamChangeCache("#test", 1, prefilled_cache={"user@foo.com": 2})
  14. self.assertTrue(cache.has_entity_changed("user@foo.com", 1))
  15. def test_has_entity_changed(self):
  16. """
  17. StreamChangeCache.entity_has_changed will mark entities as changed, and
  18. has_entity_changed will observe the changed entities.
  19. """
  20. cache = StreamChangeCache("#test", 3)
  21. cache.entity_has_changed("user@foo.com", 6)
  22. cache.entity_has_changed("bar@baz.net", 7)
  23. # If it's been changed after that stream position, return True
  24. self.assertTrue(cache.has_entity_changed("user@foo.com", 4))
  25. self.assertTrue(cache.has_entity_changed("bar@baz.net", 4))
  26. # If it's been changed at that stream position, return False
  27. self.assertFalse(cache.has_entity_changed("user@foo.com", 6))
  28. # If there's no changes after that stream position, return False
  29. self.assertFalse(cache.has_entity_changed("user@foo.com", 7))
  30. # If the entity does not exist, return False.
  31. self.assertFalse(cache.has_entity_changed("not@here.website", 7))
  32. # If we request before the stream cache's earliest known position,
  33. # return True, whether it's a known entity or not.
  34. self.assertTrue(cache.has_entity_changed("user@foo.com", 0))
  35. self.assertTrue(cache.has_entity_changed("not@here.website", 0))
  36. @patch("synapse.util.caches.CACHE_SIZE_FACTOR", 1.0)
  37. def test_has_entity_changed_pops_off_start(self):
  38. """
  39. StreamChangeCache.entity_has_changed will respect the max size and
  40. purge the oldest items upon reaching that max size.
  41. """
  42. cache = StreamChangeCache("#test", 1, max_size=2)
  43. cache.entity_has_changed("user@foo.com", 2)
  44. cache.entity_has_changed("bar@baz.net", 3)
  45. cache.entity_has_changed("user@elsewhere.org", 4)
  46. # The cache is at the max size, 2
  47. self.assertEqual(len(cache._cache), 2)
  48. # The oldest item has been popped off
  49. self.assertTrue("user@foo.com" not in cache._entity_to_key)
  50. # If we update an existing entity, it keeps the two existing entities
  51. cache.entity_has_changed("bar@baz.net", 5)
  52. self.assertEqual(
  53. set(["bar@baz.net", "user@elsewhere.org"]), set(cache._entity_to_key)
  54. )
  55. def test_get_all_entities_changed(self):
  56. """
  57. StreamChangeCache.get_all_entities_changed will return all changed
  58. entities since the given position. If the position is before the start
  59. of the known stream, it returns None instead.
  60. """
  61. cache = StreamChangeCache("#test", 1)
  62. cache.entity_has_changed("user@foo.com", 2)
  63. cache.entity_has_changed("bar@baz.net", 3)
  64. cache.entity_has_changed("user@elsewhere.org", 4)
  65. self.assertEqual(
  66. cache.get_all_entities_changed(1),
  67. ["user@foo.com", "bar@baz.net", "user@elsewhere.org"],
  68. )
  69. self.assertEqual(
  70. cache.get_all_entities_changed(2), ["bar@baz.net", "user@elsewhere.org"]
  71. )
  72. self.assertEqual(cache.get_all_entities_changed(3), ["user@elsewhere.org"])
  73. self.assertEqual(cache.get_all_entities_changed(0), None)
  74. def test_has_any_entity_changed(self):
  75. """
  76. StreamChangeCache.has_any_entity_changed will return True if any
  77. entities have been changed since the provided stream position, and
  78. False if they have not. If the cache has entries and the provided
  79. stream position is before it, it will return True, otherwise False if
  80. the cache has no entries.
  81. """
  82. cache = StreamChangeCache("#test", 1)
  83. # With no entities, it returns False for the past, present, and future.
  84. self.assertFalse(cache.has_any_entity_changed(0))
  85. self.assertFalse(cache.has_any_entity_changed(1))
  86. self.assertFalse(cache.has_any_entity_changed(2))
  87. # We add an entity
  88. cache.entity_has_changed("user@foo.com", 2)
  89. # With an entity, it returns True for the past, the stream start
  90. # position, and False for the stream position the entity was changed
  91. # on and ones after it.
  92. self.assertTrue(cache.has_any_entity_changed(0))
  93. self.assertTrue(cache.has_any_entity_changed(1))
  94. self.assertFalse(cache.has_any_entity_changed(2))
  95. self.assertFalse(cache.has_any_entity_changed(3))
  96. def test_get_entities_changed(self):
  97. """
  98. StreamChangeCache.get_entities_changed will return the entities in the
  99. given list that have changed since the provided stream ID. If the
  100. stream position is earlier than the earliest known position, it will
  101. return all of the entities queried for.
  102. """
  103. cache = StreamChangeCache("#test", 1)
  104. cache.entity_has_changed("user@foo.com", 2)
  105. cache.entity_has_changed("bar@baz.net", 3)
  106. cache.entity_has_changed("user@elsewhere.org", 4)
  107. # Query all the entries, but mid-way through the stream. We should only
  108. # get the ones after that point.
  109. self.assertEqual(
  110. cache.get_entities_changed(
  111. ["user@foo.com", "bar@baz.net", "user@elsewhere.org"], stream_pos=2
  112. ),
  113. set(["bar@baz.net", "user@elsewhere.org"]),
  114. )
  115. # Query all the entries mid-way through the stream, but include one
  116. # that doesn't exist in it. We shouldn't get back the one that doesn't
  117. # exist.
  118. self.assertEqual(
  119. cache.get_entities_changed(
  120. [
  121. "user@foo.com",
  122. "bar@baz.net",
  123. "user@elsewhere.org",
  124. "not@here.website",
  125. ],
  126. stream_pos=2,
  127. ),
  128. set(["bar@baz.net", "user@elsewhere.org"]),
  129. )
  130. # Query all the entries, but before the first known point. We will get
  131. # all the entries we queried for, including ones that don't exist.
  132. self.assertEqual(
  133. cache.get_entities_changed(
  134. [
  135. "user@foo.com",
  136. "bar@baz.net",
  137. "user@elsewhere.org",
  138. "not@here.website",
  139. ],
  140. stream_pos=0,
  141. ),
  142. set(
  143. [
  144. "user@foo.com",
  145. "bar@baz.net",
  146. "user@elsewhere.org",
  147. "not@here.website",
  148. ]
  149. ),
  150. )
  151. # Query a subset of the entries mid-way through the stream. We should
  152. # only get back the subset.
  153. self.assertEqual(
  154. cache.get_entities_changed(["bar@baz.net"], stream_pos=2),
  155. set(["bar@baz.net"]),
  156. )
  157. def test_max_pos(self):
  158. """
  159. StreamChangeCache.get_max_pos_of_last_change will return the most
  160. recent point where the entity could have changed. If the entity is not
  161. known, the stream start is provided instead.
  162. """
  163. cache = StreamChangeCache("#test", 1)
  164. cache.entity_has_changed("user@foo.com", 2)
  165. cache.entity_has_changed("bar@baz.net", 3)
  166. cache.entity_has_changed("user@elsewhere.org", 4)
  167. # Known entities will return the point where they were changed.
  168. self.assertEqual(cache.get_max_pos_of_last_change("user@foo.com"), 2)
  169. self.assertEqual(cache.get_max_pos_of_last_change("bar@baz.net"), 3)
  170. self.assertEqual(cache.get_max_pos_of_last_change("user@elsewhere.org"), 4)
  171. # Unknown entities will return the stream start position.
  172. self.assertEqual(cache.get_max_pos_of_last_change("not@here.website"), 1)