|
27 | 27 | from twisted.test.proto_helpers import MemoryReactor
|
28 | 28 |
|
29 | 29 | from synapse.api.constants import EduTypes, RoomEncryptionAlgorithms
|
| 30 | +from synapse.api.presence import UserPresenceState |
| 31 | +from synapse.federation.sender.per_destination_queue import MAX_PRESENCE_STATES_PER_EDU |
30 | 32 | from synapse.federation.units import Transaction
|
31 | 33 | from synapse.handlers.device import DeviceHandler
|
32 | 34 | from synapse.rest import admin
|
@@ -266,6 +268,123 @@ def test_send_receipts_with_backoff(self) -> None:
|
266 | 268 | )
|
267 | 269 |
|
268 | 270 |
|
| 271 | +class FederationSenderPresenceTestCases(HomeserverTestCase): |
| 272 | + """ |
| 273 | + Test federation sending for presence updates. |
| 274 | + """ |
| 275 | + |
| 276 | + def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer: |
| 277 | + self.federation_transport_client = Mock(spec=["send_transaction"]) |
| 278 | + self.federation_transport_client.send_transaction = AsyncMock() |
| 279 | + hs = self.setup_test_homeserver( |
| 280 | + federation_transport_client=self.federation_transport_client, |
| 281 | + ) |
| 282 | + |
| 283 | + return hs |
| 284 | + |
| 285 | + def default_config(self) -> JsonDict: |
| 286 | + config = super().default_config() |
| 287 | + config["federation_sender_instances"] = None |
| 288 | + return config |
| 289 | + |
| 290 | + def test_presence_simple(self) -> None: |
| 291 | + "Test that sending a single presence update works" |
| 292 | + |
| 293 | + mock_send_transaction: AsyncMock = ( |
| 294 | + self.federation_transport_client.send_transaction |
| 295 | + ) |
| 296 | + mock_send_transaction.return_value = {} |
| 297 | + |
| 298 | + sender = self.hs.get_federation_sender() |
| 299 | + self.get_success( |
| 300 | + sender.send_presence_to_destinations( |
| 301 | + [UserPresenceState.default("@user:test")], |
| 302 | + ["server"], |
| 303 | + ) |
| 304 | + ) |
| 305 | + |
| 306 | + self.pump() |
| 307 | + |
| 308 | + # expect a call to send_transaction |
| 309 | + mock_send_transaction.assert_awaited_once() |
| 310 | + |
| 311 | + json_cb = mock_send_transaction.call_args[0][1] |
| 312 | + data = json_cb() |
| 313 | + self.assertEqual( |
| 314 | + data["edus"], |
| 315 | + [ |
| 316 | + { |
| 317 | + "edu_type": EduTypes.PRESENCE, |
| 318 | + "content": { |
| 319 | + "push": [ |
| 320 | + { |
| 321 | + "presence": "offline", |
| 322 | + "user_id": "@user:test", |
| 323 | + } |
| 324 | + ] |
| 325 | + }, |
| 326 | + } |
| 327 | + ], |
| 328 | + ) |
| 329 | + |
| 330 | + def test_presence_batched(self) -> None: |
| 331 | + """Test that sending lots of presence updates to a destination are |
| 332 | + batched, rather than having them all sent in one EDU.""" |
| 333 | + |
| 334 | + mock_send_transaction: AsyncMock = ( |
| 335 | + self.federation_transport_client.send_transaction |
| 336 | + ) |
| 337 | + mock_send_transaction.return_value = {} |
| 338 | + |
| 339 | + sender = self.hs.get_federation_sender() |
| 340 | + |
| 341 | + # We now send lots of presence updates to force the federation sender to |
| 342 | + # batch the mup. |
| 343 | + number_presence_updates_to_send = MAX_PRESENCE_STATES_PER_EDU * 2 |
| 344 | + self.get_success( |
| 345 | + sender.send_presence_to_destinations( |
| 346 | + [ |
| 347 | + UserPresenceState.default(f"@user{i}:test") |
| 348 | + for i in range(number_presence_updates_to_send) |
| 349 | + ], |
| 350 | + ["server"], |
| 351 | + ) |
| 352 | + ) |
| 353 | + |
| 354 | + self.pump() |
| 355 | + |
| 356 | + # We should have seen at least one transcation be sent by now. |
| 357 | + mock_send_transaction.assert_called() |
| 358 | + |
| 359 | + # We don't want to specify exactly how the presence EDUs get sent out, |
| 360 | + # could be one per transaction or multiple per transaction. We just want |
| 361 | + # to assert that a) each presence EDU has bounded number of updates, and |
| 362 | + # b) that all updates get sent out. |
| 363 | + presence_edus = [] |
| 364 | + for transaction_call in mock_send_transaction.call_args_list: |
| 365 | + json_cb = transaction_call[0][1] |
| 366 | + data = json_cb() |
| 367 | + |
| 368 | + for edu in data["edus"]: |
| 369 | + self.assertEqual(edu.get("edu_type"), EduTypes.PRESENCE) |
| 370 | + presence_edus.append(edu) |
| 371 | + |
| 372 | + # A set of all user presence we see, this should end up matching the |
| 373 | + # number we sent out above. |
| 374 | + seen_users: Set[str] = set() |
| 375 | + |
| 376 | + for edu in presence_edus: |
| 377 | + presence_states = edu["content"]["push"] |
| 378 | + |
| 379 | + # This is where we actually check that the number of presence |
| 380 | + # updates is bounded. |
| 381 | + self.assertLessEqual(len(presence_states), MAX_PRESENCE_STATES_PER_EDU) |
| 382 | + |
| 383 | + seen_users.update(p["user_id"] for p in presence_states) |
| 384 | + |
| 385 | + self.assertEqual(len(seen_users), number_presence_updates_to_send) |
| 386 | + |
| 387 | + |
269 | 388 | class FederationSenderDevicesTestCases(HomeserverTestCase):
|
270 | 389 | """
|
271 | 390 | Test federation sending to update devices.
|
|
0 commit comments