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

Commit

Permalink
Add a test for update_presence (#10033)
Browse files Browse the repository at this point in the history
#9962 uncovered that we accidentally removed all but one of the presence updates that we store in the database when persisting multiple updates. This could cause users' presence state to be stale.

The bug was fixed in #10014, and this PR just adds a test that failed on the old code, and was used to initially verify the bug.

The test attempts to insert some presence into the database in a batch using `PresenceStore.update_presence`, and then simply pulls it out again.
  • Loading branch information
anoadragon453 authored May 21, 2021
1 parent c5413d0 commit 21bd230
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/10033.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed deletion of new presence stream states from database.
47 changes: 46 additions & 1 deletion tests/handlers/test_presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@
handle_timeout,
handle_update,
)
from synapse.rest import admin
from synapse.rest.client.v1 import room
from synapse.types import UserID, get_domain_from_id

from tests import unittest


class PresenceUpdateTestCase(unittest.TestCase):
class PresenceUpdateTestCase(unittest.HomeserverTestCase):
servlets = [admin.register_servlets]

def prepare(self, reactor, clock, homeserver):
self.store = homeserver.get_datastore()

def test_offline_to_online(self):
wheel_timer = Mock()
user_id = "@foo:bar"
Expand Down Expand Up @@ -292,6 +298,45 @@ def test_online_to_idle(self):
any_order=True,
)

def test_persisting_presence_updates(self):
"""Tests that the latest presence state for each user is persisted correctly"""
# Create some test users and presence states for them
presence_states = []
for i in range(5):
user_id = self.register_user(f"user_{i}", "password")

presence_state = UserPresenceState(
user_id=user_id,
state="online",
last_active_ts=1,
last_federation_update_ts=1,
last_user_sync_ts=1,
status_msg="I'm online!",
currently_active=True,
)
presence_states.append(presence_state)

# Persist these presence updates to the database
self.get_success(self.store.update_presence(presence_states))

# Check that each update is present in the database
db_presence_states = self.get_success(
self.store.get_all_presence_updates(
instance_name="master",
last_id=0,
current_id=len(presence_states) + 1,
limit=len(presence_states),
)
)

# Extract presence update user ID and state information into lists of tuples
db_presence_states = [(ps[0], ps[1]) for _, ps in db_presence_states[0]]
presence_states = [(ps.user_id, ps.state) for ps in presence_states]

# Compare what we put into the storage with what we got out.
# They should be identical.
self.assertEqual(presence_states, db_presence_states)


class PresenceTimeoutTestCase(unittest.TestCase):
def test_idle_timer(self):
Expand Down

0 comments on commit 21bd230

Please sign in to comment.