@@ -514,6 +514,9 @@ def test_last_active(self) -> None:
514
514
515
515
516
516
class PresenceHandlerTestCase (BaseMultiWorkerStreamTestCase ):
517
+ user_id = "@test:server"
518
+ user_id_obj = UserID .from_string (user_id )
519
+
517
520
def prepare (self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) -> None :
518
521
self .presence_handler = hs .get_presence_handler ()
519
522
self .clock = hs .get_clock ()
@@ -523,61 +526,49 @@ def test_external_process_timeout(self) -> None:
523
526
we time out their syncing users presence.
524
527
"""
525
528
process_id = "1"
526
- user_id = "@test:server"
527
529
528
530
# Notify handler that a user is now syncing.
529
531
self .get_success (
530
532
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 ()
532
534
)
533
535
)
534
536
535
537
# Check that if we wait a while without telling the handler the user has
536
538
# stopped syncing that their presence state doesn't get timed out.
537
539
self .reactor .advance (EXTERNAL_PROCESS_EXPIRY / 2 )
538
540
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 ))
542
542
self .assertEqual (state .state , PresenceState .ONLINE )
543
543
544
544
# Check that if the external process timeout fires, then the syncing
545
545
# user gets timed out
546
546
self .reactor .advance (EXTERNAL_PROCESS_EXPIRY )
547
547
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 ))
551
549
self .assertEqual (state .state , PresenceState .OFFLINE )
552
550
553
551
def test_user_goes_offline_by_timeout_status_msg_remain (self ) -> None :
554
552
"""Test that if a user doesn't update the records for a while
555
553
users presence goes `OFFLINE` because of timeout and `status_msg` remains.
556
554
"""
557
- user_id = "@test:server"
558
555
status_msg = "I'm here!"
559
556
560
557
# 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 )
564
559
565
560
# Check that if we wait a while without telling the handler the user has
566
561
# stopped syncing that their presence state doesn't get timed out.
567
562
self .reactor .advance (SYNC_ONLINE_TIMEOUT / 2 )
568
563
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 ))
572
565
self .assertEqual (state .state , PresenceState .ONLINE )
573
566
self .assertEqual (state .status_msg , status_msg )
574
567
575
568
# Check that if the timeout fires, then the syncing user gets timed out
576
569
self .reactor .advance (SYNC_ONLINE_TIMEOUT )
577
570
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 ))
581
572
# status_msg should remain even after going offline
582
573
self .assertEqual (state .state , PresenceState .OFFLINE )
583
574
self .assertEqual (state .status_msg , status_msg )
@@ -586,66 +577,51 @@ def test_user_goes_offline_manually_with_no_status_msg(self) -> None:
586
577
"""Test that if a user change presence manually to `OFFLINE`
587
578
and no status is set, that `status_msg` is `None`.
588
579
"""
589
- user_id = "@test:server"
590
580
status_msg = "I'm here!"
591
581
592
582
# 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 )
596
584
597
585
# Mark user as offline
598
586
self .get_success (
599
587
self .presence_handler .set_state (
600
- UserID . from_string ( user_id ) , {"presence" : PresenceState .OFFLINE }
588
+ self . user_id_obj , {"presence" : PresenceState .OFFLINE }
601
589
)
602
590
)
603
591
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 ))
607
593
self .assertEqual (state .state , PresenceState .OFFLINE )
608
594
self .assertEqual (state .status_msg , None )
609
595
610
596
def test_user_goes_offline_manually_with_status_msg (self ) -> None :
611
597
"""Test that if a user change presence manually to `OFFLINE`
612
598
and a status is set, that `status_msg` appears.
613
599
"""
614
- user_id = "@test:server"
615
600
status_msg = "I'm here!"
616
601
617
602
# 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 )
621
604
622
605
# 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." )
626
607
627
608
def test_user_reset_online_with_no_status (self ) -> None :
628
609
"""Test that if a user set again the presence manually
629
610
and no status is set, that `status_msg` is `None`.
630
611
"""
631
- user_id = "@test:server"
632
612
status_msg = "I'm here!"
633
613
634
614
# 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 )
638
616
639
617
# Mark user as online again
640
618
self .get_success (
641
619
self .presence_handler .set_state (
642
- UserID . from_string ( user_id ) , {"presence" : PresenceState .ONLINE }
620
+ self . user_id_obj , {"presence" : PresenceState .ONLINE }
643
621
)
644
622
)
645
623
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 ))
649
625
# status_msg should remain even after going offline
650
626
self .assertEqual (state .state , PresenceState .ONLINE )
651
627
self .assertEqual (state .status_msg , None )
@@ -654,84 +630,62 @@ def test_set_presence_with_status_msg_none(self) -> None:
654
630
"""Test that if a user set again the presence manually
655
631
and status is `None`, that `status_msg` is `None`.
656
632
"""
657
- user_id = "@test:server"
658
633
status_msg = "I'm here!"
659
634
660
635
# 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 )
664
637
665
638
# 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 )
667
640
668
641
def test_set_presence_from_syncing_not_set (self ) -> None :
669
642
"""Test that presence is not set by syncing if affect_presence is false"""
670
- user_id = "@test:server"
671
643
status_msg = "I'm here!"
672
644
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 )
676
646
677
647
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
+ )
679
651
)
680
652
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 ))
684
654
# we should still be unavailable
685
655
self .assertEqual (state .state , PresenceState .UNAVAILABLE )
686
656
# and status message should still be the same
687
657
self .assertEqual (state .status_msg , status_msg )
688
658
689
659
def test_set_presence_from_syncing_is_set (self ) -> None :
690
660
"""Test that presence is set by syncing if affect_presence is true"""
691
- user_id = "@test:server"
692
661
status_msg = "I'm here!"
693
662
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 )
697
664
698
665
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 )
700
667
)
701
668
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 ))
705
670
# we should now be online
706
671
self .assertEqual (state .state , PresenceState .ONLINE )
707
672
708
673
def test_set_presence_from_syncing_keeps_status (self ) -> None :
709
674
"""Test that presence set by syncing retains status message"""
710
- user_id = "@test:server"
711
675
status_msg = "I'm here!"
712
676
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 )
716
678
717
679
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 )
719
681
)
720
682
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 ))
724
684
# our status message should be the same as it was before
725
685
self .assertEqual (state .status_msg , status_msg )
726
686
727
687
@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 }})
735
689
def test_set_presence_from_syncing_keeps_busy (
736
690
self , test_with_workers : bool
737
691
) -> None :
@@ -741,7 +695,6 @@ def test_set_presence_from_syncing_keeps_busy(
741
695
test_with_workers: If True, check the presence state of the user by calling
742
696
/sync against a worker, rather than the main process.
743
697
"""
744
- user_id = "@test:server"
745
698
status_msg = "I'm busy!"
746
699
747
700
# By default, we call /sync against the main process.
@@ -755,44 +708,39 @@ def test_set_presence_from_syncing_keeps_busy(
755
708
)
756
709
757
710
# 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 )
759
712
760
713
# Perform a sync with a presence state other than busy. This should NOT change
761
714
# our presence status; we only change from busy if we explicitly set it via
762
715
# /presence/*.
763
716
self .get_success (
764
717
worker_to_sync_against .get_presence_handler ().user_syncing (
765
- user_id , True , PresenceState .ONLINE
718
+ self . user_id , True , PresenceState .ONLINE
766
719
)
767
720
)
768
721
769
722
# 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 ))
773
724
# we should still be busy
774
725
self .assertEqual (state .state , PresenceState .BUSY )
775
726
776
727
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 ]
778
729
) -> None :
779
730
"""Set a PresenceState and status_msg and check the result.
780
731
781
732
Args:
782
- user_id: User for that the status is to be set.
783
733
state: The new PresenceState.
784
734
status_msg: Status message that is to be set.
785
735
"""
786
736
self .get_success (
787
737
self .presence_handler .set_state (
788
- UserID . from_string ( user_id ) ,
738
+ self . user_id_obj ,
789
739
{"presence" : state , "status_msg" : status_msg },
790
740
)
791
741
)
792
742
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 ))
796
744
self .assertEqual (new_state .state , state )
797
745
self .assertEqual (new_state .status_msg , status_msg )
798
746
@@ -952,9 +900,6 @@ def test_partially_clear_queue(self) -> None:
952
900
self .assertEqual (upto_token , now_token )
953
901
self .assertFalse (limited )
954
902
955
- expected_rows = [
956
- (2 , ("dest3" , "@user3:test" )),
957
- ]
958
903
self .assertCountEqual (rows , [])
959
904
960
905
prev_token = self .queue .get_current_token (self .instance_name )
0 commit comments