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

Clean-up presence tests #16158

Merged
merged 5 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/16158.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve presence tests.
130 changes: 38 additions & 92 deletions tests/handlers/test_presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@ def test_last_active(self) -> None:


class PresenceHandlerTestCase(BaseMultiWorkerStreamTestCase):
user_id = "@test:server"
user_id_obj = UserID.from_string(user_id)

def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.presence_handler = hs.get_presence_handler()
self.clock = hs.get_clock()
Expand All @@ -523,61 +526,49 @@ def test_external_process_timeout(self) -> None:
we time out their syncing users presence.
"""
process_id = "1"
user_id = "@test:server"

# Notify handler that a user is now syncing.
self.get_success(
self.presence_handler.update_external_syncs_row(
process_id, user_id, True, self.clock.time_msec()
process_id, self.user_id, True, self.clock.time_msec()
)
)

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

state = self.get_success(
self.presence_handler.get_state(UserID.from_string(user_id))
)
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
self.assertEqual(state.state, PresenceState.ONLINE)

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

state = self.get_success(
self.presence_handler.get_state(UserID.from_string(user_id))
)
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
self.assertEqual(state.state, PresenceState.OFFLINE)

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

# Mark user as online
self._set_presencestate_with_status_msg(
user_id, PresenceState.ONLINE, status_msg
)
self._set_presencestate_with_status_msg(PresenceState.ONLINE, status_msg)

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

state = self.get_success(
self.presence_handler.get_state(UserID.from_string(user_id))
)
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
self.assertEqual(state.state, PresenceState.ONLINE)
self.assertEqual(state.status_msg, status_msg)

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

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

# Mark user as online
self._set_presencestate_with_status_msg(
user_id, PresenceState.ONLINE, status_msg
)
self._set_presencestate_with_status_msg(PresenceState.ONLINE, status_msg)

# Mark user as offline
self.get_success(
self.presence_handler.set_state(
UserID.from_string(user_id), {"presence": PresenceState.OFFLINE}
self.user_id_obj, {"presence": PresenceState.OFFLINE}
)
)

state = self.get_success(
self.presence_handler.get_state(UserID.from_string(user_id))
)
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
self.assertEqual(state.state, PresenceState.OFFLINE)
self.assertEqual(state.status_msg, None)

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

# Mark user as online
self._set_presencestate_with_status_msg(
user_id, PresenceState.ONLINE, status_msg
)
self._set_presencestate_with_status_msg(PresenceState.ONLINE, status_msg)

# Mark user as offline
self._set_presencestate_with_status_msg(
user_id, PresenceState.OFFLINE, "And now here."
)
self._set_presencestate_with_status_msg(PresenceState.OFFLINE, "And now here.")

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

# Mark user as online
self._set_presencestate_with_status_msg(
user_id, PresenceState.ONLINE, status_msg
)
self._set_presencestate_with_status_msg(PresenceState.ONLINE, status_msg)

# Mark user as online again
self.get_success(
self.presence_handler.set_state(
UserID.from_string(user_id), {"presence": PresenceState.ONLINE}
self.user_id_obj, {"presence": PresenceState.ONLINE}
)
)

state = self.get_success(
self.presence_handler.get_state(UserID.from_string(user_id))
)
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
# status_msg should remain even after going offline
self.assertEqual(state.state, PresenceState.ONLINE)
self.assertEqual(state.status_msg, None)
Expand All @@ -654,84 +630,62 @@ def test_set_presence_with_status_msg_none(self) -> None:
"""Test that if a user set again the presence manually
and status is `None`, that `status_msg` is `None`.
"""
user_id = "@test:server"
status_msg = "I'm here!"

# Mark user as online
self._set_presencestate_with_status_msg(
user_id, PresenceState.ONLINE, status_msg
)
self._set_presencestate_with_status_msg(PresenceState.ONLINE, status_msg)

# Mark user as online and `status_msg = None`
self._set_presencestate_with_status_msg(user_id, PresenceState.ONLINE, None)
self._set_presencestate_with_status_msg(PresenceState.ONLINE, None)

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

self._set_presencestate_with_status_msg(
user_id, PresenceState.UNAVAILABLE, status_msg
)
self._set_presencestate_with_status_msg(PresenceState.UNAVAILABLE, status_msg)

self.get_success(
self.presence_handler.user_syncing(user_id, False, PresenceState.ONLINE)
self.presence_handler.user_syncing(
self.user_id, False, PresenceState.ONLINE
)
)

state = self.get_success(
self.presence_handler.get_state(UserID.from_string(user_id))
)
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
# we should still be unavailable
self.assertEqual(state.state, PresenceState.UNAVAILABLE)
# and status message should still be the same
self.assertEqual(state.status_msg, status_msg)

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

self._set_presencestate_with_status_msg(
user_id, PresenceState.UNAVAILABLE, status_msg
)
self._set_presencestate_with_status_msg(PresenceState.UNAVAILABLE, status_msg)

self.get_success(
self.presence_handler.user_syncing(user_id, True, PresenceState.ONLINE)
self.presence_handler.user_syncing(self.user_id, True, PresenceState.ONLINE)
)

state = self.get_success(
self.presence_handler.get_state(UserID.from_string(user_id))
)
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
# we should now be online
self.assertEqual(state.state, PresenceState.ONLINE)

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

self._set_presencestate_with_status_msg(
user_id, PresenceState.UNAVAILABLE, status_msg
)
self._set_presencestate_with_status_msg(PresenceState.UNAVAILABLE, status_msg)

self.get_success(
self.presence_handler.user_syncing(user_id, True, PresenceState.ONLINE)
self.presence_handler.user_syncing(self.user_id, True, PresenceState.ONLINE)
)

state = self.get_success(
self.presence_handler.get_state(UserID.from_string(user_id))
)
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
# our status message should be the same as it was before
self.assertEqual(state.status_msg, status_msg)

@parameterized.expand([(False,), (True,)])
@unittest.override_config(
{
"experimental_features": {
"msc3026_enabled": True,
},
}
)
@unittest.override_config({"experimental_features": {"msc3026_enabled": True}})
def test_set_presence_from_syncing_keeps_busy(
self, test_with_workers: bool
) -> None:
Expand All @@ -741,7 +695,6 @@ def test_set_presence_from_syncing_keeps_busy(
test_with_workers: If True, check the presence state of the user by calling
/sync against a worker, rather than the main process.
"""
user_id = "@test:server"
status_msg = "I'm busy!"

