Fix errors in Synapse logs from unit tests. (#10939)

Fix some harmless errors from background processes (mostly
due to awaiting Mock objects) that occurred in the Synapse
logs during unit tests.
This commit is contained in:
Patrick Cloke 2021-09-30 11:03:29 -04:00 committed by GitHub
parent 44dee1fe8c
commit 7d84d2523a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 26 deletions

1
changelog.d/10939.misc Normal file
View file

@ -0,0 +1 @@
Fix logged errors in unit tests.

View file

@ -24,7 +24,7 @@ from synapse.appservice.scheduler import (
from synapse.logging.context import make_deferred_yieldable from synapse.logging.context import make_deferred_yieldable
from tests import unittest from tests import unittest
from tests.test_utils import make_awaitable from tests.test_utils import simple_async_mock
from ..utils import MockClock from ..utils import MockClock
@ -49,11 +49,10 @@ class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
txn = Mock(id=txn_id, service=service, events=events) txn = Mock(id=txn_id, service=service, events=events)
# mock methods # mock methods
self.store.get_appservice_state = Mock( self.store.get_appservice_state = simple_async_mock(ApplicationServiceState.UP)
return_value=defer.succeed(ApplicationServiceState.UP) txn.send = simple_async_mock(True)
) txn.complete = simple_async_mock(True)
txn.send = Mock(return_value=make_awaitable(True)) self.store.create_appservice_txn = simple_async_mock(txn)
self.store.create_appservice_txn = Mock(return_value=defer.succeed(txn))
# actual call # actual call
self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events))) self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
@ -71,10 +70,10 @@ class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
events = [Mock(), Mock()] events = [Mock(), Mock()]
txn = Mock(id="idhere", service=service, events=events) txn = Mock(id="idhere", service=service, events=events)
self.store.get_appservice_state = Mock( self.store.get_appservice_state = simple_async_mock(
return_value=defer.succeed(ApplicationServiceState.DOWN) ApplicationServiceState.DOWN
) )
self.store.create_appservice_txn = Mock(return_value=defer.succeed(txn)) self.store.create_appservice_txn = simple_async_mock(txn)
# actual call # actual call
self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events))) self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
@ -94,12 +93,10 @@ class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
txn = Mock(id=txn_id, service=service, events=events) txn = Mock(id=txn_id, service=service, events=events)
# mock methods # mock methods
self.store.get_appservice_state = Mock( self.store.get_appservice_state = simple_async_mock(ApplicationServiceState.UP)
return_value=defer.succeed(ApplicationServiceState.UP) self.store.set_appservice_state = simple_async_mock(True)
) txn.send = simple_async_mock(False) # fails to send
self.store.set_appservice_state = Mock(return_value=defer.succeed(True)) self.store.create_appservice_txn = simple_async_mock(txn)
txn.send = Mock(return_value=make_awaitable(False)) # fails to send
self.store.create_appservice_txn = Mock(return_value=defer.succeed(txn))
# actual call # actual call
self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events))) self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
@ -122,7 +119,7 @@ class ApplicationServiceSchedulerRecovererTestCase(unittest.TestCase):
self.as_api = Mock() self.as_api = Mock()
self.store = Mock() self.store = Mock()
self.service = Mock() self.service = Mock()
self.callback = Mock() self.callback = simple_async_mock()
self.recoverer = _Recoverer( self.recoverer = _Recoverer(
clock=self.clock, clock=self.clock,
as_api=self.as_api, as_api=self.as_api,
@ -144,8 +141,8 @@ class ApplicationServiceSchedulerRecovererTestCase(unittest.TestCase):
self.recoverer.recover() self.recoverer.recover()
# shouldn't have called anything prior to waiting for exp backoff # shouldn't have called anything prior to waiting for exp backoff
self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count) self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
txn.send = Mock(return_value=make_awaitable(True)) txn.send = simple_async_mock(True)
txn.complete.return_value = make_awaitable(None) txn.complete = simple_async_mock(None)
# wait for exp backoff # wait for exp backoff
self.clock.advance_time(2) self.clock.advance_time(2)
self.assertEquals(1, txn.send.call_count) self.assertEquals(1, txn.send.call_count)
@ -170,8 +167,8 @@ class ApplicationServiceSchedulerRecovererTestCase(unittest.TestCase):
self.recoverer.recover() self.recoverer.recover()
self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count) self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
txn.send = Mock(return_value=make_awaitable(False)) txn.send = simple_async_mock(False)
txn.complete.return_value = make_awaitable(None) txn.complete = simple_async_mock(None)
self.clock.advance_time(2) self.clock.advance_time(2)
self.assertEquals(1, txn.send.call_count) self.assertEquals(1, txn.send.call_count)
self.assertEquals(0, txn.complete.call_count) self.assertEquals(0, txn.complete.call_count)
@ -184,7 +181,7 @@ class ApplicationServiceSchedulerRecovererTestCase(unittest.TestCase):
self.assertEquals(3, txn.send.call_count) self.assertEquals(3, txn.send.call_count)
self.assertEquals(0, txn.complete.call_count) self.assertEquals(0, txn.complete.call_count)
self.assertEquals(0, self.callback.call_count) self.assertEquals(0, self.callback.call_count)
txn.send = Mock(return_value=make_awaitable(True)) # successfully send the txn txn.send = simple_async_mock(True) # successfully send the txn
pop_txn = True # returns the txn the first time, then no more. pop_txn = True # returns the txn the first time, then no more.
self.clock.advance_time(16) self.clock.advance_time(16)
self.assertEquals(1, txn.send.call_count) # new mock reset call count self.assertEquals(1, txn.send.call_count) # new mock reset call count
@ -195,6 +192,7 @@ class ApplicationServiceSchedulerRecovererTestCase(unittest.TestCase):
class ApplicationServiceSchedulerQueuerTestCase(unittest.TestCase): class ApplicationServiceSchedulerQueuerTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.txn_ctrl = Mock() self.txn_ctrl = Mock()
self.txn_ctrl.send = simple_async_mock()
self.queuer = _ServiceQueuer(self.txn_ctrl, MockClock()) self.queuer = _ServiceQueuer(self.txn_ctrl, MockClock())
def test_send_single_event_no_queue(self): def test_send_single_event_no_queue(self):

