def test_should_retry_on_error(retry_flag, error_code, result):
    request = Request(
        headers={'re': retry_flag},
    )

    error = TChannelError.from_code(error_code, description="retry")
    assert request.should_retry_on_error(error) == result
Exemple #2
0
def test_should_retry_on_error(retry_flag, error_code, result):
    request = Request(
        headers={'re': retry_flag},
    )

    error = TChannelError.from_code(error_code, description="retry")
    assert request.should_retry_on_error(error) == result
def test_after_receive_system_error_per_attempt(statsd_hook, req):
    error = TChannelError.from_code(code=ErrorCode.bad_request)
    statsd_hook.after_receive_system_error_per_attempt(req, error)
    statsd_hook._statsd.count.assert_called_with(
        "tchannel.outbound.calls.per-attempt.system-errors.no-service." +
        "test.endpoint1.bad-request", 1
    )
Exemple #4
0
    def send(self,
             arg1,
             arg2,
             arg3,
             headers=None,
             traceflag=None,
             retry_limit=None,
             ttl=None):
        arg1, arg2, arg3 = map(maybe_stream, [arg1, arg2, arg3])

        endpoint = yield read_full(arg1)

        headers = headers or {}
        headers.setdefault('as', self.arg_scheme)

        vcr_request = proxy.Request(
            serviceName=self.service.encode('utf-8'),
            hostPort=self.hostport,
            knownPeers=self.original_tchannel.peers.hosts,
            endpoint=endpoint,
            headers=(yield read_full(arg2)),
            body=(yield read_full(arg3)),
            argScheme=getattr(proxy.ArgScheme, self.arg_scheme.upper()),
            transportHeaders=[
                proxy.TransportHeader(bytes(k), bytes(v))
                for k, v in headers.items()
            ],
        )

        # TODO what to do with traceflag, attempt-times, ttl
        # TODO catch protocol errors

        from tchannel import TChannel
        tchannel = TChannel('proxy-client')

        with force_reset():
            vcr_response_future = tchannel.thrift(
                proxy.VCRProxy.send(vcr_request),
                hostport=self.vcr_hostport,
            )

        try:
            vcr_response = yield vcr_response_future
        except proxy.RemoteServiceError as e:
            raise TChannelError.from_code(
                e.code,
                description=("The remote service threw a protocol error: %s" %
                             e.message))

        response = Response(
            code=vcr_response.body.code,
            argstreams=[
                maybe_stream(endpoint),
                maybe_stream(vcr_response.body.headers),
                maybe_stream(vcr_response.body.body),
            ],
            # TODO headers=vcr_response.transportHeaders,
        )

        raise gen.Return(response)
def test_after_receive_system_error_per_attempt(statsd_hook, request):
    error = TChannelError.from_code(code=ErrorCode.bad_request)
    statsd_hook.after_receive_system_error_per_attempt(request, error)
    statsd_hook._statsd.count.assert_called_with(
        "tchannel.outbound.calls.per-attempt.system-errors.no-service." +
        "test.endpoint1.bad-request", 1
    )
Exemple #6
0
    def send(self, arg1, arg2, arg3,
             headers=None,
             traceflag=None,
             retry_limit=None,
             ttl=None):
        arg1, arg2, arg3 = map(maybe_stream, [arg1, arg2, arg3])

        endpoint = yield read_full(arg1)

        headers = headers or {}
        headers.setdefault('as', self.arg_scheme)

        vcr_request = proxy.Request(
            serviceName=self.service.encode('utf-8'),
            hostPort=self.hostport,
            knownPeers=self.original_tchannel.peers.hosts,
            endpoint=endpoint,
            headers=(yield read_full(arg2)),
            body=(yield read_full(arg3)),
            argScheme=getattr(proxy.ArgScheme, self.arg_scheme.upper()),
            transportHeaders=[
                proxy.TransportHeader(bytes(k), bytes(v))
                for k, v in headers.items()
            ],
        )

        # TODO what to do with traceflag, attempt-times, ttl
        # TODO catch protocol errors

        from tchannel import TChannel
        tchannel = TChannel('proxy-client')

        with force_reset():
            vcr_response_future = tchannel.thrift(
                proxy.VCRProxy.send(vcr_request),
                hostport=self.vcr_hostport,
            )

        try:
            vcr_response = yield vcr_response_future
        except proxy.RemoteServiceError as e:
            raise TChannelError.from_code(
                e.code,
                description=(
                    "The remote service threw a protocol error: %s" %
                    e.message
                )
            )

        response = Response(
            code=vcr_response.body.code,
            argstreams=[
                maybe_stream(endpoint),
                maybe_stream(vcr_response.body.headers),
                maybe_stream(vcr_response.body.body),
            ],
            # TODO headers=vcr_response.transportHeaders,
        )

        raise gen.Return(response)
