1
0

test_stream_change_cache.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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):
  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):
  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. def test_entity_has_changed_pops_off_start(self):
  43. """
  44. StreamChangeCache.entity_has_changed will respect the max size and
  45. purge the oldest items upon reaching that max size.
  46. """
  47. cache = StreamChangeCache("#test", 1, max_size=2)
  48. cache.entity_has_changed("user@foo.com", 2)
  49. cache.entity_has_changed("bar@baz.net", 3)
  50. cache.entity_has_changed("user@elsewhere.org", 4)
  51. # The cache is at the max size, 2
  52. self.assertEqual(len(cache._cache), 2)
  53. # The oldest item has been popped off
  54. self.assertTrue("user@foo.com" not in cache._entity_to_key)
  55. self.assertEqual(
  56. cache.get_all_entities_changed(2), ["bar@baz.net", "user@elsewhere.org"],
  57. )
  58. self.assertIsNone(cache.get_all_entities_changed(1))
  59. # If we update an existing entity, it keeps the two existing entities
  60. cache.entity_has_changed("bar@baz.net", 5)
  61. self.assertEqual(
  62. {"bar@baz.net", "user@elsewhere.org"}, set(cache._entity_to_key)
  63. )
  64. self.assertEqual(
  65. cache.get_all_entities_changed(2), ["user@elsewhere.org", "bar@baz.net"],
  66. )
  67. self.assertIsNone(cache.get_all_entities_changed(1))
  68. def test_get_all_entities_changed(self):
  69. """
  70. StreamChangeCache.get_all_entities_changed will return all changed
  71. entities since the given position. If the position is before the start
  72. of the known stream, it returns None instead.
  73. """
  74. cache = StreamChangeCache("#test", 1)
  75. cache.entity_has_changed("user@foo.com", 2)
  76. cache.entity_has_changed("bar@baz.net", 3)
  77. cache.entity_has_changed("anotheruser@foo.com", 3)
  78. cache.entity_has_changed("user@elsewhere.org", 4)
  79. r = cache.get_all_entities_changed(1)
  80. # either of these are valid
  81. ok1 = [
  82. "user@foo.com",
  83. "bar@baz.net",
  84. "anotheruser@foo.com",
  85. "user@elsewhere.org",
  86. ]
  87. ok2 = [
  88. "user@foo.com",
  89. "anotheruser@foo.com",
  90. "bar@baz.net",
  91. "user@elsewhere.org",
  92. ]
  93. self.assertTrue(r == ok1 or r == ok2)
  94. r = cache.get_all_entities_changed(2)
  95. self.assertTrue(r == ok1[1:] or r == ok2[1:])
  96. self.assertEqual(cache.get_all_entities_changed(3), ["user@elsewhere.org"])
  97. self.assertEqual(cache.get_all_entities_changed(0), None)
  98. # ... later, things gest more updates
  99. cache.entity_has_changed("user@foo.com", 5)
  100. cache.entity_has_changed("bar@baz.net", 5)
  101. cache.entity_has_changed("anotheruser@foo.com", 6)
  102. ok1 = [
  103. "user@elsewhere.org",
  104. "user@foo.com",
  105. "bar@baz.net",
  106. "anotheruser@foo.com",
  107. ]
  108. ok2 = [
  109. "user@elsewhere.org",
  110. "bar@baz.net",
  111. "user@foo.com",
  112. "anotheruser@foo.com",
  113. ]
  114. r = cache.get_all_entities_changed(3)
  115. self.assertTrue(r == ok1 or r == ok2)
  116. def test_has_any_entity_changed(self):
  117. """
  118. StreamChangeCache.has_any_entity_changed will return True if any
  119. entities have been changed since the provided stream position, and
  120. False if they have not. If the cache has entries and the provided
  121. stream position is before it, it will return True, otherwise False if
  122. the cache has no entries.
  123. """
  124. cache = StreamChangeCache("#test", 1)
  125. # With no entities, it returns False for the past, present, and future.
  126. self.assertFalse(cache.has_any_entity_changed(0))
  127. self.assertFalse(cache.has_any_entity_changed(1))
  128. self.assertFalse(cache.has_any_entity_changed(2))
  129. # We add an entity
  130. cache.entity_has_changed("user@foo.com", 2)
  131. # With an entity, it returns True for the past, the stream start
  132. # position, and False for the stream position the entity was changed
  133. # on and ones after it.
  134. self.assertTrue(cache.has_any_entity_changed(0))
  135. self.assertTrue(cache.has_any_entity_changed(1))
  136. self.assertFalse(cache.has_any_entity_changed(2))
  137. self.assertFalse(cache.has_any_entity_changed(3))
  138. def test_get_entities_changed(self):
  139. """
  140. StreamChangeCache.get_entities_changed will return the entities in the
  141. given list that have changed since the provided stream ID. If the
  142. stream position is earlier than the earliest known position, it will
  143. return all of the entities queried for.
  144. """
  145. cache = StreamChangeCache("#test", 1)
  146. cache.entity_has_changed("user@foo.com", 2)
  147. cache.entity_has_changed("bar@baz.net", 3)
  148. cache.entity_has_changed("user@elsewhere.org", 4)
  149. # Query all the entries, but mid-way through the stream. We should only
  150. # get the ones after that point.
  151. self.assertEqual(
  152. cache.get_entities_changed(
  153. ["user@foo.com", "bar@baz.net", "user@elsewhere.org"], stream_pos=2
  154. ),
  155. {"bar@baz.net", "user@elsewhere.org"},
  156. )
  157. # Query all the entries mid-way through the stream, but include one
  158. # that doesn't exist in it. We shouldn't get back the one that doesn't
  159. # exist.
  160. self.assertEqual(
  161. cache.get_entities_changed(
  162. [
  163. "user@foo.com",
  164. "bar@baz.net",
  165. "user@elsewhere.org",
  166. "not@here.website",
  167. ],
  168. stream_pos=2,
  169. ),
  170. {"bar@baz.net", "user@elsewhere.org"},
  171. )
  172. # Query all the entries, but before the first known point. We will get
  173. # all the entries we queried for, including ones that don't exist.
  174. self.assertEqual(
  175. cache.get_entities_changed(
  176. [
  177. "user@foo.com",
  178. "bar@baz.net",
  179. "user@elsewhere.org",
  180. "not@here.website",
  181. ],
  182. stream_pos=0,
  183. ),
  184. {"user@foo.com", "bar@baz.net", "user@elsewhere.org", "not@here.website"},
  185. )
  186. # Query a subset of the entries mid-way through the stream. We should
  187. # only get back the subset.
  188. self.assertEqual(
  189. cache.get_entities_changed(["bar@baz.net"], stream_pos=2), {"bar@baz.net"},
  190. )
  191. def test_max_pos(self):
  192. """
  193. StreamChangeCache.get_max_pos_of_last_change will return the most
  194. recent point where the entity could have changed. If the entity is not
  195. known, the stream start is provided instead.
  196. """
  197. cache = StreamChangeCache("#test", 1)
  198. cache.entity_has_changed("user@foo.com", 2)
  199. cache.entity_has_changed("bar@baz.net", 3)
  200. cache.entity_has_changed("user@elsewhere.org", 4)
  201. # Known entities will return the point where they were changed.
  202. self.assertEqual(cache.get_max_pos_of_last_change("user@foo.com"), 2)
  203. self.assertEqual(cache.get_max_pos_of_last_change("bar@baz.net"), 3)
  204. self.assertEqual(cache.get_max_pos_of_last_change("user@elsewhere.org"), 4)
  205. # Unknown entities will return the stream start position.
  206. self.assertEqual(cache.get_max_pos_of_last_change("not@here.website"), 1)