This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add some tests for propagation of device list changes between local users #11972
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1a22487
Add a file for testing device lists
anoadragon453 933ef11
Improve a docstring and add some types
anoadragon453 3eafba7
Changelog
anoadragon453 5de46ac
Fix a small typo
anoadragon453 edc28b0
Remove _user_joined_room mock
anoadragon453 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add tests for device list changes between local users. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# Copyright 2022 The Matrix.org Foundation C.I.C. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from unittest.mock import Mock | ||
|
||
from synapse.rest import admin, devices, room, sync | ||
from synapse.rest.client import account, login, register | ||
|
||
from tests import unittest | ||
|
||
|
||
class DeviceListsTestCase(unittest.HomeserverTestCase): | ||
"""Tests regarding device list changes.""" | ||
|
||
servlets = [ | ||
admin.register_servlets_for_client_rest_resource, | ||
login.register_servlets, | ||
register.register_servlets, | ||
account.register_servlets, | ||
room.register_servlets, | ||
sync.register_servlets, | ||
devices.register_servlets, | ||
] | ||
|
||
def test_receiving_local_device_list_changes(self): | ||
"""Tests that a local users that share a room receive each other's device list | ||
changes. | ||
""" | ||
# Register two users | ||
test_device_id = "TESTDEVICE" | ||
alice_user_id = self.register_user("alice", "correcthorse") | ||
alice_access_token = self.login( | ||
alice_user_id, "correcthorse", device_id=test_device_id | ||
) | ||
|
||
bob_user_id = self.register_user("bob", "ponyponypony") | ||
bob_access_token = self.login(bob_user_id, "ponyponypony") | ||
|
||
# Create a room for them to coexist peacefully in | ||
new_room_id = self.helper.create_room_as( | ||
alice_user_id, is_public=True, tok=alice_access_token | ||
) | ||
self.assertIsNotNone(new_room_id) | ||
|
||
# Pretend that Bob joined this room on a previous homeserver startup. | ||
# | ||
# There is a quirk; if Bob has joined the room since the last homeserver | ||
# restart, then an additional mechanism is employed to ensure Bob will start | ||
# receiving device list updates for that room. | ||
# In order to test the case where Bob has previously been joined to the room, | ||
# instead of restarting Synapse, we can just stub out the notifier machinery | ||
# that is triggered when a user joins a room. | ||
self.hs.get_notifier()._user_joined_room = Mock(return_value=None) | ||
|
||
# Have Bob join the room | ||
self.helper.invite( | ||
new_room_id, alice_user_id, bob_user_id, tok=alice_access_token | ||
) | ||
self.helper.join(new_room_id, bob_user_id, tok=bob_access_token) | ||
|
||
# Now have Bob initiate an initial sync (in order to get a since token) | ||
channel = self.make_request( | ||
"GET", | ||
"/sync", | ||
access_token=bob_access_token, | ||
) | ||
self.assertEqual(channel.code, 200, channel.json_body) | ||
next_batch_token = channel.json_body["next_batch"] | ||
|
||
# ...and then an incremental sync. This should block until the sync stream is woken up, | ||
# which we hope will happen as a result of Alice updating their device list. | ||
bob_sync_channel = self.make_request( | ||
"GET", | ||
f"/sync?since={next_batch_token}&timeout=30000", | ||
access_token=bob_access_token, | ||
# Start the request, then continue on. | ||
await_result=False, | ||
) | ||
|
||
# Have alice update their device list | ||
channel = self.make_request( | ||
"PUT", | ||
f"/devices/{test_device_id}", | ||
{ | ||
"display_name": "New Device Name", | ||
}, | ||
access_token=alice_access_token, | ||
) | ||
self.assertEqual(channel.code, 200, channel.json_body) | ||
|
||
# Check that bob's incremental sync contains the updated device list. | ||
# If not, the client would only receive the device list update on the | ||
# *next* sync. | ||
bob_sync_channel.await_result() | ||
self.assertEqual(bob_sync_channel.code, 200, bob_sync_channel.json_body) | ||
|
||
changed_device_lists = bob_sync_channel.json_body.get("device_lists", {}).get( | ||
"changed", [] | ||
) | ||
self.assertIn(alice_user_id, changed_device_lists, bob_sync_channel.json_body) | ||
|
||
def test_not_receiving_local_device_list_changes(self): | ||
"""Tests a local users DO NOT receive device updates from each other if they do not | ||
share a room. | ||
""" | ||
# Register two users | ||
test_device_id = "TESTDEVICE" | ||
alice_user_id = self.register_user("alice", "correcthorse") | ||
alice_access_token = self.login( | ||
alice_user_id, "correcthorse", device_id=test_device_id | ||
) | ||
|
||
bob_user_id = self.register_user("bob", "ponyponypony") | ||
bob_access_token = self.login(bob_user_id, "ponyponypony") | ||
|
||
# These users do not share a room. They are lonely. | ||
|
||
# Have Bob initiate an initial sync (in order to get a since token) | ||
channel = self.make_request( | ||
"GET", | ||
"/sync", | ||
access_token=bob_access_token, | ||
) | ||
self.assertEqual(channel.code, 200, channel.json_body) | ||
next_batch_token = channel.json_body["next_batch"] | ||
|
||
# ...and then an incremental sync. This should block until the sync stream is woken up, | ||
# which we hope will happen as a result of Alice updating their device list. | ||
bob_sync_channel = self.make_request( | ||
"GET", | ||
f"/sync?since={next_batch_token}&timeout=1000", | ||
access_token=bob_access_token, | ||
# Start the request, then continue on. | ||
await_result=False, | ||
) | ||
|
||
# Have alice update their device list | ||
channel = self.make_request( | ||
"PUT", | ||
f"/devices/{test_device_id}", | ||
{ | ||
"display_name": "New Device Name", | ||
}, | ||
access_token=alice_access_token, | ||
) | ||
self.assertEqual(channel.code, 200, channel.json_body) | ||
|
||
# Check that bob's incremental sync does not contain the updated device list. | ||
bob_sync_channel.await_result() | ||
self.assertEqual(bob_sync_channel.code, 200, bob_sync_channel.json_body) | ||
|
||
changed_device_lists = bob_sync_channel.json_body.get("device_lists", {}).get( | ||
"changed", [] | ||
) | ||
self.assertNotIn( | ||
alice_user_id, changed_device_lists, bob_sync_channel.json_body | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fairly dubious about doing this, as it really is wrenching into the guts of the code and changing things (rather than just mocking out an interface). Why do we need this? My reading of the code is that
_user_joined_room
will only have an effect if the user is currently syncing when they joined the room?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, a user stream entry is created when a user syncs, but I fail to see where it's removed at the end of a sync. I only see streams being removed when they expire:
synapse/synapse/notifier.py
Line 701 in 5de46ac
Nevertheless, putting in a breakpoint here resulted in no trigger when running the full test, so this is indeed not necessary! Removed in edc28b0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It happens here:
synapse/synapse/notifier.py
Lines 155 to 164 in 5de46ac
(I spent a while tracking that down)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, though the only code I can see that calls this is
remove_expired_streams
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that seems odd that it doesn't get removed when the stream gets notified 🤷