@@ -817,74 +817,80 @@ def test_set_presence_from_syncing_is_set(self) -> None:
817
817
# * The expected user presence state after both devices have synced.
818
818
# * The expected user presence state after device 1 has idled.
819
819
# * The expected user presence state after device 2 has idled.
820
+ # * True to use workers, False a monolith.
820
821
[
821
- # If both devices have the same state, nothing exciting should happen.
822
- (
823
- PresenceState .ONLINE ,
824
- PresenceState .ONLINE ,
825
- PresenceState .ONLINE ,
826
- PresenceState .ONLINE ,
827
- PresenceState .UNAVAILABLE ,
828
- ),
829
- (
830
- PresenceState .UNAVAILABLE ,
831
- PresenceState .UNAVAILABLE ,
832
- PresenceState .UNAVAILABLE ,
833
- PresenceState .UNAVAILABLE ,
834
- PresenceState .UNAVAILABLE ,
835
- ),
836
- (
837
- PresenceState .OFFLINE ,
838
- PresenceState .OFFLINE ,
839
- PresenceState .OFFLINE ,
840
- PresenceState .OFFLINE ,
841
- PresenceState .OFFLINE ,
842
- ),
843
- # If the second device has a "lower" state it should fallback to it.
844
- (
845
- PresenceState .ONLINE ,
846
- PresenceState .UNAVAILABLE ,
847
- PresenceState .ONLINE ,
848
- PresenceState .UNAVAILABLE ,
849
- PresenceState .UNAVAILABLE ,
850
- ),
851
- (
852
- PresenceState .ONLINE ,
853
- PresenceState .OFFLINE ,
854
- PresenceState .ONLINE ,
855
- PresenceState .UNAVAILABLE ,
856
- PresenceState .UNAVAILABLE ,
857
- ),
858
- (
859
- PresenceState .UNAVAILABLE ,
860
- PresenceState .OFFLINE ,
861
- PresenceState .UNAVAILABLE ,
862
- PresenceState .UNAVAILABLE ,
863
- PresenceState .UNAVAILABLE ,
864
- ),
865
- # If the second device has a "higher" state it should override.
866
- (
867
- PresenceState .UNAVAILABLE ,
868
- PresenceState .ONLINE ,
869
- PresenceState .ONLINE ,
870
- PresenceState .ONLINE ,
871
- PresenceState .UNAVAILABLE ,
872
- ),
873
- (
874
- PresenceState .OFFLINE ,
875
- PresenceState .ONLINE ,
876
- PresenceState .ONLINE ,
877
- PresenceState .ONLINE ,
878
- PresenceState .UNAVAILABLE ,
879
- ),
880
- (
881
- PresenceState .OFFLINE ,
882
- PresenceState .UNAVAILABLE ,
883
- PresenceState .UNAVAILABLE ,
884
- PresenceState .UNAVAILABLE ,
885
- PresenceState .UNAVAILABLE ,
886
- ),
887
- ]
822
+ (* cases , workers )
823
+ for workers in (False , True )
824
+ for cases in [
825
+ # If both devices have the same state, nothing exciting should happen.
826
+ (
827
+ PresenceState .ONLINE ,
828
+ PresenceState .ONLINE ,
829
+ PresenceState .ONLINE ,
830
+ PresenceState .ONLINE ,
831
+ PresenceState .UNAVAILABLE ,
832
+ ),
833
+ (
834
+ PresenceState .UNAVAILABLE ,
835
+ PresenceState .UNAVAILABLE ,
836
+ PresenceState .UNAVAILABLE ,
837
+ PresenceState .UNAVAILABLE ,
838
+ PresenceState .UNAVAILABLE ,
839
+ ),
840
+ (
841
+ PresenceState .OFFLINE ,
842
+ PresenceState .OFFLINE ,
843
+ PresenceState .OFFLINE ,
844
+ PresenceState .OFFLINE ,
845
+ PresenceState .OFFLINE ,
846
+ ),
847
+ # If the second device has a "lower" state it should fallback to it.
848
+ (
849
+ PresenceState .ONLINE ,
850
+ PresenceState .UNAVAILABLE ,
851
+ PresenceState .ONLINE ,
852
+ PresenceState .UNAVAILABLE ,
853
+ PresenceState .UNAVAILABLE ,
854
+ ),
855
+ (
856
+ PresenceState .ONLINE ,
857
+ PresenceState .OFFLINE ,
858
+ PresenceState .ONLINE ,
859
+ PresenceState .UNAVAILABLE ,
860
+ PresenceState .UNAVAILABLE ,
861
+ ),
862
+ (
863
+ PresenceState .UNAVAILABLE ,
864
+ PresenceState .OFFLINE ,
865
+ PresenceState .UNAVAILABLE ,
866
+ PresenceState .UNAVAILABLE ,
867
+ PresenceState .UNAVAILABLE ,
868
+ ),
869
+ # If the second device has a "higher" state it should override.
870
+ (
871
+ PresenceState .UNAVAILABLE ,
872
+ PresenceState .ONLINE ,
873
+ PresenceState .ONLINE ,
874
+ PresenceState .ONLINE ,
875
+ PresenceState .UNAVAILABLE ,
876
+ ),
877
+ (
878
+ PresenceState .OFFLINE ,
879
+ PresenceState .ONLINE ,
880
+ PresenceState .ONLINE ,
881
+ PresenceState .ONLINE ,
882
+ PresenceState .UNAVAILABLE ,
883
+ ),
884
+ (
885
+ PresenceState .OFFLINE ,
886
+ PresenceState .UNAVAILABLE ,
887
+ PresenceState .UNAVAILABLE ,
888
+ PresenceState .UNAVAILABLE ,
889
+ PresenceState .UNAVAILABLE ,
890
+ ),
891
+ ]
892
+ ],
893
+ name_func = lambda testcase_func , param_num , params : f"{ testcase_func .__name__ } _{ param_num } _{ 'workers' if params .args [5 ] else 'monolith' } " ,
888
894
)
889
895
def test_set_presence_from_syncing_multi_device (
890
896
self ,
@@ -893,6 +899,7 @@ def test_set_presence_from_syncing_multi_device(
893
899
expected_state_1 : str ,
894
900
expected_state_2 : str ,
895
901
expected_state_3 : str ,
902
+ test_with_workers : bool ,
896
903
) -> None :
897
904
"""
898
905
Test the behaviour of multiple devices syncing at the same time.
@@ -907,14 +914,26 @@ def test_set_presence_from_syncing_multi_device(
907
914
"""
908
915
user_id = f"@test:{ self .hs .config .server .server_name } "
909
916
917
+ # By default, we call /sync against the main process.
918
+ worker_presence_handler = self .presence_handler
919
+ if test_with_workers :
920
+ # Create a worker and use it to handle /sync traffic instead.
921
+ # This is used to test that presence changes get replicated from workers
922
+ # to the main process correctly.
923
+ worker_to_sync_against = self .make_worker_hs (
924
+ "synapse.app.generic_worker" , {"worker_name" : "presence_writer" }
925
+ )
926
+ worker_presence_handler = worker_to_sync_against .get_presence_handler ()
927
+
910
928
# 1. Sync with the first device.
911
929
self .get_success (
912
- self . presence_handler .user_syncing (
930
+ worker_presence_handler .user_syncing (
913
931
user_id ,
914
932
"dev-1" ,
915
933
affect_presence = dev_1_state != PresenceState .OFFLINE ,
916
934
presence_state = dev_1_state ,
917
- )
935
+ ),
936
+ by = 0.01
918
937
)
919
938
920
939
# 2. Wait half the idle timer.
@@ -923,12 +942,13 @@ def test_set_presence_from_syncing_multi_device(
923
942
924
943
# 3. Sync with the second device.
925
944
self .get_success (
926
- self . presence_handler .user_syncing (
945
+ worker_presence_handler .user_syncing (
927
946
user_id ,
928
947
"dev-2" ,
929
948
affect_presence = dev_2_state != PresenceState .OFFLINE ,
930
949
presence_state = dev_2_state ,
931
- )
950
+ ),
951
+ by = 0.01
932
952
)
933
953
934
954
# 4. Assert the expected presence state.
@@ -939,8 +959,8 @@ def test_set_presence_from_syncing_multi_device(
939
959
940
960
# 5. Advance such that the first device should be discarded (the idle timer),
941
961
# then pump so _handle_timeouts function to called.
942
- self .reactor .advance (IDLE_TIMER / 1000 )
943
- self .reactor .pump ([0.1 ])
962
+ self .reactor .advance (IDLE_TIMER / 1000 / 2 )
963
+ self .reactor .pump ([0.01 ])
944
964
945
965
# 6. Assert the expected presence state.
946
966
state = self .get_success (
@@ -968,72 +988,79 @@ def test_set_presence_from_syncing_multi_device(
968
988
# * The presence state of device 2.
969
989
# * The expected user presence state after both devices have synced.
970
990
# * The expected user presence state after device 1 has stopped syncing.
991
+ # * True to use workers, False a monolith.
971
992
[
972
- # If both devices have the same state, nothing exciting should happen.
973
- (
974
- PresenceState .ONLINE ,
975
- PresenceState .ONLINE ,
976
- PresenceState .ONLINE ,
977
- PresenceState .ONLINE ,
978
- ),
979
- (
980
- PresenceState .UNAVAILABLE ,
981
- PresenceState .UNAVAILABLE ,
982
- PresenceState .UNAVAILABLE ,
983
- PresenceState .UNAVAILABLE ,
984
- ),
985
- (
986
- PresenceState .OFFLINE ,
987
- PresenceState .OFFLINE ,
988
- PresenceState .OFFLINE ,
989
- PresenceState .OFFLINE ,
990
- ),
991
- # If the second device has a "lower" state it should fallback to it.
992
- (
993
- PresenceState .ONLINE ,
994
- PresenceState .UNAVAILABLE ,
995
- PresenceState .ONLINE ,
996
- PresenceState .UNAVAILABLE ,
997
- ),
998
- (
999
- PresenceState .ONLINE ,
1000
- PresenceState .OFFLINE ,
1001
- PresenceState .ONLINE ,
1002
- PresenceState .OFFLINE ,
1003
- ),
1004
- (
1005
- PresenceState .UNAVAILABLE ,
1006
- PresenceState .OFFLINE ,
1007
- PresenceState .UNAVAILABLE ,
1008
- PresenceState .OFFLINE ,
1009
- ),
1010
- # If the second device has a "higher" state it should override.
1011
- (
1012
- PresenceState .UNAVAILABLE ,
1013
- PresenceState .ONLINE ,
1014
- PresenceState .ONLINE ,
1015
- PresenceState .ONLINE ,
1016
- ),
1017
- (
1018
- PresenceState .OFFLINE ,
1019
- PresenceState .ONLINE ,
1020
- PresenceState .ONLINE ,
1021
- PresenceState .ONLINE ,
1022
- ),
1023
- (
1024
- PresenceState .OFFLINE ,
1025
- PresenceState .UNAVAILABLE ,
1026
- PresenceState .UNAVAILABLE ,
1027
- PresenceState .UNAVAILABLE ,
1028
- ),
1029
- ]
993
+ (* cases , workers )
994
+ for workers in (False , True )
995
+ for cases in [
996
+ # If both devices have the same state, nothing exciting should happen.
997
+ (
998
+ PresenceState .ONLINE ,
999
+ PresenceState .ONLINE ,
1000
+ PresenceState .ONLINE ,
1001
+ PresenceState .ONLINE ,
1002
+ ),
1003
+ (
1004
+ PresenceState .UNAVAILABLE ,
1005
+ PresenceState .UNAVAILABLE ,
1006
+ PresenceState .UNAVAILABLE ,
1007
+ PresenceState .UNAVAILABLE ,
1008
+ ),
1009
+ (
1010
+ PresenceState .OFFLINE ,
1011
+ PresenceState .OFFLINE ,
1012
+ PresenceState .OFFLINE ,
1013
+ PresenceState .OFFLINE ,
1014
+ ),
1015
+ # If the second device has a "lower" state it should fallback to it.
1016
+ (
1017
+ PresenceState .ONLINE ,
1018
+ PresenceState .UNAVAILABLE ,
1019
+ PresenceState .ONLINE ,
1020
+ PresenceState .UNAVAILABLE ,
1021
+ ),
1022
+ (
1023
+ PresenceState .ONLINE ,
1024
+ PresenceState .OFFLINE ,
1025
+ PresenceState .ONLINE ,
1026
+ PresenceState .OFFLINE ,
1027
+ ),
1028
+ (
1029
+ PresenceState .UNAVAILABLE ,
1030
+ PresenceState .OFFLINE ,
1031
+ PresenceState .UNAVAILABLE ,
1032
+ PresenceState .OFFLINE ,
1033
+ ),
1034
+ # If the second device has a "higher" state it should override.
1035
+ (
1036
+ PresenceState .UNAVAILABLE ,
1037
+ PresenceState .ONLINE ,
1038
+ PresenceState .ONLINE ,
1039
+ PresenceState .ONLINE ,
1040
+ ),
1041
+ (
1042
+ PresenceState .OFFLINE ,
1043
+ PresenceState .ONLINE ,
1044
+ PresenceState .ONLINE ,
1045
+ PresenceState .ONLINE ,
1046
+ ),
1047
+ (
1048
+ PresenceState .OFFLINE ,
1049
+ PresenceState .UNAVAILABLE ,
1050
+ PresenceState .UNAVAILABLE ,
1051
+ PresenceState .UNAVAILABLE ,
1052
+ ),
1053
+ ]
1054
+ ],
1055
+ name_func = lambda testcase_func , param_num , params : f"{ testcase_func .__name__ } _{ param_num } _{ 'workers' if params .args [4 ] else 'monolith' } " ,
1030
1056
)
1031
1057
def test_set_presence_from_non_syncing_multi_device (
1032
1058
self ,
1033
1059
dev_1_state : str ,
1034
1060
dev_2_state : str ,
1035
1061
expected_state_1 : str ,
1036
1062
expected_state_2 : str ,
1063
+ test_with_workers : bool ,
1037
1064
) -> None :
1038
1065
"""
1039
1066
Test the behaviour of multiple devices syncing at the same time.
@@ -1048,24 +1075,37 @@ def test_set_presence_from_non_syncing_multi_device(
1048
1075
"""
1049
1076
user_id = f"@test:{ self .hs .config .server .server_name } "
1050
1077
1078
+ # By default, we call /sync against the main process.
1079
+ worker_presence_handler = self .presence_handler
1080
+ if test_with_workers :
1081
+ # Create a worker and use it to handle /sync traffic instead.
1082
+ # This is used to test that presence changes get replicated from workers
1083
+ # to the main process correctly.
1084
+ worker_to_sync_against = self .make_worker_hs (
1085
+ "synapse.app.generic_worker" , {"worker_name" : "presence_writer" }
1086
+ )
1087
+ worker_presence_handler = worker_to_sync_against .get_presence_handler ()
1088
+
1051
1089
# 1. Sync with the first device.
1052
1090
sync_1 = self .get_success (
1053
- self . presence_handler .user_syncing (
1091
+ worker_presence_handler .user_syncing (
1054
1092
user_id ,
1055
1093
"dev-1" ,
1056
1094
affect_presence = dev_1_state != PresenceState .OFFLINE ,
1057
1095
presence_state = dev_1_state ,
1058
- )
1096
+ ),
1097
+ by = 0.1
1059
1098
)
1060
1099
1061
1100
# 2. Sync with the second device.
1062
1101
sync_2 = self .get_success (
1063
- self . presence_handler .user_syncing (
1102
+ worker_presence_handler .user_syncing (
1064
1103
user_id ,
1065
1104
"dev-2" ,
1066
1105
affect_presence = dev_2_state != PresenceState .OFFLINE ,
1067
1106
presence_state = dev_2_state ,
1068
- )
1107
+ ),
1108
+ by = 0.1
1069
1109
)
1070
1110
1071
1111
# 3. Assert the expected presence state.
0 commit comments