25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

271 lines
9.0 KiB

  1. # Copyright 2016-2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from unittest.mock import Mock
  15. from twisted.test.proto_helpers import MemoryReactor
  16. from synapse.server import HomeServer
  17. from synapse.storage.databases.main.event_push_actions import NotifCounts
  18. from synapse.util import Clock
  19. from tests.unittest import HomeserverTestCase
  20. USER_ID = "@user:example.com"
  21. PlAIN_NOTIF = ["notify", {"set_tweak": "highlight", "value": False}]
  22. HIGHLIGHT = [
  23. "notify",
  24. {"set_tweak": "sound", "value": "default"},
  25. {"set_tweak": "highlight"},
  26. ]
  27. class EventPushActionsStoreTestCase(HomeserverTestCase):
  28. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  29. self.store = hs.get_datastores().main
  30. persist_events_store = hs.get_datastores().persist_events
  31. assert persist_events_store is not None
  32. self.persist_events_store = persist_events_store
  33. def test_get_unread_push_actions_for_user_in_range_for_http(self) -> None:
  34. self.get_success(
  35. self.store.get_unread_push_actions_for_user_in_range_for_http(
  36. USER_ID, 0, 1000, 20
  37. )
  38. )
  39. def test_get_unread_push_actions_for_user_in_range_for_email(self) -> None:
  40. self.get_success(
  41. self.store.get_unread_push_actions_for_user_in_range_for_email(
  42. USER_ID, 0, 1000, 20
  43. )
  44. )
  45. def test_count_aggregation(self) -> None:
  46. room_id = "!foo:example.com"
  47. user_id = "@user1235:test"
  48. last_read_stream_ordering = [0]
  49. def _assert_counts(noitf_count: int, highlight_count: int) -> None:
  50. counts = self.get_success(
  51. self.store.db_pool.runInteraction(
  52. "",
  53. self.store._get_unread_counts_by_pos_txn,
  54. room_id,
  55. user_id,
  56. last_read_stream_ordering[0],
  57. )
  58. )
  59. self.assertEqual(
  60. counts,
  61. NotifCounts(
  62. notify_count=noitf_count,
  63. unread_count=0, # Unread counts are tested in the sync tests.
  64. highlight_count=highlight_count,
  65. ),
  66. )
  67. def _inject_actions(stream: int, action: list) -> None:
  68. event = Mock()
  69. event.room_id = room_id
  70. event.event_id = f"$test{stream}:example.com"
  71. event.internal_metadata.stream_ordering = stream
  72. event.internal_metadata.is_outlier.return_value = False
  73. event.depth = stream
  74. self.store._events_stream_cache.entity_has_changed(room_id, stream)
  75. self.get_success(
  76. self.store.db_pool.simple_insert(
  77. table="events",
  78. values={
  79. "stream_ordering": stream,
  80. "topological_ordering": stream,
  81. "type": "m.room.message",
  82. "room_id": room_id,
  83. "processed": True,
  84. "outlier": False,
  85. "event_id": event.event_id,
  86. },
  87. )
  88. )
  89. self.get_success(
  90. self.store.add_push_actions_to_staging(
  91. event.event_id,
  92. {user_id: action},
  93. False,
  94. )
  95. )
  96. self.get_success(
  97. self.store.db_pool.runInteraction(
  98. "",
  99. self.persist_events_store._set_push_actions_for_event_and_users_txn,
  100. [(event, None)],
  101. [(event, None)],
  102. )
  103. )
  104. def _rotate(stream: int) -> None:
  105. self.get_success(
  106. self.store.db_pool.runInteraction(
  107. "rotate-receipts", self.store._handle_new_receipts_for_notifs_txn
  108. )
  109. )
  110. self.get_success(
  111. self.store.db_pool.runInteraction(
  112. "rotate-notifs", self.store._rotate_notifs_before_txn, stream
  113. )
  114. )
  115. def _mark_read(stream: int, depth: int) -> None:
  116. last_read_stream_ordering[0] = stream
  117. self.get_success(
  118. self.store.insert_receipt(
  119. room_id,
  120. "m.read",
  121. user_id=user_id,
  122. event_ids=[f"$test{stream}:example.com"],
  123. data={},
  124. )
  125. )
  126. _assert_counts(0, 0)
  127. _inject_actions(1, PlAIN_NOTIF)
  128. _assert_counts(1, 0)
  129. _rotate(1)
  130. _assert_counts(1, 0)
  131. _inject_actions(3, PlAIN_NOTIF)
  132. _assert_counts(2, 0)
  133. _rotate(3)
  134. _assert_counts(2, 0)
  135. _inject_actions(5, PlAIN_NOTIF)
  136. _mark_read(3, 3)
  137. _assert_counts(1, 0)
  138. _mark_read(5, 5)
  139. _assert_counts(0, 0)
  140. _inject_actions(6, PlAIN_NOTIF)
  141. _rotate(6)
  142. _assert_counts(1, 0)
  143. self.get_success(
  144. self.store.db_pool.simple_delete(
  145. table="event_push_actions", keyvalues={"1": 1}, desc=""
  146. )
  147. )
  148. _assert_counts(1, 0)
  149. _mark_read(6, 6)
  150. _assert_counts(0, 0)
  151. _inject_actions(8, HIGHLIGHT)
  152. _assert_counts(1, 1)
  153. _rotate(8)
  154. _assert_counts(1, 1)
  155. # Check that adding another notification and rotating after highlight
  156. # works.
  157. _inject_actions(10, PlAIN_NOTIF)
  158. _rotate(10)
  159. _assert_counts(2, 1)
  160. # Check that sending read receipts at different points results in the
  161. # right counts.
  162. _mark_read(8, 8)
  163. _assert_counts(1, 0)
  164. _mark_read(10, 10)
  165. _assert_counts(0, 0)
  166. _inject_actions(11, HIGHLIGHT)
  167. _assert_counts(1, 1)
  168. _mark_read(11, 11)
  169. _assert_counts(0, 0)
  170. _rotate(11)
  171. _assert_counts(0, 0)
  172. def test_find_first_stream_ordering_after_ts(self) -> None:
  173. def add_event(so: int, ts: int) -> None:
  174. self.get_success(
  175. self.store.db_pool.simple_insert(
  176. "events",
  177. {
  178. "stream_ordering": so,
  179. "received_ts": ts,
  180. "event_id": "event%i" % so,
  181. "type": "",
  182. "room_id": "",
  183. "content": "",
  184. "processed": True,
  185. "outlier": False,
  186. "topological_ordering": 0,
  187. "depth": 0,
  188. },
  189. )
  190. )
  191. # start with the base case where there are no events in the table
  192. r = self.get_success(self.store.find_first_stream_ordering_after_ts(11))
  193. self.assertEqual(r, 0)
  194. # now with one event
  195. add_event(2, 10)
  196. r = self.get_success(self.store.find_first_stream_ordering_after_ts(9))
  197. self.assertEqual(r, 2)
  198. r = self.get_success(self.store.find_first_stream_ordering_after_ts(10))
  199. self.assertEqual(r, 2)
  200. r = self.get_success(self.store.find_first_stream_ordering_after_ts(11))
  201. self.assertEqual(r, 3)
  202. # add a bunch of dummy events to the events table
  203. for (stream_ordering, ts) in (
  204. (3, 110),
  205. (4, 120),
  206. (5, 120),
  207. (10, 130),
  208. (20, 140),
  209. ):
  210. add_event(stream_ordering, ts)
  211. r = self.get_success(self.store.find_first_stream_ordering_after_ts(110))
  212. self.assertEqual(r, 3, "First event after 110ms should be 3, was %i" % r)
  213. # 4 and 5 are both after 120: we want 4 rather than 5
  214. r = self.get_success(self.store.find_first_stream_ordering_after_ts(120))
  215. self.assertEqual(r, 4, "First event after 120ms should be 4, was %i" % r)
  216. r = self.get_success(self.store.find_first_stream_ordering_after_ts(129))
  217. self.assertEqual(r, 10, "First event after 129ms should be 10, was %i" % r)
  218. # check we can get the last event
  219. r = self.get_success(self.store.find_first_stream_ordering_after_ts(140))
  220. self.assertEqual(r, 20, "First event after 14ms should be 20, was %i" % r)
  221. # off the end
  222. r = self.get_success(self.store.find_first_stream_ordering_after_ts(160))
  223. self.assertEqual(r, 21)
  224. # check we can find an event at ordering zero
  225. add_event(0, 5)
  226. r = self.get_success(self.store.find_first_stream_ordering_after_ts(1))
  227. self.assertEqual(r, 0)