test_stream_change_cache.py 9.7 KB

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