def test_endpoint_can_return_just_body(): # Given this test server: server = TChannel(name='server') @server.register(scheme=schemes.RAW) def endpoint(request): return 'resp body' server.listen() # Make a call: tchannel = TChannel(name='client') resp = yield tchannel.call( scheme=schemes.RAW, service='server', arg1='endpoint', hostport=server.hostport, ) # verify response assert isinstance(resp, Response) assert resp.headers == '' # TODO should be is None to match server assert resp.body == 'resp body'
def test_timeout_should_raise_timeout_error(): # Given this test server: server = TChannel(name='server') @server.register(scheme=schemes.RAW) @gen.coroutine def endpoint(request): yield gen.sleep(0.05) raise gen.Return('hello') server.listen() # Make a call: tchannel = TChannel(name='client') # timeout is less than server, should timeout with pytest.raises(TimeoutError): yield tchannel.call( scheme=schemes.RAW, service='server', arg1='endpoint', hostport=server.hostport, timeout=0.02, ) # timeout is more than server, should not timeout yield tchannel.raw( service='server', endpoint='endpoint', hostport=server.hostport, timeout=0.1, )
def test_headers_and_body_should_be_optional(): # Given this test server: server = TChannel(name='server') @server.register(scheme=schemes.RAW) def endpoint(request): # assert request.headers is None # TODO uncomment # assert request.body is None # TODO uncomment pass server.listen() # Make a call: tchannel = TChannel(name='client') resp = yield tchannel.call( scheme=schemes.RAW, service='server', arg1='endpoint', hostport=server.hostport, ) # verify response assert isinstance(resp, Response) assert resp.headers == '' # TODO should be None to match server assert resp.body == '' # TODO should be None to match server
def test_error_trace(): tchannel = TChannel('test') class ErrorEventHook(EventHook): def __init__(self): self.request_trace = None self.error_trace = None def before_receive_request(self, request): self.request_trace = request.tracing def after_send_error(self, error): self.error_trace = error.tracing hook = ErrorEventHook() tchannel.hooks.register(hook) tchannel.listen() with pytest.raises(BadRequestError): yield tchannel.call( scheme=schemes.RAW, service='test', arg1='endpoint', hostport=tchannel.hostport, timeout=0.02, ) assert hook.error_trace assert hook.request_trace assert hook.error_trace == hook.request_trace
def test_after_send_error_event_called(): tchannel = TChannel('test') tchannel.listen() with mock.patch( 'tchannel.event.EventEmitter.fire', autospec=True, ) as mock_fire: mock_fire.return_value = None with pytest.raises(BadRequestError): yield tchannel.call( scheme=schemes.RAW, service='test', arg1='endpoint', hostport=tchannel.hostport, timeout=0.3, ) mock_fire.assert_any_call( mock.ANY, EventType.after_send_error, mock.ANY, )
def test_endpoint_can_be_called_as_a_pure_func(): # Given this test server: server = TChannel(name='server') @server.register(scheme=schemes.RAW) def endpoint(request): assert isinstance(request, Request) assert request.body == 'req body' assert request.headers == 'req headers' return Response('resp body', headers='resp headers') server.listen() # Able to call over TChannel tchannel = TChannel(name='client') resp = yield tchannel.call( scheme=schemes.RAW, service='server', arg1='endpoint', arg2='req headers', arg3='req body', hostport=server.hostport, ) assert isinstance(resp, Response) assert resp.headers == 'resp headers' assert resp.body == 'resp body' # Able to call as function resp = endpoint(Request('req body', headers='req headers')) assert isinstance(resp, Response) assert resp.headers == 'resp headers' assert resp.body == 'resp body'
def test_response_status_is_copied(): server = TChannel(name='server') server.listen() @server.register(TChannel.FALLBACK) def handler(request): return Response( status=1, headers=b'\x00\x00', body=b'\x00', ) client = TChannel(name='client', known_peers=[server.hostport]) response = yield client.call( scheme='thrift', service='server', arg1='hello', arg2=b'\x00\x00', arg3=b'\x00', ) assert 1 == response.status
def test_context_should_carry_tracing_info(): context = [None, None] server = TChannel(name='server') @server.register(scheme=schemes.RAW) @gen.coroutine def endpoint1(request): yield server.call( scheme=schemes.RAW, service='server', arg1='endpoint2', arg2='req headers', arg3='req body', hostport=server.hostport, ) context[0] = get_current_context() raise gen.Return(Response('resp body', 'resp headers')) @server.register(scheme=schemes.RAW) def endpoint2(request): context[1] = get_current_context() return Response('resp body', 'resp headers') server.listen() # Make a call: tchannel = TChannel(name='client') yield tchannel.call( scheme=schemes.RAW, service='server', arg1='endpoint1', arg2='req headers', arg3='req body', hostport=server.hostport, ) assert context[0].parent_tracing.name == 'endpoint1' assert context[1].parent_tracing.name == 'endpoint2'
def test_call_should_get_response(): # Given this test server: server = TChannel(name='server') @server.register(scheme=schemes.RAW) def endpoint(request): assert isinstance(request, Request) assert request.headers == 'req headers' assert request.body == 'req body' return Response('resp body', 'resp headers') server.listen() # Make a call: tchannel = TChannel(name='client') resp = yield tchannel.call( scheme=schemes.RAW, service='server', arg1='endpoint', arg2='req headers', arg3='req body', hostport=server.hostport, ) # verify response assert isinstance(resp, Response) assert resp.headers == 'resp headers' assert resp.body == 'resp body' # verify response transport headers assert isinstance(resp.transport, TransportHeaders) assert resp.transport.scheme == schemes.RAW assert resp.transport.failure_domain is None