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