test__base.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. from http import HTTPStatus
  15. from typing import Tuple
  16. from twisted.web.server import Request
  17. from synapse.api.errors import Codes
  18. from synapse.http.server import JsonResource, cancellable
  19. from synapse.replication.http import REPLICATION_PREFIX
  20. from synapse.replication.http._base import ReplicationEndpoint
  21. from synapse.server import HomeServer
  22. from synapse.types import JsonDict
  23. from tests import unittest
  24. from tests.http.server._base import test_disconnect
  25. class CancellableReplicationEndpoint(ReplicationEndpoint):
  26. NAME = "cancellable_sleep"
  27. PATH_ARGS = ()
  28. CACHE = False
  29. def __init__(self, hs: HomeServer):
  30. super().__init__(hs)
  31. self.clock = hs.get_clock()
  32. @staticmethod
  33. async def _serialize_payload() -> JsonDict:
  34. return {}
  35. @cancellable
  36. async def _handle_request( # type: ignore[override]
  37. self, request: Request
  38. ) -> Tuple[int, JsonDict]:
  39. await self.clock.sleep(1.0)
  40. return HTTPStatus.OK, {"result": True}
  41. class UncancellableReplicationEndpoint(ReplicationEndpoint):
  42. NAME = "uncancellable_sleep"
  43. PATH_ARGS = ()
  44. CACHE = False
  45. def __init__(self, hs: HomeServer):
  46. super().__init__(hs)
  47. self.clock = hs.get_clock()
  48. @staticmethod
  49. async def _serialize_payload() -> JsonDict:
  50. return {}
  51. async def _handle_request( # type: ignore[override]
  52. self, request: Request
  53. ) -> Tuple[int, JsonDict]:
  54. await self.clock.sleep(1.0)
  55. return HTTPStatus.OK, {"result": True}
  56. class ReplicationEndpointCancellationTestCase(unittest.HomeserverTestCase):
  57. """Tests for `ReplicationEndpoint` cancellation."""
  58. def create_test_resource(self):
  59. """Overrides `HomeserverTestCase.create_test_resource`."""
  60. resource = JsonResource(self.hs)
  61. CancellableReplicationEndpoint(self.hs).register(resource)
  62. UncancellableReplicationEndpoint(self.hs).register(resource)
  63. return resource
  64. def test_cancellable_disconnect(self) -> None:
  65. """Test that handlers with the `@cancellable` flag can be cancelled."""
  66. path = f"{REPLICATION_PREFIX}/{CancellableReplicationEndpoint.NAME}/"
  67. channel = self.make_request("POST", path, await_result=False)
  68. test_disconnect(
  69. self.reactor,
  70. channel,
  71. expect_cancellation=True,
  72. expected_body={"error": "Request cancelled", "errcode": Codes.UNKNOWN},
  73. )
  74. def test_uncancellable_disconnect(self) -> None:
  75. """Test that handlers without the `@cancellable` flag cannot be cancelled."""
  76. path = f"{REPLICATION_PREFIX}/{UncancellableReplicationEndpoint.NAME}/"
  77. channel = self.make_request("POST", path, await_result=False)
  78. test_disconnect(
  79. self.reactor,
  80. channel,
  81. expect_cancellation=False,
  82. expected_body={"result": True},
  83. )