View file

@ -26,6 +26,7 @@ from synapse.rest.client import login, presence, room
from synapse.types import JsonDict, StreamToken, create_requester from synapse.types import JsonDict, StreamToken, create_requester
from tests.handlers.test_sync import generate_sync_config from tests.handlers.test_sync import generate_sync_config
from tests.test_utils import simple_async_mock
from tests.unittest import FederatingHomeserverTestCase, TestCase, override_config from tests.unittest import FederatingHomeserverTestCase, TestCase, override_config
@ -133,8 +134,12 @@ class PresenceRouterTestCase(FederatingHomeserverTestCase):
] ]
def make_homeserver(self, reactor, clock): def make_homeserver(self, reactor, clock):
# Mock out the calls over federation.
fed_transport_client = Mock(spec=["send_transaction"])
fed_transport_client.send_transaction = simple_async_mock({})
hs = self.setup_test_homeserver( hs = self.setup_test_homeserver(
federation_transport_client=Mock(spec=["send_transaction"]), federation_transport_client=fed_transport_client,
) )
# Load the modules into the homeserver # Load the modules into the homeserver
module_api = hs.get_module_api() module_api = hs.get_module_api()

View file

@ -336,7 +336,7 @@ class FederationSenderDevicesTestCases(HomeserverTestCase):
recovery recovery
""" """
mock_send_txn = self.hs.get_federation_transport_client().send_transaction mock_send_txn = self.hs.get_federation_transport_client().send_transaction
mock_send_txn.side_effect = lambda t, cb: defer.fail("fail") mock_send_txn.side_effect = lambda t, cb: defer.fail(AssertionError("fail"))
# create devices # create devices
u1 = self.register_user("user", "pass") u1 = self.register_user("user", "pass")
@ -376,7 +376,7 @@ class FederationSenderDevicesTestCases(HomeserverTestCase):
This case tests the behaviour when the server has never been reachable. This case tests the behaviour when the server has never been reachable.
""" """
mock_send_txn = self.hs.get_federation_transport_client().send_transaction mock_send_txn = self.hs.get_federation_transport_client().send_transaction
mock_send_txn.side_effect = lambda t, cb: defer.fail("fail") mock_send_txn.side_effect = lambda t, cb: defer.fail(AssertionError("fail"))
# create devices # create devices
u1 = self.register_user("user", "pass") u1 = self.register_user("user", "pass")
@ -429,7 +429,7 @@ class FederationSenderDevicesTestCases(HomeserverTestCase):
# now the server goes offline # now the server goes offline
mock_send_txn = self.hs.get_federation_transport_client().send_transaction mock_send_txn = self.hs.get_federation_transport_client().send_transaction
mock_send_txn.side_effect = lambda t, cb: defer.fail("fail") mock_send_txn.side_effect = lambda t, cb: defer.fail(AssertionError("fail"))
self.login("user", "pass", device_id="D2") self.login("user", "pass", device_id="D2")
self.login("user", "pass", device_id="D3") self.login("user", "pass", device_id="D3")

View file

@ -25,6 +25,7 @@ from synapse.types import create_requester
from tests.events.test_presence_router import send_presence_update, sync_presence from tests.events.test_presence_router import send_presence_update, sync_presence
from tests.replication._base import BaseMultiWorkerStreamTestCase from tests.replication._base import BaseMultiWorkerStreamTestCase
from tests.test_utils import simple_async_mock
from tests.test_utils.event_injection import inject_member_event from tests.test_utils.event_injection import inject_member_event
from tests.unittest import HomeserverTestCase, override_config from tests.unittest import HomeserverTestCase, override_config
from tests.utils import USE_POSTGRES_FOR_TESTS from tests.utils import USE_POSTGRES_FOR_TESTS
@ -46,8 +47,12 @@ class ModuleApiTestCase(HomeserverTestCase):
self.auth_handler = homeserver.get_auth_handler() self.auth_handler = homeserver.get_auth_handler()
def make_homeserver(self, reactor, clock): def make_homeserver(self, reactor, clock):
# Mock out the calls over federation.
fed_transport_client = Mock(spec=["send_transaction"])
fed_transport_client.send_transaction = simple_async_mock({})
return self.setup_test_homeserver( return self.setup_test_homeserver(
federation_transport_client=Mock(spec=["send_transaction"]), federation_transport_client=fed_transport_client,
) )
def test_can_register_user(self): def test_can_register_user(self):