|
| 1 | +from concurrent import futures |
| 2 | + |
| 3 | +import pytest |
| 4 | +from google.protobuf import any_pb2 |
| 5 | +from google.rpc import code_pb2, status_pb2 |
| 6 | +from grpc import Status, StatusCode, server |
| 7 | +from grpc.aio import insecure_channel |
| 8 | +from grpc_status.rpc_status import to_status |
| 9 | + |
| 10 | +from cadence._internal.rpc.error import CadenceErrorInterceptor |
| 11 | +from cadence.api.v1 import error_pb2, service_meta_pb2_grpc |
| 12 | +from cadence import error |
| 13 | +from google.protobuf.message import Message |
| 14 | + |
| 15 | +from cadence.api.v1.service_meta_pb2 import HealthRequest, HealthResponse |
| 16 | +from cadence.error import CadenceError |
| 17 | + |
| 18 | + |
| 19 | +class FakeService(service_meta_pb2_grpc.MetaAPIServicer): |
| 20 | + def __init__(self) -> None: |
| 21 | + super().__init__() |
| 22 | + self.status: Status | None = None |
| 23 | + self.port: int | None = None |
| 24 | + |
| 25 | + def Health(self, request, context): |
| 26 | + if temp := self.status: |
| 27 | + self.status = None |
| 28 | + context.abort_with_status(temp) |
| 29 | + return HealthResponse(ok=True) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(scope="module") |
| 33 | +def fake_service(): |
| 34 | + fake = FakeService() |
| 35 | + sync_server = server(futures.ThreadPoolExecutor(max_workers=1)) |
| 36 | + service_meta_pb2_grpc.add_MetaAPIServicer_to_server(fake, sync_server) |
| 37 | + fake.port = sync_server.add_insecure_port("[::]:0") |
| 38 | + sync_server.start() |
| 39 | + yield fake |
| 40 | + sync_server.stop(grace=None) |
| 41 | + |
| 42 | +@pytest.mark.usefixtures("fake_service") |
| 43 | +@pytest.mark.parametrize( |
| 44 | + "err,expected", |
| 45 | + [ |
| 46 | + pytest.param(None, None,id="no error"), |
| 47 | + pytest.param( |
| 48 | + error_pb2.WorkflowExecutionAlreadyStartedError(start_request_id="start_request", run_id="run_id"), |
| 49 | + error.WorkflowExecutionAlreadyStartedError(message="message", code=StatusCode.INVALID_ARGUMENT, start_request_id="start_request", run_id="run_id"), |
| 50 | + id="WorkflowExecutionAlreadyStartedError"), |
| 51 | + pytest.param( |
| 52 | + error_pb2.EntityNotExistsError(current_cluster="current_cluster", active_cluster="active_cluster", active_clusters=["active_clusters"]), |
| 53 | + error.EntityNotExistsError(message="message", code=StatusCode.INVALID_ARGUMENT, current_cluster="current_cluster", active_cluster="active_cluster", active_clusters=["active_clusters"]), |
| 54 | + id="EntityNotExistsError"), |
| 55 | + pytest.param( |
| 56 | + error_pb2.WorkflowExecutionAlreadyCompletedError(), |
| 57 | + error.WorkflowExecutionAlreadyCompletedError(message="message", code=StatusCode.INVALID_ARGUMENT), |
| 58 | + id="WorkflowExecutionAlreadyCompletedError"), |
| 59 | + pytest.param( |
| 60 | + error_pb2.DomainNotActiveError(domain="domain", current_cluster="current_cluster", active_cluster="active_cluster", active_clusters=["active_clusters"]), |
| 61 | + error.DomainNotActiveError(message="message", code=StatusCode.INVALID_ARGUMENT, domain="domain", current_cluster="current_cluster", active_cluster="active_cluster", active_clusters=["active_clusters"]), |
| 62 | + id="DomainNotActiveError"), |
| 63 | + pytest.param( |
| 64 | + error_pb2.ClientVersionNotSupportedError(feature_version="feature_version", client_impl="client_impl", supported_versions="supported_versions"), |
| 65 | + error.ClientVersionNotSupportedError(message="message", code=StatusCode.INVALID_ARGUMENT, feature_version="feature_version", client_impl="client_impl", supported_versions="supported_versions"), |
| 66 | + id="ClientVersionNotSupportedError"), |
| 67 | + pytest.param( |
| 68 | + error_pb2.FeatureNotEnabledError(feature_flag="feature_flag"), |
| 69 | + error.FeatureNotEnabledError(message="message", code=StatusCode.INVALID_ARGUMENT,feature_flag="feature_flag"), |
| 70 | + id="FeatureNotEnabledError"), |
| 71 | + pytest.param( |
| 72 | + error_pb2.CancellationAlreadyRequestedError(), |
| 73 | + error.CancellationAlreadyRequestedError(message="message", code=StatusCode.INVALID_ARGUMENT), |
| 74 | + id="CancellationAlreadyRequestedError"), |
| 75 | + pytest.param( |
| 76 | + error_pb2.DomainAlreadyExistsError(), |
| 77 | + error.DomainAlreadyExistsError(message="message", code=StatusCode.INVALID_ARGUMENT), |
| 78 | + id="DomainAlreadyExistsError"), |
| 79 | + pytest.param( |
| 80 | + error_pb2.LimitExceededError(), |
| 81 | + error.LimitExceededError(message="message", code=StatusCode.INVALID_ARGUMENT), |
| 82 | + id="LimitExceededError"), |
| 83 | + pytest.param( |
| 84 | + error_pb2.QueryFailedError(), |
| 85 | + error.QueryFailedError(message="message", code=StatusCode.INVALID_ARGUMENT), |
| 86 | + id="QueryFailedError"), |
| 87 | + pytest.param( |
| 88 | + error_pb2.ServiceBusyError(reason="reason"), |
| 89 | + error.ServiceBusyError(message="message", code=StatusCode.INVALID_ARGUMENT, reason="reason"), |
| 90 | + id="ServiceBusyError"), |
| 91 | + pytest.param( |
| 92 | + to_status(status_pb2.Status(code=code_pb2.PERMISSION_DENIED, message="no permission")), |
| 93 | + error.CadenceError(message="no permission", code=StatusCode.PERMISSION_DENIED), |
| 94 | + id="unknown error type"), |
| 95 | + ] |
| 96 | +) |
| 97 | +@pytest.mark.asyncio |
| 98 | +async def test_map_error(fake_service, err: Message | Status, expected: CadenceError): |
| 99 | + async with insecure_channel(f"[::]:{fake_service.port}", interceptors=[CadenceErrorInterceptor()]) as channel: |
| 100 | + stub = service_meta_pb2_grpc.MetaAPIStub(channel) |
| 101 | + if expected is None: |
| 102 | + response = await stub.Health(HealthRequest(), timeout=1) |
| 103 | + assert response == HealthResponse(ok=True) |
| 104 | + else: |
| 105 | + if isinstance(err, Message): |
| 106 | + fake_service.status = details_to_status(err) |
| 107 | + else: |
| 108 | + fake_service.status = err |
| 109 | + with pytest.raises(type(expected)) as exc_info: |
| 110 | + await stub.Health(HealthRequest(), timeout=1) |
| 111 | + assert exc_info.value.args == expected.args |
| 112 | + |
| 113 | +def details_to_status(message: Message) -> Status: |
| 114 | + detail = any_pb2.Any() |
| 115 | + detail.Pack(message) |
| 116 | + status_proto = status_pb2.Status( |
| 117 | + code=code_pb2.INVALID_ARGUMENT, |
| 118 | + message="message", |
| 119 | + details=[detail], |
| 120 | + ) |
| 121 | + return to_status(status_proto) |
| 122 | + |
0 commit comments