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

Commit 6567f8c

Browse files
committed
Run tests on workers.
1 parent 34f6011 commit 6567f8c

File tree

1 file changed

+175
-135
lines changed

1 file changed

+175
-135
lines changed

tests/handlers/test_presence.py

Lines changed: 175 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -817,74 +817,80 @@ def test_set_presence_from_syncing_is_set(self) -> None:
817817
# * The expected user presence state after both devices have synced.
818818
# * The expected user presence state after device 1 has idled.
819819
# * The expected user presence state after device 2 has idled.
820+
# * True to use workers, False a monolith.
820821
[
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'}",
888894
)
889895
def test_set_presence_from_syncing_multi_device(
890896
self,
@@ -893,6 +899,7 @@ def test_set_presence_from_syncing_multi_device(
893899
expected_state_1: str,
894900
expected_state_2: str,
895901
expected_state_3: str,
902+
test_with_workers: bool,
896903
) -> None:
897904
"""
898905
Test the behaviour of multiple devices syncing at the same time.
@@ -907,14 +914,26 @@ def test_set_presence_from_syncing_multi_device(
907914
"""
908915
user_id = f"@test:{self.hs.config.server.server_name}"
909916

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+
910928
# 1. Sync with the first device.
911929
self.get_success(
912-
self.presence_handler.user_syncing(
930+
worker_presence_handler.user_syncing(
913931
user_id,
914932
"dev-1",
915933
affect_presence=dev_1_state != PresenceState.OFFLINE,
916934
presence_state=dev_1_state,
917-
)
935+
),
936+
by=0.01
918937
)
919938

920939
# 2. Wait half the idle timer.
@@ -923,12 +942,13 @@ def test_set_presence_from_syncing_multi_device(
923942

924943
# 3. Sync with the second device.
925944
self.get_success(
926-
self.presence_handler.user_syncing(
945+
worker_presence_handler.user_syncing(
927946
user_id,
928947
"dev-2",
929948
affect_presence=dev_2_state != PresenceState.OFFLINE,
930949
presence_state=dev_2_state,
931-
)
950+
),
951+
by=0.01
932952
)
933953

934954
# 4. Assert the expected presence state.
@@ -939,8 +959,8 @@ def test_set_presence_from_syncing_multi_device(
939959

940960
# 5. Advance such that the first device should be discarded (the idle timer),
941961
# 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])
944964

945965
# 6. Assert the expected presence state.
946966
state = self.get_success(
@@ -968,72 +988,79 @@ def test_set_presence_from_syncing_multi_device(
968988
# * The presence state of device 2.
969989
# * The expected user presence state after both devices have synced.
970990
# * The expected user presence state after device 1 has stopped syncing.
991+
# * True to use workers, False a monolith.
971992
[
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'}",
10301056
)
10311057
def test_set_presence_from_non_syncing_multi_device(
10321058
self,
10331059
dev_1_state: str,
10341060
dev_2_state: str,
10351061
expected_state_1: str,
10361062
expected_state_2: str,
1063+
test_with_workers: bool,
10371064
) -> None:
10381065
"""
10391066
Test the behaviour of multiple devices syncing at the same time.
@@ -1048,24 +1075,37 @@ def test_set_presence_from_non_syncing_multi_device(
10481075
"""
10491076
user_id = f"@test:{self.hs.config.server.server_name}"
10501077

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+
10511089
# 1. Sync with the first device.
10521090
sync_1 = self.get_success(
1053-
self.presence_handler.user_syncing(
1091+
worker_presence_handler.user_syncing(
10541092
user_id,
10551093
"dev-1",
10561094
affect_presence=dev_1_state != PresenceState.OFFLINE,
10571095
presence_state=dev_1_state,
1058-
)
1096+
),
1097+
by=0.1
10591098
)
10601099

10611100
# 2. Sync with the second device.
10621101
sync_2 = self.get_success(
1063-
self.presence_handler.user_syncing(
1102+
worker_presence_handler.user_syncing(
10641103
user_id,
10651104
"dev-2",
10661105
affect_presence=dev_2_state != PresenceState.OFFLINE,
10671106
presence_state=dev_2_state,
1068-
)
1107+
),
1108+
by=0.1
10691109
)
10701110

10711111
# 3. Assert the expected presence state.

0 commit comments

Comments
 (0)