# By default, we call /sync against the main process.
Expand All @@ -755,44 +708,40 @@ def test_set_presence_from_syncing_keeps_busy(
)

# Set presence to BUSY
self._set_presencestate_with_status_msg(user_id, PresenceState.BUSY, status_msg)
self._set_presencestate_with_status_msg(PresenceState.BUSY, status_msg)

# Perform a sync with a presence state other than busy. This should NOT change
# our presence status; we only change from busy if we explicitly set it via
# /presence/*.
self.get_success(
worker_to_sync_against.get_presence_handler().user_syncing(
user_id, True, PresenceState.ONLINE
self.user_id, True, PresenceState.ONLINE
)
)

# Check against the main process that the user's presence did not change.
state = self.get_success(
self.presence_handler.get_state(UserID.from_string(user_id))
)
state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
# we should still be busy
self.assertEqual(state.state, PresenceState.BUSY)

def _set_presencestate_with_status_msg(
self, user_id: str, state: str, status_msg: Optional[str]
self, state: str, status_msg: Optional[str]
) -> None:
"""Set a PresenceState and status_msg and check the result.

Args:
user_id: User for that the status is to be set.
self.user_id: User for that the status is to be set.
clokep marked this conversation as resolved.
Show resolved Hide resolved
state: The new PresenceState.
status_msg: Status message that is to be set.
"""
self.get_success(
self.presence_handler.set_state(
UserID.from_string(user_id),
self.user_id_obj,
{"presence": state, "status_msg": status_msg},
)
)

new_state = self.get_success(
self.presence_handler.get_state(UserID.from_string(user_id))
)
new_state = self.get_success(self.presence_handler.get_state(self.user_id_obj))
self.assertEqual(new_state.state, state)
self.assertEqual(new_state.status_msg, status_msg)

Expand Down Expand Up @@ -952,9 +901,6 @@ def test_partially_clear_queue(self) -> None:
self.assertEqual(upto_token, now_token)
self.assertFalse(limited)

expected_rows = [
(2, ("dest3", "@user3:test")),
]
self.assertCountEqual(rows, [])

prev_token = self.queue.get_current_token(self.instance_name)
Expand Down
Loading