-
-
Couldn't load subscription status.
- Fork 2.1k
Add missing type hints in tests #14879
Changes from 1 commit
de7c662
eaf93d9
2f42026
ea5b1a7
22e4bde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,18 +13,22 @@ | |
| # limitations under the License. | ||
|
|
||
| from http import HTTPStatus | ||
| from typing import Any, Generator, Tuple, cast | ||
| from unittest.mock import Mock, call | ||
|
|
||
| from twisted.internet import defer, reactor | ||
| from twisted.internet import defer, reactor as _reactor | ||
|
|
||
| from synapse.logging.context import SENTINEL_CONTEXT, LoggingContext, current_context | ||
| from synapse.rest.client.transactions import CLEANUP_PERIOD_MS, HttpTransactionCache | ||
| from synapse.types import ISynapseReactor, JsonDict | ||
| from synapse.util import Clock | ||
|
|
||
| from tests import unittest | ||
| from tests.test_utils import make_awaitable | ||
| from tests.utils import MockClock | ||
|
|
||
| reactor = cast(ISynapseReactor, _reactor) | ||
|
|
||
|
|
||
| class HttpTransactionCacheTestCase(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
|
|
@@ -34,11 +38,13 @@ def setUp(self) -> None: | |
| self.hs.get_auth = Mock() | ||
| self.cache = HttpTransactionCache(self.hs) | ||
|
|
||
| self.mock_http_response = (HTTPStatus.OK, "GOOD JOB!") | ||
| self.mock_http_response = (HTTPStatus.OK, {"result": "GOOD JOB!"}) | ||
|
Comment on lines
-37
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is just making the mock response match what we expect a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| self.mock_key = "foo" | ||
|
|
||
| @defer.inlineCallbacks | ||
| def test_executes_given_function(self): | ||
| def test_executes_given_function( | ||
| self, | ||
| ) -> Generator["defer.Deferred[Any]", object, None]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this signature means that
That's ever so slightly sad, but I don't think there's a good way to get mypy to check this short of using proper There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It unfortunately needs to match what |
||
| cb = Mock(return_value=make_awaitable(self.mock_http_response)) | ||
| res = yield self.cache.fetch_or_execute( | ||
| self.mock_key, cb, "some_arg", keyword="arg" | ||
|
|
@@ -47,7 +53,9 @@ def test_executes_given_function(self): | |
| self.assertEqual(res, self.mock_http_response) | ||
|
|
||
| @defer.inlineCallbacks | ||
| def test_deduplicates_based_on_key(self): | ||
| def test_deduplicates_based_on_key( | ||
| self, | ||
| ) -> Generator["defer.Deferred[Any]", object, None]: | ||
| cb = Mock(return_value=make_awaitable(self.mock_http_response)) | ||
| for i in range(3): # invoke multiple times | ||
| res = yield self.cache.fetch_or_execute( | ||
|
|
@@ -58,18 +66,20 @@ def test_deduplicates_based_on_key(self): | |
| cb.assert_called_once_with("some_arg", keyword="arg", changing_args=0) | ||
|
|
||
| @defer.inlineCallbacks | ||
| def test_logcontexts_with_async_result(self): | ||
| def test_logcontexts_with_async_result( | ||
| self, | ||
| ) -> Generator["defer.Deferred[Any]", object, None]: | ||
| @defer.inlineCallbacks | ||
| def cb(): | ||
| def cb() -> Generator["defer.Deferred[object]", object, Tuple[int, JsonDict]]: | ||
| yield Clock(reactor).sleep(0) | ||
| return "yay" | ||
| return 1, {} | ||
|
|
||
| @defer.inlineCallbacks | ||
| def test(): | ||
| def test() -> Generator["defer.Deferred[Any]", object, None]: | ||
| with LoggingContext("c") as c1: | ||
| res = yield self.cache.fetch_or_execute(self.mock_key, cb) | ||
| self.assertIs(current_context(), c1) | ||
| self.assertEqual(res, "yay") | ||
| self.assertEqual(res, (1, {})) | ||
|
|
||
| # run the test twice in parallel | ||
| d = defer.gatherResults([test(), test()]) | ||
|
|
@@ -78,13 +88,15 @@ def test(): | |
| self.assertIs(current_context(), SENTINEL_CONTEXT) | ||
|
|
||
| @defer.inlineCallbacks | ||
| def test_does_not_cache_exceptions(self): | ||
| def test_does_not_cache_exceptions( | ||
| self, | ||
| ) -> Generator["defer.Deferred[Any]", object, None]: | ||
| """Checks that, if the callback throws an exception, it is called again | ||
| for the next request. | ||
| """ | ||
| called = [False] | ||
|
|
||
| def cb(): | ||
| def cb() -> "defer.Deferred[Tuple[int, JsonDict]]": | ||
| if called[0]: | ||
| # return a valid result the second time | ||
| return defer.succeed(self.mock_http_response) | ||
|
|
@@ -104,13 +116,15 @@ def cb(): | |
| self.assertIs(current_context(), test_context) | ||
|
|
||
| @defer.inlineCallbacks | ||
| def test_does_not_cache_failures(self): | ||
| def test_does_not_cache_failures( | ||
| self, | ||
| ) -> Generator["defer.Deferred[Any]", object, None]: | ||
| """Checks that, if the callback returns a failure, it is called again | ||
| for the next request. | ||
| """ | ||
| called = [False] | ||
|
|
||
| def cb(): | ||
| def cb() -> "defer.Deferred[Tuple[int, JsonDict]]": | ||
| if called[0]: | ||
| # return a valid result the second time | ||
| return defer.succeed(self.mock_http_response) | ||
|
|
@@ -130,7 +144,7 @@ def cb(): | |
| self.assertIs(current_context(), test_context) | ||
|
|
||
| @defer.inlineCallbacks | ||
| def test_cleans_up(self): | ||
| def test_cleans_up(self) -> Generator["defer.Deferred[Any]", object, None]: | ||
| cb = Mock(return_value=make_awaitable(self.mock_http_response)) | ||
| yield self.cache.fetch_or_execute(self.mock_key, cb, "an arg") | ||
| # should NOT have cleaned up yet | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.