Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix logged errors from unit tests. #10939

Merged
merged 5 commits into from
Sep 30, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions tests/appservice/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from synapse.logging.context import make_deferred_yieldable

from tests import unittest
from tests.test_utils import make_awaitable, simple_async_mock
from tests.test_utils import simple_async_mock

from ..utils import MockClock

Expand All @@ -49,11 +49,10 @@ def test_single_service_up_txn_sent(self):
txn = Mock(id=txn_id, service=service, events=events)

# mock methods
self.store.get_appservice_state = Mock(
return_value=defer.succeed(ApplicationServiceState.UP)
)
txn.send = Mock(return_value=make_awaitable(True))
self.store.create_appservice_txn = Mock(return_value=defer.succeed(txn))
self.store.get_appservice_state = simple_async_mock(ApplicationServiceState.UP)
txn.send = simple_async_mock(True)
txn.complete = simple_async_mock(True)
self.store.create_appservice_txn = simple_async_mock(txn)
Comment on lines -52 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any change in behaviour here, or is this just a cleaner way of doing it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one bugfix in here, which is also handling txn.complete, which previously ended up as just a Mock which can't be awaited. The rest is just refactoring to be a bit cleaner. (Sorry for not separating those!)

While I was in here I also converted defer.succeed since the mocked functions don't return Deferreds anymore. Note that we (annoyingly) have two ways of making async mocks at the moment (simple_async_mock and make_awaitable), essentially you can switch defer.succeed -> make_awaitable OR you can switch Mock(return_value=defer.succeed(...)) -> simple_async_mock(...). Sad times.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it. Thanks!


# actual call
self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
Expand All @@ -71,10 +70,10 @@ def test_single_service_down(self):
events = [Mock(), Mock()]

txn = Mock(id="idhere", service=service, events=events)
self.store.get_appservice_state = Mock(
return_value=defer.succeed(ApplicationServiceState.DOWN)
self.store.get_appservice_state = simple_async_mock(
ApplicationServiceState.DOWN
)
self.store.create_appservice_txn = Mock(return_value=defer.succeed(txn))
self.store.create_appservice_txn = simple_async_mock(txn)

# actual call
self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
Expand All @@ -94,12 +93,10 @@ def test_single_service_up_txn_not_sent(self):
txn = Mock(id=txn_id, service=service, events=events)

# mock methods
self.store.get_appservice_state = Mock(
return_value=defer.succeed(ApplicationServiceState.UP)
)
self.store.set_appservice_state = Mock(return_value=defer.succeed(True))
txn.send = Mock(return_value=make_awaitable(False)) # fails to send
self.store.create_appservice_txn = Mock(return_value=defer.succeed(txn))
self.store.get_appservice_state = simple_async_mock(ApplicationServiceState.UP)
self.store.set_appservice_state = simple_async_mock(True)
txn.send = simple_async_mock(False) # fails to send
self.store.create_appservice_txn = simple_async_mock(txn)

# actual call
self.successResultOf(defer.ensureDeferred(self.txnctrl.send(service, events)))
Expand Down Expand Up @@ -144,8 +141,8 @@ def take_txn(*args, **kwargs):
self.recoverer.recover()
# shouldn't have called anything prior to waiting for exp backoff
self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
txn.send = Mock(return_value=make_awaitable(True))
txn.complete.return_value = make_awaitable(None)
txn.send = simple_async_mock(True)
txn.complete = simple_async_mock(None)
# wait for exp backoff
self.clock.advance_time(2)
self.assertEquals(1, txn.send.call_count)
Expand All @@ -170,8 +167,8 @@ def take_txn(*args, **kwargs):

self.recoverer.recover()
self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
txn.send = Mock(return_value=make_awaitable(False))
txn.complete.return_value = make_awaitable(None)
txn.send = simple_async_mock(False)
txn.complete = simple_async_mock(None)
self.clock.advance_time(2)
self.assertEquals(1, txn.send.call_count)
self.assertEquals(0, txn.complete.call_count)
Expand All @@ -184,7 +181,7 @@ def take_txn(*args, **kwargs):
self.assertEquals(3, txn.send.call_count)
self.assertEquals(0, txn.complete.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.
self.clock.advance_time(16)
self.assertEquals(1, txn.send.call_count) # new mock reset call count
Expand All @@ -195,6 +192,7 @@ def take_txn(*args, **kwargs):
class ApplicationServiceSchedulerQueuerTestCase(unittest.TestCase):
def setUp(self):
self.txn_ctrl = Mock()
self.txn_ctrl.send = simple_async_mock()
self.queuer = _ServiceQueuer(self.txn_ctrl, MockClock())

def test_send_single_event_no_queue(self):
Expand Down