test_event_push_actions.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 synapse.storage.databases.main.event_push_actions import NotifCounts
  16. from tests.unittest import HomeserverTestCase
  17. USER_ID = "@user:example.com"
  18. PlAIN_NOTIF = ["notify", {"set_tweak": "highlight", "value": False}]
  19. HIGHLIGHT = [
  20. "notify",
  21. {"set_tweak": "sound", "value": "default"},
  22. {"set_tweak": "highlight"},
  23. ]
  24. class EventPushActionsStoreTestCase(HomeserverTestCase):
  25. def prepare(self, reactor, clock, hs):
  26. self.store = hs.get_datastore()
  27. self.persist_events_store = hs.get_datastores().persist_events
  28. def test_get_unread_push_actions_for_user_in_range_for_http(self):
  29. self.get_success(
  30. self.store.get_unread_push_actions_for_user_in_range_for_http(
  31. USER_ID, 0, 1000, 20
  32. )
  33. )
  34. def test_get_unread_push_actions_for_user_in_range_for_email(self):
  35. self.get_success(
  36. self.store.get_unread_push_actions_for_user_in_range_for_email(
  37. USER_ID, 0, 1000, 20
  38. )
  39. )
  40. def test_count_aggregation(self):
  41. room_id = "!foo:example.com"
  42. user_id = "@user1235:example.com"
  43. def _assert_counts(noitf_count, highlight_count):
  44. counts = self.get_success(
  45. self.store.db_pool.runInteraction(
  46. "", self.store._get_unread_counts_by_pos_txn, room_id, user_id, 0
  47. )
  48. )
  49. self.assertEquals(
  50. counts,
  51. NotifCounts(
  52. notify_count=noitf_count,
  53. unread_count=0, # Unread counts are tested in the sync tests.
  54. highlight_count=highlight_count,
  55. ),
  56. )
  57. def _inject_actions(stream, action):
  58. event = Mock()
  59. event.room_id = room_id
  60. event.event_id = "$test:example.com"
  61. event.internal_metadata.stream_ordering = stream
  62. event.internal_metadata.is_outlier.return_value = False
  63. event.depth = stream
  64. self.get_success(
  65. self.store.add_push_actions_to_staging(
  66. event.event_id,
  67. {user_id: action},
  68. False,
  69. )
  70. )
  71. self.get_success(
  72. self.store.db_pool.runInteraction(
  73. "",
  74. self.persist_events_store._set_push_actions_for_event_and_users_txn,
  75. [(event, None)],
  76. [(event, None)],
  77. )
  78. )
  79. def _rotate(stream):
  80. self.get_success(
  81. self.store.db_pool.runInteraction(
  82. "", self.store._rotate_notifs_before_txn, stream
  83. )
  84. )
  85. def _mark_read(stream, depth):
  86. self.get_success(
  87. self.store.db_pool.runInteraction(
  88. "",
  89. self.store._remove_old_push_actions_before_txn,
  90. room_id,
  91. user_id,
  92. stream,
  93. )
  94. )
  95. _assert_counts(0, 0)
  96. _inject_actions(1, PlAIN_NOTIF)
  97. _assert_counts(1, 0)
  98. _rotate(2)
  99. _assert_counts(1, 0)
  100. _inject_actions(3, PlAIN_NOTIF)
  101. _assert_counts(2, 0)
  102. _rotate(4)
  103. _assert_counts(2, 0)
  104. _inject_actions(5, PlAIN_NOTIF)
  105. _mark_read(3, 3)
  106. _assert_counts(1, 0)
  107. _mark_read(5, 5)
  108. _assert_counts(0, 0)
  109. _inject_actions(6, PlAIN_NOTIF)
  110. _rotate(7)
  111. self.get_success(
  112. self.store.db_pool.simple_delete(
  113. table="event_push_actions", keyvalues={"1": 1}, desc=""
  114. )
  115. )
  116. _assert_counts(1, 0)
  117. _mark_read(7, 7)
  118. _assert_counts(0, 0)
  119. _inject_actions(8, HIGHLIGHT)
  120. _assert_counts(1, 1)
  121. _rotate(9)
  122. _assert_counts(1, 1)
  123. _rotate(10)
  124. _assert_counts(1, 1)
  125. def test_find_first_stream_ordering_after_ts(self):
  126. def add_event(so, ts):
  127. self.get_success(
  128. self.store.db_pool.simple_insert(
  129. "events",
  130. {
  131. "stream_ordering": so,
  132. "received_ts": ts,
  133. "event_id": "event%i" % so,
  134. "type": "",
  135. "room_id": "",
  136. "content": "",
  137. "processed": True,
  138. "outlier": False,
  139. "topological_ordering": 0,
  140. "depth": 0,
  141. },
  142. )
  143. )
  144. # start with the base case where there are no events in the table
  145. r = self.get_success(self.store.find_first_stream_ordering_after_ts(11))
  146. self.assertEqual(r, 0)
  147. # now with one event
  148. add_event(2, 10)
  149. r = self.get_success(self.store.find_first_stream_ordering_after_ts(9))
  150. self.assertEqual(r, 2)
  151. r = self.get_success(self.store.find_first_stream_ordering_after_ts(10))
  152. self.assertEqual(r, 2)
  153. r = self.get_success(self.store.find_first_stream_ordering_after_ts(11))
  154. self.assertEqual(r, 3)
  155. # add a bunch of dummy events to the events table
  156. for (stream_ordering, ts) in (
  157. (3, 110),
  158. (4, 120),
  159. (5, 120),
  160. (10, 130),
  161. (20, 140),
  162. ):
  163. add_event(stream_ordering, ts)
  164. r = self.get_success(self.store.find_first_stream_ordering_after_ts(110))
  165. self.assertEqual(r, 3, "First event after 110ms should be 3, was %i" % r)
  166. # 4 and 5 are both after 120: we want 4 rather than 5
  167. r = self.get_success(self.store.find_first_stream_ordering_after_ts(120))
  168. self.assertEqual(r, 4, "First event after 120ms should be 4, was %i" % r)
  169. r = self.get_success(self.store.find_first_stream_ordering_after_ts(129))
  170. self.assertEqual(r, 10, "First event after 129ms should be 10, was %i" % r)
  171. # check we can get the last event
  172. r = self.get_success(self.store.find_first_stream_ordering_after_ts(140))
  173. self.assertEqual(r, 20, "First event after 14ms should be 20, was %i" % r)
  174. # off the end
  175. r = self.get_success(self.store.find_first_stream_ordering_after_ts(160))
  176. self.assertEqual(r, 21)
  177. # check we can find an event at ordering zero
  178. add_event(0, 5)
  179. r = self.get_success(self.store.find_first_stream_ordering_after_ts(1))
  180. self.assertEqual(r, 0)