forked from MirrorHub/synapse
port BackgroundUpdateTestCase to HomeserverTestCase (#6653)
This commit is contained in:
parent
1ff5491117
commit
d20c346544
2 changed files with 38 additions and 35 deletions
1
changelog.d/6653.misc
Normal file
1
changelog.d/6653.misc
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Port core background update routines to async/await.
|
|
@ -2,44 +2,37 @@ from mock import Mock
|
||||||
|
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
|
||||||
|
from synapse.storage.background_updates import BackgroundUpdater
|
||||||
|
|
||||||
from tests import unittest
|
from tests import unittest
|
||||||
from tests.utils import setup_test_homeserver
|
|
||||||
|
|
||||||
|
|
||||||
class BackgroundUpdateTestCase(unittest.TestCase):
|
class BackgroundUpdateTestCase(unittest.HomeserverTestCase):
|
||||||
@defer.inlineCallbacks
|
def prepare(self, reactor, clock, homeserver):
|
||||||
def setUp(self):
|
self.updates = self.hs.get_datastore().db.updates # type: BackgroundUpdater
|
||||||
hs = yield setup_test_homeserver(self.addCleanup)
|
# the base test class should have run the real bg updates for us
|
||||||
self.store = hs.get_datastore()
|
self.assertTrue(self.updates.has_completed_background_updates())
|
||||||
self.clock = hs.get_clock()
|
|
||||||
|
|
||||||
self.update_handler = Mock()
|
self.update_handler = Mock()
|
||||||
|
self.updates.register_background_update_handler(
|
||||||
yield self.store.db.updates.register_background_update_handler(
|
|
||||||
"test_update", self.update_handler
|
"test_update", self.update_handler
|
||||||
)
|
)
|
||||||
|
|
||||||
# run the real background updates, to get them out the way
|
|
||||||
# (perhaps we should run them as part of the test HS setup, since we
|
|
||||||
# run all of the other schema setup stuff there?)
|
|
||||||
while True:
|
|
||||||
res = yield self.store.db.updates.do_next_background_update(1000)
|
|
||||||
if res is None:
|
|
||||||
break
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
|
||||||
def test_do_background_update(self):
|
def test_do_background_update(self):
|
||||||
desired_count = 1000
|
# the time we claim each update takes
|
||||||
duration_ms = 42
|
duration_ms = 42
|
||||||
|
|
||||||
|
# the target runtime for each bg update
|
||||||
|
target_background_update_duration_ms = 50000
|
||||||
|
|
||||||
# first step: make a bit of progress
|
# first step: make a bit of progress
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def update(progress, count):
|
def update(progress, count):
|
||||||
self.clock.advance_time_msec(count * duration_ms)
|
yield self.clock.sleep((count * duration_ms) / 1000)
|
||||||
progress = {"my_key": progress["my_key"] + 1}
|
progress = {"my_key": progress["my_key"] + 1}
|
||||||
yield self.store.db.runInteraction(
|
yield self.hs.get_datastore().db.runInteraction(
|
||||||
"update_progress",
|
"update_progress",
|
||||||
self.store.db.updates._background_update_progress_txn,
|
self.updates._background_update_progress_txn,
|
||||||
"test_update",
|
"test_update",
|
||||||
progress,
|
progress,
|
||||||
)
|
)
|
||||||
|
@ -47,37 +40,46 @@ class BackgroundUpdateTestCase(unittest.TestCase):
|
||||||
|
|
||||||
self.update_handler.side_effect = update
|
self.update_handler.side_effect = update
|
||||||
|
|
||||||
yield self.store.db.updates.start_background_update(
|
self.get_success(
|
||||||
"test_update", {"my_key": 1}
|
self.updates.start_background_update("test_update", {"my_key": 1})
|
||||||
)
|
)
|
||||||
|
|
||||||
self.update_handler.reset_mock()
|
self.update_handler.reset_mock()
|
||||||
result = yield self.store.db.updates.do_next_background_update(
|
res = self.get_success(
|
||||||
duration_ms * desired_count
|
self.updates.do_next_background_update(
|
||||||
|
target_background_update_duration_ms
|
||||||
|
),
|
||||||
|
by=0.1,
|
||||||
)
|
)
|
||||||
self.assertIsNotNone(result)
|
self.assertIsNotNone(res)
|
||||||
|
|
||||||
|
# on the first call, we should get run with the default background update size
|
||||||
self.update_handler.assert_called_once_with(
|
self.update_handler.assert_called_once_with(
|
||||||
{"my_key": 1}, self.store.db.updates.DEFAULT_BACKGROUND_BATCH_SIZE
|
{"my_key": 1}, self.updates.DEFAULT_BACKGROUND_BATCH_SIZE
|
||||||
)
|
)
|
||||||
|
|
||||||
# second step: complete the update
|
# second step: complete the update
|
||||||
|
# we should now get run with a much bigger number of items to update
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def update(progress, count):
|
def update(progress, count):
|
||||||
yield self.store.db.updates._end_background_update("test_update")
|
self.assertEqual(progress, {"my_key": 2})
|
||||||
|
self.assertAlmostEqual(
|
||||||
|
count, target_background_update_duration_ms / duration_ms, places=0,
|
||||||
|
)
|
||||||
|
yield self.updates._end_background_update("test_update")
|
||||||
return count
|
return count
|
||||||
|
|
||||||
self.update_handler.side_effect = update
|
self.update_handler.side_effect = update
|
||||||
self.update_handler.reset_mock()
|
self.update_handler.reset_mock()
|
||||||
result = yield self.store.db.updates.do_next_background_update(
|
result = self.get_success(
|
||||||
duration_ms * desired_count
|
self.updates.do_next_background_update(target_background_update_duration_ms)
|
||||||
)
|
)
|
||||||
self.assertIsNotNone(result)
|
self.assertIsNotNone(result)
|
||||||
self.update_handler.assert_called_once_with({"my_key": 2}, desired_count)
|
self.update_handler.assert_called_once()
|
||||||
|
|
||||||
# third step: we don't expect to be called any more
|
# third step: we don't expect to be called any more
|
||||||
self.update_handler.reset_mock()
|
self.update_handler.reset_mock()
|
||||||
result = yield self.store.db.updates.do_next_background_update(
|
result = self.get_success(
|
||||||
duration_ms * desired_count
|
self.updates.do_next_background_update(target_background_update_duration_ms)
|
||||||
)
|
)
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
self.assertFalse(self.update_handler.called)
|
self.assertFalse(self.update_handler.called)
|
||||||
|
|
Loading…
Reference in a new issue