Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 3343035

Browse files
authored
Use a real room in the notification rotation tests. (#13260)
Instead of manually inserting fake data. This fixes some issues with having to manually calculate stream orderings and other oddities.
1 parent 7281591 commit 3343035

File tree

2 files changed

+80
-116
lines changed

2 files changed

+80
-116
lines changed

changelog.d/13260.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clean-up tests for notifications.

tests/storage/test_event_push_actions.py

Lines changed: 79 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from unittest.mock import Mock
16-
1715
from twisted.test.proto_helpers import MemoryReactor
1816

17+
from synapse.rest import admin
18+
from synapse.rest.client import login, room
1919
from synapse.server import HomeServer
2020
from synapse.storage.databases.main.event_push_actions import NotifCounts
2121
from synapse.util import Clock
@@ -24,15 +24,14 @@
2424

2525
USER_ID = "@user:example.com"
2626

27-
PlAIN_NOTIF = ["notify", {"set_tweak": "highlight", "value": False}]
28-
HIGHLIGHT = [
29-
"notify",
30-
{"set_tweak": "sound", "value": "default"},
31-
{"set_tweak": "highlight"},
32-
]
33-
3427

3528
class EventPushActionsStoreTestCase(HomeserverTestCase):
29+
servlets = [
30+
admin.register_servlets,
31+
room.register_servlets,
32+
login.register_servlets,
33+
]
34+
3635
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
3736
self.store = hs.get_datastores().main
3837
persist_events_store = hs.get_datastores().persist_events
@@ -54,154 +53,118 @@ def test_get_unread_push_actions_for_user_in_range_for_email(self) -> None:
5453
)
5554

5655
def test_count_aggregation(self) -> None:
57-
room_id = "!foo:example.com"
58-
user_id = "@user1235:test"
56+
# Create a user to receive notifications and send receipts.
57+
user_id = self.register_user("user1235", "pass")
58+
token = self.login("user1235", "pass")
59+
60+
# And another users to send events.
61+
other_id = self.register_user("other", "pass")
62+
other_token = self.login("other", "pass")
63+
64+
# Create a room and put both users in it.
65+
room_id = self.helper.create_room_as(user_id, tok=token)
66+
self.helper.join(room_id, other_id, tok=other_token)
5967

60-
last_read_stream_ordering = [0]
68+
last_event_id: str
6169

62-
def _assert_counts(noitf_count: int, highlight_count: int) -> None:
70+
def _assert_counts(
71+
noitf_count: int, unread_count: int, highlight_count: int
72+
) -> None:
6373
counts = self.get_success(
6474
self.store.db_pool.runInteraction(
65-
"",
66-
self.store._get_unread_counts_by_pos_txn,
75+
"get-unread-counts",
76+
self.store._get_unread_counts_by_receipt_txn,
6777
room_id,
6878
user_id,
69-
last_read_stream_ordering[0],
7079
)
7180
)
7281
self.assertEqual(
7382
counts,
7483
NotifCounts(
7584
notify_count=noitf_count,
76-
unread_count=0, # Unread counts are tested in the sync tests.
85+
unread_count=unread_count,
7786
highlight_count=highlight_count,
7887
),
7988
)
8089

