0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-12-25 10:24:32 +01:00
synapse/tests/storage/test_background_update.py

90 lines
3.1 KiB
Python
Raw Normal View History

2021-04-09 19:44:38 +02:00
from unittest.mock import Mock
2018-07-09 08:09:20 +02:00
from synapse.storage.background_updates import BackgroundUpdater
2018-07-09 08:09:20 +02:00
from tests import unittest
2015-11-10 16:51:40 +01:00
2016-02-19 16:34:38 +01:00
class BackgroundUpdateTestCase(unittest.HomeserverTestCase):
def prepare(self, reactor, clock, homeserver):
self.updates: BackgroundUpdater = self.hs.get_datastore().db_pool.updates
# the base test class should have run the real bg updates for us
self.assertTrue(
self.get_success(self.updates.has_completed_background_updates())
)
2015-11-10 16:51:40 +01:00
self.update_handler = Mock()
self.updates.register_background_update_handler(
2015-11-10 16:51:40 +01:00
"test_update", self.update_handler
)
def test_do_background_update(self):
# the time we claim each update takes
2016-02-19 16:34:38 +01:00
duration_ms = 42
2015-11-10 16:51:40 +01:00
# the target runtime for each bg update
target_background_update_duration_ms = 50000
store = self.hs.get_datastore()
self.get_success(
store.db_pool.simple_insert(
"background_updates",
values={"update_name": "test_update", "progress_json": '{"my_key": 1}'},
)
)
# first step: make a bit of progress
async def update(progress, count):
await self.clock.sleep((count * duration_ms) / 1000)
2015-11-10 16:51:40 +01:00
progress = {"my_key": progress["my_key"] + 1}
await store.db_pool.runInteraction(
2015-11-10 16:51:40 +01:00
"update_progress",
self.updates._background_update_progress_txn,
2015-11-10 16:51:40 +01:00
"test_update",
progress,
)
return count
2015-11-10 16:51:40 +01:00
self.update_handler.side_effect = update
self.update_handler.reset_mock()
res = self.get_success(
self.updates.do_next_background_update(
target_background_update_duration_ms
),
by=0.1,
)
self.assertFalse(res)
# on the first call, we should get run with the default background update size
2015-11-10 16:51:40 +01:00
self.update_handler.assert_called_once_with(
{"my_key": 1}, self.updates.DEFAULT_BACKGROUND_BATCH_SIZE
2015-11-10 16:51:40 +01:00
)
# second step: complete the update
# we should now get run with a much bigger number of items to update
async def update(progress, count):
self.assertEqual(progress, {"my_key": 2})
self.assertAlmostEqual(
count,
target_background_update_duration_ms / duration_ms,
places=0,
)
await self.updates._end_background_update("test_update")
return count
2015-11-10 16:51:40 +01:00
self.update_handler.side_effect = update
self.update_handler.reset_mock()
result = self.get_success(
self.updates.do_next_background_update(target_background_update_duration_ms)
)
self.assertFalse(result)
self.update_handler.assert_called_once()
2015-11-10 16:51:40 +01:00
# third step: we don't expect to be called any more
2015-11-10 16:51:40 +01:00
self.update_handler.reset_mock()
result = self.get_success(
self.updates.do_next_background_update(target_background_update_duration_ms)
)
self.assertTrue(result)
2015-11-10 16:51:40 +01:00
self.assertFalse(self.update_handler.called)