2018-07-09 09:09:20 +03:00
|
|
|
from mock import Mock
|
|
|
|
|
2015-11-10 18:51:40 +03:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2018-07-09 09:09:20 +03:00
|
|
|
from tests import unittest
|
2015-11-10 18:51:40 +03:00
|
|
|
from tests.utils import setup_test_homeserver
|
|
|
|
|
2016-02-19 18:34:38 +03:00
|
|
|
|
2015-11-10 18:51:40 +03:00
|
|
|
class BackgroundUpdateTestCase(unittest.TestCase):
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp(self):
|
2019-05-10 08:12:11 +03:00
|
|
|
hs = yield setup_test_homeserver(self.addCleanup)
|
2015-11-10 18:51:40 +03:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
|
|
|
|
self.update_handler = Mock()
|
|
|
|
|
|
|
|
yield self.store.register_background_update_handler(
|
|
|
|
"test_update", self.update_handler
|
|
|
|
)
|
|
|
|
|
2016-07-25 14:10:42 +03:00
|
|
|
# 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.do_next_background_update(1000)
|
|
|
|
if res is None:
|
|
|
|
break
|
|
|
|
|
2015-11-10 18:51:40 +03:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_do_background_update(self):
|
2016-02-19 18:34:38 +03:00
|
|
|
desired_count = 1000
|
|
|
|
duration_ms = 42
|
2015-11-10 18:51:40 +03:00
|
|
|
|
2016-07-25 14:10:42 +03:00
|
|
|
# first step: make a bit of progress
|
2015-11-10 18:51:40 +03:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def update(progress, count):
|
|
|
|
self.clock.advance_time_msec(count * duration_ms)
|
|
|
|
progress = {"my_key": progress["my_key"] + 1}
|
|
|
|
yield self.store.runInteraction(
|
|
|
|
"update_progress",
|
|
|
|
self.store._background_update_progress_txn,
|
|
|
|
"test_update",
|
|
|
|
progress,
|
|
|
|
)
|
2019-07-23 16:00:55 +03:00
|
|
|
return count
|
2015-11-10 18:51:40 +03:00
|
|
|
|
|
|
|
self.update_handler.side_effect = update
|
|
|
|
|
|
|
|
yield self.store.start_background_update("test_update", {"my_key": 1})
|
|
|
|
|
|
|
|
self.update_handler.reset_mock()
|
2018-08-10 16:54:09 +03:00
|
|
|
result = yield self.store.do_next_background_update(duration_ms * desired_count)
|
2015-11-10 18:51:40 +03:00
|
|
|
self.assertIsNotNone(result)
|
|
|
|
self.update_handler.assert_called_once_with(
|
|
|
|
{"my_key": 1}, self.store.DEFAULT_BACKGROUND_BATCH_SIZE
|
|
|
|
)
|
|
|
|
|
2016-07-25 14:10:42 +03:00
|
|
|
# second step: complete the update
|
2015-11-10 18:51:40 +03:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def update(progress, count):
|
|
|
|
yield self.store._end_background_update("test_update")
|
2019-07-23 16:00:55 +03:00
|
|
|
return count
|
2015-11-10 18:51:40 +03:00
|
|
|
|
|
|
|
self.update_handler.side_effect = update
|
|
|
|
self.update_handler.reset_mock()
|
2018-08-10 16:54:09 +03:00
|
|
|
result = yield self.store.do_next_background_update(duration_ms * desired_count)
|
2015-11-10 18:51:40 +03:00
|
|
|
self.assertIsNotNone(result)
|
2018-08-10 16:54:09 +03:00
|
|
|
self.update_handler.assert_called_once_with({"my_key": 2}, desired_count)
|
2015-11-10 18:51:40 +03:00
|
|
|
|
2016-07-25 14:10:42 +03:00
|
|
|
# third step: we don't expect to be called any more
|
2015-11-10 18:51:40 +03:00
|
|
|
self.update_handler.reset_mock()
|
2018-08-10 16:54:09 +03:00
|
|
|
result = yield self.store.do_next_background_update(duration_ms * desired_count)
|
2015-11-10 18:51:40 +03:00
|
|
|
self.assertIsNone(result)
|
|
|
|
self.assertFalse(self.update_handler.called)
|