81-
def _inject_actions(stream: int, action: list) -> None:
82-
event = Mock()
83-
event.room_id = room_id
84-
event.event_id = f"$test{stream}:example.com"
85-
event.internal_metadata.stream_ordering = stream
86-
event.internal_metadata.is_outlier.return_value = False
87-
event.depth = stream
88-
89-
self.store._events_stream_cache.entity_has_changed(room_id, stream)
90-
91-
self.get_success(
92-
self.store.db_pool.simple_insert(
93-
table="events",
94-
values={
95-
"stream_ordering": stream,
96-
"topological_ordering": stream,
97-
"type": "m.room.message",
98-
"room_id": room_id,
99-
"processed": True,
100-
"outlier": False,
101-
"event_id": event.event_id,
102-
},
103-
)
90+
def _create_event(highlight: bool = False) -> str:
91+
result = self.helper.send_event(
92+
room_id,
93+
type="m.room.message",
94+
content={"msgtype": "m.text", "body": user_id if highlight else "msg"},
95+
tok=other_token,
10496
)
97+
nonlocal last_event_id
98+
last_event_id = result["event_id"]
99+
return last_event_id
105100

106-
self.get_success(
107-
self.store.add_push_actions_to_staging(
108-
event.event_id,
109-
{user_id: action},
110-
False,
111-
)
112-
)
113-
self.get_success(
114-
self.store.db_pool.runInteraction(
115-
"",
116-
self.persist_events_store._set_push_actions_for_event_and_users_txn,
117-
[(event, None)],
118-
[(event, None)],
119-
)
120-
)
121-
122-
def _rotate(stream: int) -> None:
123-
self.get_success(
124-
self.store.db_pool.runInteraction(
125-
"rotate-receipts", self.store._handle_new_receipts_for_notifs_txn
126-
)
127-
)
128-
129-
self.get_success(
130-
self.store.db_pool.runInteraction(
131-
"rotate-notifs", self.store._rotate_notifs_before_txn, stream
132-
)
133-
)
134-
135-
def _mark_read(stream: int, depth: int) -> None:
136-
last_read_stream_ordering[0] = stream
101+
def _rotate() -> None:
102+
self.get_success(self.store._rotate_notifs())
137103

104+
def _mark_read(event_id: str) -> None:
138105
self.get_success(
139106
self.store.insert_receipt(
140107
room_id,
141108
"m.read",
142109
user_id=user_id,
143-
event_ids=[f"$test{stream}:example.com"],
110+
event_ids=[event_id],
144111
data={},
145112
)
146113
)
147114

148-
_assert_counts(0, 0)
149-
_inject_actions(1, PlAIN_NOTIF)
150-
_assert_counts(1, 0)
151-
_rotate(1)
152-
_assert_counts(1, 0)
115+
_assert_counts(0, 0, 0)
116+
_create_event()
117+
_assert_counts(1, 1, 0)
118+
_rotate()
119+
_assert_counts(1, 1, 0)
153120

154-
_inject_actions(3, PlAIN_NOTIF)
155-
_assert_counts(2, 0)
156-
_rotate(3)
157-
_assert_counts(2, 0)
121+
event_id = _create_event()
122+
_assert_counts(2, 2, 0)
123+
_rotate()
124+
_assert_counts(2, 2, 0)
158125

159-
_inject_actions(5, PlAIN_NOTIF)
160-
_mark_read(3, 3)
161-
_assert_counts(1, 0)
126+
_create_event()
127+
_mark_read(event_id)
128+
_assert_counts(1, 1, 0)
162129

163-
_mark_read(5, 5)
164-
_assert_counts(0, 0)
130+
_mark_read(last_event_id)
131+
_assert_counts(0, 0, 0)
165132

166-
_inject_actions(6, PlAIN_NOTIF)
167-
_rotate(6)
168-
_assert_counts(1, 0)
169-
170-
self.get_success(
171-
self.store.db_pool.simple_delete(
172-
table="event_push_actions", keyvalues={"1": 1}, desc=""
173-
)
174-
)
133+
_create_event()
134+
_rotate()
135+
_assert_counts(1, 1, 0)
175136

176-
_assert_counts(1, 0)
137+
# Delete old event push actions, this should not affect the (summarised) count.
138+
self.get_success(self.store._remove_old_push_actions_that_have_rotated())
139+
_assert_counts(1, 1, 0)
177140

178-
_mark_read(6, 6)
179-
_assert_counts(0, 0)
141+
_mark_read(last_event_id)
142+
_assert_counts(0, 0, 0)
180143

181-
_inject_actions(8, HIGHLIGHT)
182-
_assert_counts(1, 1)
183-
_rotate(8)
184-
_assert_counts(1, 1)
144+
event_id = _create_event(True)
145+
_assert_counts(1, 1, 1)
146+
_rotate()
147+
_assert_counts(1, 1, 1)
185148

186149
# Check that adding another notification and rotating after highlight
187150
# works.
188-
_inject_actions(10, PlAIN_NOTIF)
189-
_rotate(10)
190-
_assert_counts(2, 1)
151+
_create_event()
152+
_rotate()
153+
_assert_counts(2, 2, 1)
191154

192155
# Check that sending read receipts at different points results in the
193156
# right counts.
194-
_mark_read(8, 8)
195-
_assert_counts(1, 0)
196-
_mark_read(10, 10)
197-
_assert_counts(0, 0)
198-
199-
_inject_actions(11, HIGHLIGHT)
200-
_assert_counts(1, 1)
201-
_mark_read(11, 11)
202-
_assert_counts(0, 0)
203-
_rotate(11)
204-
_assert_counts(0, 0)
157+
_mark_read(event_id)
158+
_assert_counts(1, 1, 0)
159+
_mark_read(last_event_id)
160+
_assert_counts(0, 0, 0)
161+
162+
_create_event(True)
163+
_assert_counts(1, 1, 1)
164+
_mark_read(last_event_id)
165+
_assert_counts(0, 0, 0)
166+
_rotate()
167+
_assert_counts(0, 0, 0)
205168

206169
def test_find_first_stream_ordering_after_ts(self) -> None:
207170
def add_event(so: int, ts: int) -> None:

0 commit comments

Comments
 (0)