Exemple #7
0
    def send(self, arg1, arg2, arg3, headers=None, ttl=None, **kwargs):
        arg1, arg2, arg3 = list(map(maybe_stream, [arg1, arg2, arg3]))

        endpoint = yield read_full(arg1)

        headers = headers or {}
        headers.setdefault('as', self.arg_scheme)

        vcr_request = proxy.Request(
            serviceName=self.service.encode('utf-8'),
            hostPort=self.hostport,
            knownPeers=self.original_tchannel.peers.hosts,
            endpoint=endpoint,
            headers=(yield read_full(arg2)),
            body=(yield read_full(arg3)),
            argScheme=getattr(proxy.ArgScheme, self.arg_scheme.upper()),
            transportHeaders=[
                proxy.TransportHeader(bytes(k.encode('utf8')),
                                      bytes(v.encode('utf8')))
                for k, v in headers.items()
            ],
        )

        # TODO what to do with traceflag, attempt-times, ttl
        # TODO catch protocol errors

        from tchannel import TChannel
        tchannel = TChannel('proxy-client')

        with force_reset():
            vcr_response_future = tchannel.thrift(
                proxy.VCRProxy.send(vcr_request),
                hostport=self.vcr_hostport,
                timeout=float(ttl) * 1.1 if ttl else ttl,
                # If a timeout was specified, use that plus 10% to give some
                # leeway to the VCR proxy request itself.
            )

        try:
            vcr_response = yield vcr_response_future
        except proxy.RemoteServiceError as e:
            raise TChannelError.from_code(
                e.code,
                description=("The remote service threw a protocol error: %s" %
                             (e, )))

        response = Response(
            code=vcr_response.body.code,
            argstreams=[
                maybe_stream(endpoint),
                maybe_stream(vcr_response.body.headers),
                maybe_stream(vcr_response.body.body),
            ],
            # TODO headers=vcr_response.transportHeaders,
        )

        raise gen.Return(response)
Exemple #8
0
def handler_error(request, response):
    yield tornado.gen.sleep(0.01)
    response.connection.send_error(
        ErrorCode.busy,
        "retry",
        response.id,
    )
    # stop normal response streams
    response.set_exception(TChannelError("stop stream"))
Exemple #9
0
 def execute(request, response):
     # send error message for test purpose only
     connection = response.connection
     connection.send_error(
         protocoal_error.code,
         protocoal_error.description,
         response.id,
     )
     # stop normal response streams
     response.set_exception(TChannelError("stop stream"))
def test_protocol_error(vcr_service, cassette, call, mock_server):
    allow(cassette).can_replay.and_return(False)
    expect(cassette).record.never()

    mock_server.expect_call("endpoint").and_raise(TChannelError.from_code(1, description="great sadness"))

    with pytest.raises(VCRProxy.RemoteServiceError) as exc_info:
        yield call("endpoint", "body")

    assert "great sadness" in str(exc_info)
    assert exc_info.value.code == 1
Exemple #11
0
def test_protocol_error(vcr_service, cassette, call, mock_server):
    allow(cassette).can_replay.and_return(False)
    expect(cassette).record.never()

    mock_server.expect_call('endpoint').and_raise(
        TChannelError.from_code(1, description='great sadness'))

    with pytest.raises(proxy.RemoteServiceError) as exc_info:
        yield call('endpoint', 'body')

    assert 'great sadness' in str(exc_info)
    assert exc_info.value.code == 1
Exemple #12
0
def handler_error(request, response):
    yield tornado.gen.sleep(0.01)
    yield response.connection.send_error(BusyError("retry", request.id))
    # stop normal response streams
    response.set_exception(TChannelError("stop stream"))