test_event_push_actions.py 7.7 KB

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