1
0

test_stream_change_cache.py 9.5 KB

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