|
| 1 | +# Copyright 2022 The Matrix.org Foundation C.I.C. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from http import HTTPStatus |
| 16 | +from typing import Dict, List, Tuple |
| 17 | + |
| 18 | +from synapse.api.errors import Codes |
| 19 | +from synapse.federation.transport.server import BaseFederationServlet |
| 20 | +from synapse.federation.transport.server._base import Authenticator |
| 21 | +from synapse.http.server import JsonResource, cancellable |
| 22 | +from synapse.server import HomeServer |
| 23 | +from synapse.types import JsonDict |
| 24 | +from synapse.util.ratelimitutils import FederationRateLimiter |
| 25 | + |
| 26 | +from tests import unittest |
| 27 | +from tests.http.server._base import EndpointCancellationTestHelperMixin |
| 28 | + |
| 29 | + |
| 30 | +class CancellableFederationServlet(BaseFederationServlet): |
| 31 | + PATH = "/sleep" |
| 32 | + |
| 33 | + def __init__( |
| 34 | + self, |
| 35 | + hs: HomeServer, |
| 36 | + authenticator: Authenticator, |
| 37 | + ratelimiter: FederationRateLimiter, |
| 38 | + server_name: str, |
| 39 | + ): |
| 40 | + super().__init__(hs, authenticator, ratelimiter, server_name) |
| 41 | + self.clock = hs.get_clock() |
| 42 | + |
| 43 | + @cancellable |
| 44 | + async def on_GET( |
| 45 | + self, origin: str, content: None, query: Dict[bytes, List[bytes]] |
| 46 | + ) -> Tuple[int, JsonDict]: |
| 47 | + await self.clock.sleep(1.0) |
| 48 | + return HTTPStatus.OK, {"result": True} |
| 49 | + |
| 50 | + async def on_POST( |
| 51 | + self, origin: str, content: JsonDict, query: Dict[bytes, List[bytes]] |
| 52 | + ) -> Tuple[int, JsonDict]: |
| 53 | + await self.clock.sleep(1.0) |
| 54 | + return HTTPStatus.OK, {"result": True} |
| 55 | + |
| 56 | + |
| 57 | +class BaseFederationServletCancellationTests( |
| 58 | + unittest.FederatingHomeserverTestCase, EndpointCancellationTestHelperMixin |
| 59 | +): |
| 60 | + """Tests for `BaseFederationServlet` cancellation.""" |
| 61 | + |
| 62 | + path = f"{CancellableFederationServlet.PREFIX}{CancellableFederationServlet.PATH}" |
| 63 | + |
| 64 | + def create_test_resource(self): |
| 65 | + """Overrides `HomeserverTestCase.create_test_resource`.""" |
| 66 | + resource = JsonResource(self.hs) |
| 67 | + |
| 68 | + CancellableFederationServlet( |
| 69 | + hs=self.hs, |
| 70 | + authenticator=Authenticator(self.hs), |
| 71 | + ratelimiter=self.hs.get_federation_ratelimiter(), |
| 72 | + server_name=self.hs.hostname, |
| 73 | + ).register(resource) |
| 74 | + |
| 75 | + return resource |
| 76 | + |
| 77 | + def test_cancellable_disconnect(self) -> None: |
| 78 | + """Test that handlers with the `@cancellable` flag can be cancelled.""" |
| 79 | + channel = self.make_signed_federation_request( |
| 80 | + "GET", self.path, await_result=False |
| 81 | + ) |
| 82 | + |
| 83 | + # Advance past all the rate limiting logic. If we disconnect too early, the |
| 84 | + # request won't be processed. |
| 85 | + self.pump() |
| 86 | + |
| 87 | + self._test_disconnect( |
| 88 | + self.reactor, |
| 89 | + channel, |
| 90 | + expect_cancellation=True, |
| 91 | + expected_body={"error": "Request cancelled", "errcode": Codes.UNKNOWN}, |
| 92 | + ) |
| 93 | + |
| 94 | + def test_uncancellable_disconnect(self) -> None: |
| 95 | + """Test that handlers without the `@cancellable` flag cannot be cancelled.""" |
| 96 | + channel = self.make_signed_federation_request( |
| 97 | + "POST", |
| 98 | + self.path, |
| 99 | + content={}, |
| 100 | + await_result=False, |
| 101 | + ) |
| 102 | + |
| 103 | + # Advance past all the rate limiting logic. If we disconnect too early, the |
| 104 | + # request won't be processed. |
| 105 | + self.pump() |
| 106 | + |
| 107 | + self._test_disconnect( |
| 108 | + self.reactor, |
| 109 | + channel, |
| 110 | + expect_cancellation=False, |
| 111 | + expected_body={"result": True}, |
| 112 | + ) |
0 commit comments