Add tests
This commit is contained in:
parent
0ace9f8d85
commit
dccddf15b0
4
setup.py
4
setup.py
|
@ -118,7 +118,9 @@ CONDITIONAL_REQUIREMENTS["mypy"] = [
|
|||
# Tests assume that all optional dependencies are installed.
|
||||
#
|
||||
# parameterized_class decorator was introduced in parameterized 0.7.0
|
||||
CONDITIONAL_REQUIREMENTS["test"] = ["parameterized>=0.7.0"]
|
||||
#
|
||||
# We use `mock` library as that backports `AsyncMock` to Python 3.6
|
||||
CONDITIONAL_REQUIREMENTS["test"] = ["parameterized>=0.7.0", "mock>=4.0.0"]
|
||||
|
||||
CONDITIONAL_REQUIREMENTS["dev"] = (
|
||||
CONDITIONAL_REQUIREMENTS["lint"]
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
from unittest.mock import Mock
|
||||
from mock import AsyncMock, Mock
|
||||
|
||||
from synapse.storage.background_updates import BackgroundUpdater
|
||||
from twisted.internet.defer import Deferred, ensureDeferred
|
||||
|
||||
from synapse.storage.background_updates import (
|
||||
BackgroundUpdateController,
|
||||
BackgroundUpdater,
|
||||
)
|
||||
|
||||
from tests import unittest
|
||||
|
||||
|
@ -79,3 +84,71 @@ class BackgroundUpdateTestCase(unittest.HomeserverTestCase):
|
|||
result = self.get_success(self.updates.do_next_background_update(False))
|
||||
self.assertTrue(result)
|
||||
self.assertFalse(self.update_handler.called)
|
||||
|
||||
|
||||
class BackgroundUpdateControllerTestCase(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())
|
||||
)
|
||||
|
||||
self.update_deferred = Deferred()
|
||||
self.update_handler = Mock(return_value=self.update_deferred)
|
||||
self.updates.register_background_update_handler(
|
||||
"test_update", self.update_handler
|
||||
)
|
||||
|
||||
self._controller_ctx_mgr = AsyncMock(name="_controller_ctx_mgr")
|
||||
self._controller = AsyncMock(BackgroundUpdateController)
|
||||
self._controller.run_update.return_value = self._controller_ctx_mgr
|
||||
|
||||
self.updates.register_update_controller(self._controller)
|
||||
|
||||
def test_controller(self):
|
||||
store = self.hs.get_datastore()
|
||||
self.get_success(
|
||||
store.db_pool.simple_insert(
|
||||
"background_updates",
|
||||
values={"update_name": "test_update", "progress_json": "{}"},
|
||||
)
|
||||
)
|
||||
|
||||
default_batch_size = 100
|
||||
|
||||
# Set up the return values of the controller.
|
||||
enter_defer = Deferred()
|
||||
self._controller_ctx_mgr.__aenter__ = Mock(return_value=enter_defer)
|
||||
self._controller.default_batch_size.return_value = default_batch_size
|
||||
self._controller.min_batch_size.return_value = default_batch_size
|
||||
|
||||
# Start the background update.
|
||||
do_update_d = ensureDeferred(self.updates.do_next_background_update(True))
|
||||
|
||||
self.pump()
|
||||
|
||||
# `run_update` should have been called, but the update handler won't be
|
||||
# called until the `enter_defer` (returned by `__aenter__`) is resolved.
|
||||
self._controller.run_update.assert_called_once_with(
|
||||
update_name="test_update",
|
||||
database_name="master",
|
||||
oneshot=False,
|
||||
)
|
||||
self.assertFalse(do_update_d.called)
|
||||
self.assertFalse(self.update_deferred.called)
|
||||
|
||||
# Resolving the `enter_defer` should call the update handler, which then
|
||||
# blocks.
|
||||
enter_defer.callback(100)
|
||||
self.pump()
|
||||
self.update_handler.assert_called_once_with({}, default_batch_size)
|
||||
self.assertFalse(self.update_deferred.called)
|
||||
self._controller_ctx_mgr.__aexit__.assert_not_awaited()
|
||||
|
||||
# Resolving the update handler deferred should cause the
|
||||
# `do_next_background_update` to finish and return
|
||||
self.update_deferred.callback(100)
|
||||
self.pump()
|
||||
self._controller_ctx_mgr.__aexit__.assert_awaited()
|
||||
self.get_success(do_update_d)
|
||||
|
|
Loading…
Reference in a new issue