|
22 | 22 | from synapse.api.constants import EventTypes, HistoryVisibility |
23 | 23 | from synapse.rest.client import login, room, sync |
24 | 24 | from synapse.server import HomeServer |
| 25 | +from synapse.types import JsonDict |
25 | 26 | from synapse.util.clock import Clock |
26 | 27 |
|
27 | 28 | from tests.rest.client.sliding_sync.test_sliding_sync import SlidingSyncBase |
@@ -126,6 +127,124 @@ def test_room_subscriptions_with_join_membership(self) -> None: |
126 | 127 | response_body["rooms"][room_id1], |
127 | 128 | ) |
128 | 129 |
|
| 130 | + def test_room_subscription_required_state_expansion_returns_immediately( |
| 131 | + self, |
| 132 | + ) -> None: |
| 133 | + """ |
| 134 | + Test that adding a room subscription with stronger params than the list causes an |
| 135 | + incremental long-poll to return immediately, even without new stream activity. |
| 136 | + """ |
| 137 | + user1_id = self.register_user("user1", "pass") |
| 138 | + user1_tok = self.login(user1_id, "pass") |
| 139 | + |
| 140 | + room_id1 = self.helper.create_room_as(user1_id, tok=user1_tok) |
| 141 | + |
| 142 | + sync_body: JsonDict = { |
| 143 | + "lists": { |
| 144 | + "foo-list": { |
| 145 | + "ranges": [[0, 0]], |
| 146 | + "required_state": [], |
| 147 | + "timeline_limit": 0, |
| 148 | + } |
| 149 | + }, |
| 150 | + "conn_id": "conn_id", |
| 151 | + } |
| 152 | + _, from_token = self.do_sync(sync_body, tok=user1_tok) |
| 153 | + |
| 154 | + sync_body["room_subscriptions"] = { |
| 155 | + room_id1: { |
| 156 | + "required_state": [ |
| 157 | + [EventTypes.Create, ""], |
| 158 | + ], |
| 159 | + "timeline_limit": 0, |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + channel = self.make_request( |
| 164 | + "POST", |
| 165 | + self.sync_endpoint + f"?timeout=10000&pos={from_token}", |
| 166 | + content=sync_body, |
| 167 | + access_token=user1_tok, |
| 168 | + await_result=False, |
| 169 | + ) |
| 170 | + channel.await_result(timeout_ms=3000) |
| 171 | + self.assertEqual(channel.code, 200, channel.json_body) |
| 172 | + |
| 173 | + state_map = self.get_success( |
| 174 | + self.storage_controllers.state.get_current_state(room_id1) |
| 175 | + ) |
| 176 | + |
| 177 | + room_response = channel.json_body["rooms"][room_id1] |
| 178 | + self.assertNotIn("initial", room_response) |
| 179 | + self._assertRequiredStateIncludes( |
| 180 | + room_response["required_state"], |
| 181 | + { |
| 182 | + state_map[(EventTypes.Create, "")], |
| 183 | + }, |
| 184 | + exact=True, |
| 185 | + ) |
| 186 | + |
| 187 | + def test_room_subscription_required_state_change_returns_immediately(self) -> None: |
| 188 | + """ |
| 189 | + Test that expanding an existing room subscription's required state causes an |
| 190 | + incremental long-poll to return immediately, even without new stream activity. |
| 191 | + """ |
| 192 | + user1_id = self.register_user("user1", "pass") |
| 193 | + user1_tok = self.login(user1_id, "pass") |
| 194 | + |
| 195 | + room_id1 = self.helper.create_room_as( |
| 196 | + user1_id, tok=user1_tok, extra_content={"name": "Foo"} |
| 197 | + ) |
| 198 | + |
| 199 | + sync_body: JsonDict = { |
| 200 | + "room_subscriptions": { |
| 201 | + room_id1: { |
| 202 | + "required_state": [ |
| 203 | + [EventTypes.Create, ""], |
| 204 | + ], |
| 205 | + "timeline_limit": 0, |
| 206 | + } |
| 207 | + }, |
| 208 | + "conn_id": "conn_id", |
| 209 | + } |
| 210 | + response_body, from_token = self.do_sync(sync_body, tok=user1_tok) |
| 211 | + |
| 212 | + state_map = self.get_success( |
| 213 | + self.storage_controllers.state.get_current_state(room_id1) |
| 214 | + ) |
| 215 | + self._assertRequiredStateIncludes( |
| 216 | + response_body["rooms"][room_id1]["required_state"], |
| 217 | + { |
| 218 | + state_map[(EventTypes.Create, "")], |
| 219 | + }, |
| 220 | + exact=True, |
| 221 | + ) |
| 222 | + |
| 223 | + sync_body["room_subscriptions"][room_id1]["required_state"] = [ |
| 224 | + [EventTypes.Create, ""], |
| 225 | + [EventTypes.Name, ""], |
| 226 | + ] |
| 227 | + |
| 228 | + channel = self.make_request( |
| 229 | + "POST", |
| 230 | + self.sync_endpoint + f"?timeout=10000&pos={from_token}", |
| 231 | + content=sync_body, |
| 232 | + access_token=user1_tok, |
| 233 | + await_result=False, |
| 234 | + ) |
| 235 | + channel.await_result(timeout_ms=3000) |
| 236 | + self.assertEqual(channel.code, 200, channel.json_body) |
| 237 | + |
| 238 | + room_response = channel.json_body["rooms"][room_id1] |
| 239 | + self.assertNotIn("initial", room_response) |
| 240 | + self._assertRequiredStateIncludes( |
| 241 | + room_response["required_state"], |
| 242 | + { |
| 243 | + state_map[(EventTypes.Name, "")], |
| 244 | + }, |
| 245 | + exact=True, |
| 246 | + ) |
| 247 | + |
129 | 248 | def test_room_subscriptions_with_leave_membership(self) -> None: |
130 | 249 | """ |
131 | 250 | Test `room_subscriptions` with a leave room should give us timeline and state |
|
0 commit comments