This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add helper class for testing request cancellation #12630
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
75e1c07
Expose the `SynapseRequest` from `FakeChannel` for testing disconnection
f0b57d5
Add helper class for testing request cancellation
957474d
Add newsfile
ccd0ab7
Rename to `EndpointCancellationTestCase` to `EndpointCancellationTest…
390117a
Add missing docstring for `expected_body` parameter
8c9b9f6
Improve assertion message when `await_result=False` is forgotten
ee45caf
Update the changelog, since not all related PRs will land in the same…
01ad80b
Update the changelog, since not all related PRs will land in the same…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add a helper class for testing request cancellation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2022 The Matrix.org Foundation C.I.C. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Copyright 2022 The Matrix.org Foundation C.I.C. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unles4s required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from http import HTTPStatus | ||
from typing import Any, Callable, Optional, Union | ||
from unittest import mock | ||
|
||
from twisted.internet.error import ConnectionDone | ||
|
||
from synapse.http.server import ( | ||
HTTP_STATUS_REQUEST_CANCELLED, | ||
respond_with_html_bytes, | ||
respond_with_json, | ||
) | ||
from synapse.types import JsonDict | ||
|
||
from tests import unittest | ||
from tests.server import FakeChannel, ThreadedMemoryReactorClock | ||
|
||
|
||
class EndpointCancellationTestHelperMixin(unittest.TestCase): | ||
"""Provides helper methods for testing cancellation of endpoints.""" | ||
|
||
def _test_disconnect( | ||
self, | ||
reactor: ThreadedMemoryReactorClock, | ||
channel: FakeChannel, | ||
expect_cancellation: bool, | ||
expected_body: Union[bytes, JsonDict], | ||
expected_code: Optional[int] = None, | ||
) -> None: | ||
"""Disconnects an in-flight request and checks the response. | ||
|
||
Args: | ||
reactor: The twisted reactor running the request handler. | ||
channel: The `FakeChannel` for the request. | ||
expect_cancellation: `True` if request processing is expected to be | ||
cancelled, `False` if the request should run to completion. | ||
expected_body: The expected response for the request. | ||
expected_code: The expected status code for the request. Defaults to `200` | ||
or `499` depending on `expect_cancellation`. | ||
""" | ||
# Determine the expected status code. | ||
if expected_code is None: | ||
if expect_cancellation: | ||
expected_code = HTTP_STATUS_REQUEST_CANCELLED | ||
else: | ||
expected_code = HTTPStatus.OK | ||
|
||
request = channel.request | ||
self.assertFalse( | ||
channel.is_finished(), | ||
"Request finished before we could disconnect - " | ||
"was `await_result=False` passed to `make_request`?", | ||
) | ||
|
||
# We're about to disconnect the request. This also disconnects the channel, so | ||
# we have to rely on mocks to extract the response. | ||
respond_method: Callable[..., Any] | ||
if isinstance(expected_body, bytes): | ||
respond_method = respond_with_html_bytes | ||
else: | ||
respond_method = respond_with_json | ||
|
||
with mock.patch( | ||
f"synapse.http.server.{respond_method.__name__}", wraps=respond_method | ||
) as respond_mock: | ||
# Disconnect the request. | ||
request.connectionLost(reason=ConnectionDone()) | ||
|
||
if expect_cancellation: | ||
# An immediate cancellation is expected. | ||
respond_mock.assert_called_once() | ||
args, _kwargs = respond_mock.call_args | ||
code, body = args[1], args[2] | ||
self.assertEqual(code, expected_code) | ||
self.assertEqual(request.code, expected_code) | ||
self.assertEqual(body, expected_body) | ||
else: | ||
respond_mock.assert_not_called() | ||
|
||
# The handler is expected to run to completion. | ||
reactor.pump([1.0]) | ||
respond_mock.assert_called_once() | ||
args, _kwargs = respond_mock.call_args | ||
code, body = args[1], args[2] | ||
self.assertEqual(code, expected_code) | ||
self.assertEqual(request.code, expected_code) | ||
self.assertEqual(body, expected_body) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.