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

Commit 3f17178

Browse files
authored
Clean-up presence tests (#16158)
Reduce duplicated code & remove unused variables.
1 parent 803f63d commit 3f17178

File tree

2 files changed

+38
-92
lines changed

2 files changed

+38
-92
lines changed

changelog.d/16158.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve presence tests.

tests/handlers/test_presence.py

Lines changed: 37 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,9 @@ def test_last_active(self) -> None:
514514

515515

516516
class PresenceHandlerTestCase(BaseMultiWorkerStreamTestCase):
517+
user_id = "@test:server"
518+
user_id_obj = UserID.from_string(user_id)
519+
517520
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
518521
self.presence_handler = hs.get_presence_handler()
519522
self.clock = hs.get_clock()
@@ -523,61 +526,49 @@ def test_external_process_timeout(self) -> None:
523526
we time out their syncing users presence.
524527
"""
525528
process_id = "1"
526-
user_id = "@test:server"
527529

528530
# Notify handler that a user is now syncing.
529531
self.get_success(
530532
self.presence_handler.update_external_syncs_row(
531-
process_id, user_id, True, self.clock.time_msec()
533+
process_id, self.user_id, True, self.clock.time_msec()
532534
)
533535
)
534536

535537
# Check that if we wait a while without telling the handler the user has
536538
# stopped syncing that their presence state doesn't get timed out.
537539
self.reactor.advance(EXTERNAL_PROCESS_EXPIRY / 2)
538540

539-
state = self.get_success(
540-
self.presence_handler.get_state(UserID.from_string(user_id))
541-
)
541+
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
542542
self.assertEqual(state.state, PresenceState.ONLINE)
543543

544544
# Check that if the external process timeout fires, then the syncing
545545
# user gets timed out
546546
self.reactor.advance(EXTERNAL_PROCESS_EXPIRY)
547547

548-
state = self.get_success(
549-
self.presence_handler.get_state(UserID.from_string(user_id))
550-
)
548+
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
551549
self.assertEqual(state.state, PresenceState.OFFLINE)
552550

553551
def test_user_goes_offline_by_timeout_status_msg_remain(self) -> None:
554552
"""Test that if a user doesn't update the records for a while
555553
users presence goes `OFFLINE` because of timeout and `status_msg` remains.
556554
"""
557-
user_id = "@test:server"
558555
status_msg = "I'm here!"
559556

560557
# Mark user as online
561-
self._set_presencestate_with_status_msg(
562-
user_id, PresenceState.ONLINE, status_msg
563-
)
558+
self._set_presencestate_with_status_msg(PresenceState.ONLINE, status_msg)
564559

565560
# Check that if we wait a while without telling the handler the user has
566561
# stopped syncing that their presence state doesn't get timed out.
567562
self.reactor.advance(SYNC_ONLINE_TIMEOUT / 2)
568563

569-
state = self.get_success(
570-
self.presence_handler.get_state(UserID.from_string(user_id))
571-
)
564+
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
572565
self.assertEqual(state.state, PresenceState.ONLINE)
573566
self.assertEqual(state.status_msg, status_msg)
574567

575568
# Check that if the timeout fires, then the syncing user gets timed out
576569
self.reactor.advance(SYNC_ONLINE_TIMEOUT)
577570

578-
state = self.get_success(
579-
self.presence_handler.get_state(UserID.from_string(user_id))
580-
)
571+
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
581572
# status_msg should remain even after going offline
582573
self.assertEqual(state.state, PresenceState.OFFLINE)
583574
self.assertEqual(state.status_msg, status_msg)
@@ -586,66 +577,51 @@ def test_user_goes_offline_manually_with_no_status_msg(self) -> None:
586577
"""Test that if a user change presence manually to `OFFLINE`
587578
and no status is set, that `status_msg` is `None`.
588579
"""
589-
user_id = "@test:server"
590580
status_msg = "I'm here!"
591581

592582
# Mark user as online
593-
self._set_presencestate_with_status_msg(
594-
user_id, PresenceState.ONLINE, status_msg
595-
)
583+
self._set_presencestate_with_status_msg(PresenceState.ONLINE, status_msg)
596584

597585
# Mark user as offline
598586
self.get_success(
599587
self.presence_handler.set_state(
600-
UserID.from_string(user_id), {"presence": PresenceState.OFFLINE}
588+
self.user_id_obj, {"presence": PresenceState.OFFLINE}
601589
)
602590
)
603591

604-
state = self.get_success(
605-
self.presence_handler.get_state(UserID.from_string(user_id))
606-
)
592+
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
607593
self.assertEqual(state.state, PresenceState.OFFLINE)
608594
self.assertEqual(state.status_msg, None)
609595

610596
def test_user_goes_offline_manually_with_status_msg(self) -> None:
611597
"""Test that if a user change presence manually to `OFFLINE`
612598
and a status is set, that `status_msg` appears.
613599
"""
614-
user_id = "@test:server"
615600
status_msg = "I'm here!"
616601

617602
# Mark user as online
618-
self._set_presencestate_with_status_msg(
619-
user_id, PresenceState.ONLINE, status_msg
620-
)
603+
self._set_presencestate_with_status_msg(PresenceState.ONLINE, status_msg)
621604

622605
# Mark user as offline
623-
self._set_presencestate_with_status_msg(
624-
user_id, PresenceState.OFFLINE, "And now here."
625-
)
606+
self._set_presencestate_with_status_msg(PresenceState.OFFLINE, "And now here.")
626607

627608
def test_user_reset_online_with_no_status(self) -> None:
628609
"""Test that if a user set again the presence manually
629610
and no status is set, that `status_msg` is `None`.
630611
"""
631-
user_id = "@test:server"
632612
status_msg = "I'm here!"
633613

634614
# Mark user as online
635-
self._set_presencestate_with_status_msg(
636-
user_id, PresenceState.ONLINE, status_msg
637-
)
615+
self._set_presencestate_with_status_msg(PresenceState.ONLINE, status_msg)
638616

639617
# Mark user as online again
640618
self.get_success(
641619
self.presence_handler.set_state(
642-
UserID.from_string(user_id), {"presence": PresenceState.ONLINE}
620+
self.user_id_obj, {"presence": PresenceState.ONLINE}
643621
)
644622
)
645623

646-
state = self.get_success(
647-
self.presence_handler.get_state(UserID.from_string(user_id))
648-
)
624+
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
649625
# status_msg should remain even after going offline
650626
self.assertEqual(state.state, PresenceState.ONLINE)
651627
self.assertEqual(state.status_msg, None)
@@ -654,84 +630,62 @@ def test_set_presence_with_status_msg_none(self) -> None:
654630
"""Test that if a user set again the presence manually
655631
and status is `None`, that `status_msg` is `None`.
656632
"""
657-
user_id = "@test:server"
658633
status_msg = "I'm here!"
659634

660635
# Mark user as online
661-
self._set_presencestate_with_status_msg(
662-
user_id, PresenceState.ONLINE, status_msg
663-
)
636+
self._set_presencestate_with_status_msg(PresenceState.ONLINE, status_msg)
664637

665638
# Mark user as online and `status_msg = None`
666-
self._set_presencestate_with_status_msg(user_id, PresenceState.ONLINE, None)
639+
self._set_presencestate_with_status_msg(PresenceState.ONLINE, None)
667640

668641
def test_set_presence_from_syncing_not_set(self) -> None:
669642
"""Test that presence is not set by syncing if affect_presence is false"""
670-
user_id = "@test:server"
671643
status_msg = "I'm here!"
672644

673-
self._set_presencestate_with_status_msg(
674-
user_id, PresenceState.UNAVAILABLE, status_msg
675-
)
645+
self._set_presencestate_with_status_msg(PresenceState.UNAVAILABLE, status_msg)
676646

677647
self.get_success(
678-
self.presence_handler.user_syncing(user_id, False, PresenceState.ONLINE)
648+
self.presence_handler.user_syncing(
649+
self.user_id, False, PresenceState.ONLINE
650+
)
679651
)
680652

681-
state = self.get_success(
682-
self.presence_handler.get_state(UserID.from_string(user_id))
683-
)
653+
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
684654
# we should still be unavailable
685655
self.assertEqual(state.state, PresenceState.UNAVAILABLE)
686656
# and status message should still be the same
687657
self.assertEqual(state.status_msg, status_msg)
688658

689659
def test_set_presence_from_syncing_is_set(self) -> None:
690660
"""Test that presence is set by syncing if affect_presence is true"""
691-
user_id = "@test:server"
692661
status_msg = "I'm here!"
693662

694-
self._set_presencestate_with_status_msg(
695-
user_id, PresenceState.UNAVAILABLE, status_msg
696-
)
663+
self._set_presencestate_with_status_msg(PresenceState.UNAVAILABLE, status_msg)
697664

698665
self.get_success(
699-
self.presence_handler.user_syncing(user_id, True, PresenceState.ONLINE)
666+
self.presence_handler.user_syncing(self.user_id, True, PresenceState.ONLINE)
700667
)
701668

702-
state = self.get_success(
703-
self.presence_handler.get_state(UserID.from_string(user_id))
704-
)
669+
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
705670
# we should now be online
706671
self.assertEqual(state.state, PresenceState.ONLINE)
707672

708673
def test_set_presence_from_syncing_keeps_status(self) -> None:
709674
"""Test that presence set by syncing retains status message"""
710-
user_id = "@test:server"
711675
status_msg = "I'm here!"
712676

713-
self._set_presencestate_with_status_msg(
714-
user_id, PresenceState.UNAVAILABLE, status_msg
715-
)
677+
self._set_presencestate_with_status_msg(PresenceState.UNAVAILABLE, status_msg)
716678

717679
self.get_success(
718-
self.presence_handler.user_syncing(user_id, True, PresenceState.ONLINE)
680+
self.presence_handler.user_syncing(self.user_id, True, PresenceState.ONLINE)
719681
)
720682

721-
state = self.get_success(
722-
self.presence_handler.get_state(UserID.from_string(user_id))
723-
)
683+
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
724684
# our status message should be the same as it was before
725685
self.assertEqual(state.status_msg, status_msg)
726686

727687
@parameterized.expand([(False,), (True,)])
728-
@unittest.override_config(
729-
{
730-
"experimental_features": {
731-
"msc3026_enabled": True,
732-
},
733-
}
734-
)
688+
@unittest.override_config({"experimental_features": {"msc3026_enabled": True}})
735689
def test_set_presence_from_syncing_keeps_busy(
736690
self, test_with_workers: bool
737691
) -> None:
@@ -741,7 +695,6 @@ def test_set_presence_from_syncing_keeps_busy(
741695
test_with_workers: If True, check the presence state of the user by calling
742696
/sync against a worker, rather than the main process.
743697
"""
744-
user_id = "@test:server"
745698
status_msg = "I'm busy!"
746699

747700
# By default, we call /sync against the main process.
@@ -755,44 +708,39 @@ def test_set_presence_from_syncing_keeps_busy(
755708
)
756709

757710
# Set presence to BUSY
758-
self._set_presencestate_with_status_msg(user_id, PresenceState.BUSY, status_msg)
711+
self._set_presencestate_with_status_msg(PresenceState.BUSY, status_msg)
759712

760713
# Perform a sync with a presence state other than busy. This should NOT change
761714
# our presence status; we only change from busy if we explicitly set it via
762715
# /presence/*.
763716
self.get_success(
764717
worker_to_sync_against.get_presence_handler().user_syncing(
765-
user_id, True, PresenceState.ONLINE
718+
self.user_id, True, PresenceState.ONLINE
766719
)
767720
)
768721

769722
# Check against the main process that the user's presence did not change.
770-
state = self.get_success(
771-
self.presence_handler.get_state(UserID.from_string(user_id))
772-
)
723+
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
773724
# we should still be busy
774725
self.assertEqual(state.state, PresenceState.BUSY)
775726

776727
def _set_presencestate_with_status_msg(
777-
self, user_id: str, state: str, status_msg: Optional[str]
728+
self, state: str, status_msg: Optional[str]
778729
) -> None:
779730
"""Set a PresenceState and status_msg and check the result.
780731
781732
Args:
782-
user_id: User for that the status is to be set.
783733
state: The new PresenceState.
784734
status_msg: Status message that is to be set.
785735
"""
786736
self.get_success(
787737
self.presence_handler.set_state(
788-
UserID.from_string(user_id),
738+
self.user_id_obj,
789739
{"presence": state, "status_msg": status_msg},
790740
)
791741
)
792742

793-
new_state = self.get_success(
794-
self.presence_handler.get_state(UserID.from_string(user_id))
795-
)
743+
new_state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
796744
self.assertEqual(new_state.state, state)
797745
self.assertEqual(new_state.status_msg, status_msg)
798746

@@ -952,9 +900,6 @@ def test_partially_clear_queue(self) -> None:
952900
self.assertEqual(upto_token, now_token)
953901
self.assertFalse(limited)
954902

955-
expected_rows = [
956-
(2, ("dest3", "@user3:test")),
957-
]
958903
self.assertCountEqual(rows, [])
959904

960905
prev_token = self.queue.get_current_token(self.instance_name)

0 commit comments

Comments
 (0)