def test_loop_failure(tornado_pair):
    server, client = tornado_pair
    headers = dummy_headers()

    # ... yeah
    server.tchannel = mock.MagicMock()
    server.tchannel.event_emitter.fire.return_value = gen.maybe_future(None)

    client.tchannel = mock.MagicMock()
    client.tchannel.event_emitter.fire.return_value = gen.maybe_future(None)

    handshake_future = client.initiate_handshake(headers=headers)
    yield server.expect_handshake(headers=headers)
    yield handshake_future

    assert client._handshake_performed
    assert server._handshake_performed

    # We'll put an invalid message into the reader queue. This should cause one
    # iteration of the loop to fail but the system should continue working
    # afterwards.

    yield server.reader.queue.put(gen.maybe_future(42))  # not a message

    id = client.writer.next_message_id()
    response_future = client.send_request(
        Request(
            id=id,
            service='server',
            endpoint='bar',
            headers={'cn': 'client'},
        ))

    call_req = yield server. await ()
    assert call_req.message_type == messages.Types.CALL_REQ

    response = Response(id=id)
    response.close_argstreams(force=True)

    yield server.post_response(response)
    yield response_future

    assert client._handshake_performed
    assert server._handshake_performed

    client.close()

    # The system needs a little time to recognize that the connections were
    # closed.
    yield gen.sleep(0.15)

    assert client.closed
    assert server.closed
def test_loop_failure(tornado_pair):
    server, client = tornado_pair
    headers = dummy_headers()

    # ... yeah
    server.tchannel = mock.MagicMock()
    client.tchannel = mock.MagicMock()

    handshake_future = client.initiate_handshake(headers=headers)
    yield server.expect_handshake(headers=headers)
    yield handshake_future

    assert client._handshake_performed
    assert server._handshake_performed

    # We'll put an invalid message into the reader queue. This should cause one
    # iteration of the loop to fail but the system should continue working
    # afterwards.

    yield server.reader.queue.put(gen.maybe_future(42))  # not a message

    id = client.writer.next_message_id()
    response_future = client.send_request(Request(
        id=id,
        service='server',
        endpoint='bar',
        headers={'cn': 'client'},
    ))

    call_req = yield server.await()
    assert call_req.message_type == messages.Types.CALL_REQ

    response = Response(id=id)
    response.close_argstreams(force=True)

    yield server.post_response(response)
    yield response_future

    assert client._handshake_performed
    assert server._handshake_performed

    client.close()

    # The system needs a little time to recognize that the connections were
    # closed.
    yield gen.sleep(0.15)

    assert client.closed
    assert server.closed
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
0
def test_deprecated_build_handler():

    def call(treq, tres):
        assert treq.transport.headers == {
            'as': 'thrift', 'cn': 'test_caller'
        }

        tres.write_header('foo', 'baar')
        return "world"

    response_header = InMemStream()
    response_body = InMemStream()

    req = Request(
        argstreams=[
            InMemStream('hello'),
            InMemStream('\00\00'),  # no headers
            InMemStream('\00'),  # empty struct
        ],
        serializer=ThriftSerializer(FakeResult),
        headers={'cn': 'test_caller', 'as': 'thrift'},
    )
    req.close_argstreams()

    res = Response(
        argstreams=[
            InMemStream(),
            response_header,
            response_body,
        ],
        serializer=ThriftSerializer(FakeResult),
    )

    handler = deprecated_build_handler(FakeResult, call)
    yield handler(req, res)

    serialized_headers = yield response_header.read()
    assert serialized_headers == bytearray(
        [
            0x00, 0x01,  # num headers = 1
            0x00, 0x03,  # strlen('foo') = 3
        ] + list('foo') + [
            0x00, 0x04,  # strlen('baar') = 4
        ] + list('baar')
    )

    serialized_body = yield response_body.read()
    assert serialized_body == bytearray([
        0x0b,                    # field type = TType.STRING
        0x00, 0x00,              # field ID = 0
        0x00, 0x00, 0x00, 0x05,  # string length = 5
    ] + list("world") + [
        0x00,                    # end struct
    ])

    assert 0 == res.status_code
Beispiel #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 = VCRProxy.Request(
            serviceName=self.service.encode('utf-8'),
            hostPort=self.hostport,
            endpoint=endpoint,
            headers=(yield read_full(arg2)),
            body=(yield read_full(arg3)),
            argScheme=getattr(VCRProxy.ArgScheme, self.arg_scheme.upper()),
            transportHeaders=[
                VCRProxy.TransportHeader(k, v) for k, v in headers.items()
            ],
        )

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

        with force_reset():
            vcr_response_future = self.vcr_client.send(vcr_request)
        try:
            vcr_response = yield vcr_response_future
        except VCRProxy.RemoteServiceError as e:
            raise ProtocolError(
                code=e.code,
                description=("The remote service threw a protocol error: %s" %
                             e.message))
        response = Response(
            code=vcr_response.code,
            argstreams=[
                maybe_stream(endpoint),
                maybe_stream(vcr_response.headers),
                maybe_stream(vcr_response.body),
            ],
            # TODO headers=vcr_response.transportHeaders,
        )

        raise gen.Return(response)
Beispiel #7
0
def test_deprecated_build_handler_exception():
    def call(treq, tres):
        raise FakeException('fail')

    response_body = mock.Mock(spec=InMemStream)

    req = Request(
        argstreams=[
            InMemStream('hello'),
            InMemStream('\00\00'),  # no headers
            InMemStream('\00'),  # empty struct
        ],
        serializer=ThriftSerializer(FakeResult),
    )
    req.close_argstreams()

    res = Response(
        argstreams=[
            InMemStream(),
            InMemStream(),
            response_body,
        ],
        serializer=ThriftSerializer(FakeResult),
    )

    handler = deprecated_build_handler(FakeResult, call)
    yield handler(req, res)

    response_body.write.assert_called_once_with(
        bytearray([
            0x0c,  # field type = TType.STRUCT
            0x00,
            0x01,  # field ID = 1
            0x0b,  # field type = TType.STRING
            0x00,
            0x01,  # field ID = 1
            0x00,
            0x00,
            0x00,
            0x04,  # string length = 5
        ] + list(b"fail") + [
            0x00,  # end exception struct
            0x00,  # end response struct
        ]))
    assert 1 == res.status_code