def test_void(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testVoid(request): pass server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) resp = yield tchannel.thrift(service.testVoid()) assert resp.headers == {} assert resp.body is None
def test_enum(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testEnum(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) x = ThriftTest.Numberz.FIVE resp = yield tchannel.thrift( service.testEnum(thing=x) ) assert resp.headers == {} assert resp.body == x
def test_map(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testMap(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) x = { 0: 1, 1: 2, 2: 3, 3: 4, -1: -2, } resp = yield tchannel.thrift( service.testMap(thing=x) ) assert resp.headers == {} assert resp.body == x
def test_string_map(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testStringMap(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) x = { 'hello': 'there', 'my': 'name', 'is': 'shirly', } resp = yield tchannel.thrift( service.testStringMap(thing=x) ) assert resp.headers == {} assert resp.body == x
def test_binary(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testBinary(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) resp = yield tchannel.thrift( service.testBinary( # this is ThriftTest.Xtruct(string_thing='hi') '\x0c\x00\x00\x0b\x00\x01\x00\x00\x00\x0bhi\x00\x00' ) ) assert resp.headers == {} assert ( resp.body == '\x0c\x00\x00\x0b\x00\x01\x00\x00\x00\x0bhi\x00\x00' )
def test_struct(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testStruct(request): assert request.body.thing.string_thing == 'req string' return ThriftTest.Xtruct( string_thing="resp string" ) server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='service', thrift_module=ThriftTest, hostport=server.hostport ) resp = yield tchannel.thrift( service.testStruct(ThriftTest.Xtruct("req string")) ) # verify response assert isinstance(resp, Response) assert resp.headers == {} assert resp.body == ThriftTest.Xtruct("resp string")
def test_i32(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testI32(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) # case #1 resp = yield tchannel.thrift( service.testI32(-1) ) assert resp.headers == {} assert resp.body == -1 # case #2 resp = yield tchannel.thrift( service.testI32(1) ) assert resp.headers == {} assert resp.body == 1
def test_double(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testDouble(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) resp = yield tchannel.thrift( service.testDouble(-5.235098235) ) assert resp.headers == {} assert resp.body == -5.235098235
def test_client_for_with_sync_tchannel(): server = TChannel(name='server') @server.thrift.register(ThriftTest) def testString(request): return request.body.thing.encode('rot13') server.listen() tchannel = SyncTChannel(name='client') client = sync_client_for('server', ThriftTest)( tchannel=tchannel, hostport=server.hostport, ) future = client.testString(thing='foo') assert not isinstance(future, concurrent.Future) # Our server is sharing our IO loop so let it handle the # request. while not future.done(): yield gen.moment resp = future.result() assert resp == 'sbb'
def test_value_expected_but_none_returned_should_error(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testString(request): pass server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) with pytest.raises(ValueExpectedError): yield tchannel.thrift( service.testString('no return!?') )
def test_call_unexpected_error_should_result_in_unexpected_error(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testMultiException(request): raise Exception('well, this is unfortunate') server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) with pytest.raises(UnexpectedError): yield tchannel.thrift( service.testMultiException(arg0='Xception', arg1='thingy') )
def test_type_def(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testTypedef(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) x = 0xffffffffffffff # 7 bytes of 0xff resp = yield tchannel.thrift( service.testTypedef(thing=x) ) assert resp.headers == {} assert resp.body == x
def test_call_unexpected_error_should_result_in_unexpected_error(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testMultiException(request): raise Exception('well, this is unfortunate') server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) with pytest.raises(UnexpectedError): yield tchannel.thrift( service.testMultiException(arg0='Xception', arg1='thingy'))
def test_writer_multiplexing(): server = TChannel('server') server.listen() received = {'chunked': False, 'singleframe': False} @server.raw.register('chunked') def chunked(request): received['chunked'] = True return b'chunked' @server.raw.register('singleframe') def singleframe(request): received['singleframe'] = True return b'singleframe' client = TChannel('client') chunked_future = client.raw( 'server', 'chunked', bytes([0x00] * 1024 * 1024), # 1 MB = 16 frames hostport=server.hostport, timeout=0.5, ) yield client.raw( 'server', 'singleframe', b'\x00', # single frame hostport=server.hostport, ) assert received['singleframe'] assert not received['chunked'] yield chunked_future assert received['chunked']
def test_roundtrip(benchmark): loop = ioloop.IOLoop.current() server = TChannel('benchmark-server') server.listen() clients = [TChannel('benchmark-client') for _ in range(10)] @server.thrift.register(service.KeyValue) def getValue(request): return 'bar' def roundtrip(): @gen.coroutine def doit(): futures = [] # 10 clients send 10 requests concurrently for client in clients: for _ in range(10): futures.append( client.thrift( service.KeyValue.getValue("foo"), hostport=server.hostport, ) ) yield futures return loop.run_sync(doit) # Establish initial connection roundtrip() benchmark(roundtrip)
def test_oneway(): # Given this test server: server = TChannel(name='server') # TODO - server should raise same exception as client with pytest.raises(AssertionError): @server.thrift.register(ThriftTest) def testOneway(request): pass server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) with pytest.raises(OneWayNotSupportedError): yield tchannel.thrift(service.testOneway(1))
def proxy_server(keyvalue_server): server = TChannel(name='keyvalue-proxy') server.listen() # The client that the proxy uses to make requests should be a different # TChannel. That's because TChannel treats all peers (incoming and # outgoing) as the same. So, if the server receives a request and then # uses the same channel to make the request, there's a chance that it gets # forwarded back to the peer that originally made the request. # # This is desirable behavior because we do want to treat all Hyperbahn # nodes as equal. proxy_server_client = TChannel( name='proxy-client', known_peers=[keyvalue_server.hostport], ) @server.register(TChannel.FALLBACK) @gen.coroutine def handler(request): response = yield proxy_server_client.call( scheme=request.transport.scheme, service=request.service, arg1=request.endpoint, arg2=request.headers, arg3=request.body, timeout=request.timeout / 2, retry_on=request.transport.retry_flags, retry_limit=0, shard_key=request.transport.shard_key, routing_delegate=request.transport.routing_delegate, ) raise gen.Return(response) return server
def test_void_with_headers(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testVoid(request): assert request.headers == {'req': 'header'} return Response(headers={'resp': 'header'}) server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) resp = yield tchannel.thrift( service.testVoid(), headers={'req': 'header'}, ) assert resp.headers == { 'resp': 'header' } assert resp.body is None
def test_type_def(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testTypedef(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) x = 0xffffffffffffff # 7 bytes of 0xff resp = yield tchannel.thrift(service.testTypedef(thing=x)) assert resp.headers == {} assert resp.body == x
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_map(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testMap(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) x = { 0: 1, 1: 2, 2: 3, 3: 4, -1: -2, } resp = yield tchannel.thrift(service.testMap(thing=x)) assert resp.headers == {} assert resp.body == x
def test_tracing_field_in_error_message(): 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=1.0, # used to be 0.02, but was unstable in Travis ) assert hook.error_trace assert hook.request_trace assert hook.error_trace == hook.request_trace
def test_service_inheritance(tmpdir): path = tmpdir.join('myservice.thrift') path.write(''' service Base { bool healthy() } service MyService extends Base { i32 getValue() } ''') service = thrift.load(str(path), service='myservice') server = TChannel('server') server.listen() @server.thrift.register(service.MyService) def getValue(request): return 42 @server.thrift.register(service.MyService) def healthy(request): return True client = TChannel('client', known_peers=[server.hostport]) response = yield client.thrift(service.MyService.getValue()) assert response.body == 42 response = yield client.thrift(service.MyService.healthy()) assert response.body is True
def test_client_connection_change_callback(): server = TChannel('server') server.listen() @server.raw.register def hello(request): return 'hi' client = TChannel('client') count = [0] def test_cb(peer): count[0] += 1 client._dep_tchannel.peers.get( server.hostport)._on_conn_change_cb = test_cb yield client.raw( hostport=server.hostport, body='work', endpoint='hello', service='server' ) # 1: connection built, 1: sending request, 1: finish sending request assert count[0] == 3
def test_local_timeout_unconsumed_message(): """Verify that if the client has a local timeout and the server eventually sends the message, the client does not log an "Unconsumed message" warning. """ server = TChannel('server') @server.raw.register('hello') @gen.coroutine def hello(request): yield gen.sleep(0.07) raise gen.Return('eventual response') server.listen() client = TChannel('client') with pytest.raises(TimeoutError): yield client.raw( 'server', 'hello', 'world', timeout=0.05, hostport=server.hostport, # The server will take 70 milliseconds but we allow at most 50. ) # Wait for the server to send the late response and make sure it doesn't # log a warning. with mock.patch.object(connection.log, 'warn') as mock_warn: # :( yield gen.sleep(0.03) assert mock_warn.call_count == 0
def test_service_inheritance_with_import(tmpdir): tmpdir.join('shared/base.thrift').write( 'service Base { bool healthy() }', ensure=True ) inherited = tmpdir.join('myservice.thrift') inherited.write(''' include "./shared/base.thrift" service MyService extends base.Base { i32 getValue() } ''') service = thrift.load(str(inherited), service='myservice') server = TChannel('server') @server.thrift.register(service.MyService) def getValue(request): return 42 @server.thrift.register(service.MyService) def healthy(request): return True server.listen() client = TChannel('client', known_peers=[server.hostport]) response = yield client.thrift(service.MyService.getValue()) assert response.body == 42 response = yield client.thrift(service.MyService.healthy()) assert response.body is True
def keyvalue_server(io_loop, keyvalue, keyvalue_data): server = TChannel(name='keyvalue') server.listen() @server.thrift.register(keyvalue.KeyValue) def getItem(request): assert request.service == 'keyvalue' key = request.body.key if key in keyvalue_data: assert request.headers == {'expect': 'success'} return keyvalue_data[key] else: assert request.headers == {'expect': 'failure'} raise keyvalue.ItemDoesNotExist(key) @server.json.register('putItem') def json_put_item(request): assert request.service == 'keyvalue' assert request.timeout == 0.5 key = request.body['key'] value = request.body['value'] keyvalue_data[key] = value return {'success': True} return server
def test_service_inheritance_with_import(tmpdir): tmpdir.join('shared/base.thrift').write('service Base { bool healthy() }', ensure=True) inherited = tmpdir.join('myservice.thrift') inherited.write(''' include "./shared/base.thrift" service MyService extends base.Base { i32 getValue() } ''') service = thrift.load(str(inherited), service='myservice') server = TChannel('server') @server.thrift.register(service.MyService) def getValue(request): return 42 @server.thrift.register(service.MyService) def healthy(request): return True server.listen() client = TChannel('client', known_peers=[server.hostport]) response = yield client.thrift(service.MyService.getValue()) assert response.body == 42 response = yield client.thrift(service.MyService.healthy()) assert response.body is True
def test_roundtrip(benchmark): loop = ioloop.IOLoop.current() server = TChannel('benchmark-server') server.listen() clients = [TChannel('benchmark-client') for _ in range(10)] @server.thrift.register(service.KeyValue) def getValue(request): return 'bar' def roundtrip(): @gen.coroutine def doit(): futures = [] # 10 clients send 10 requests concurrently for client in clients: for _ in range(10): futures.append( client.thrift( service.KeyValue.getValue("foo"), hostport=server.hostport, )) yield futures return loop.run_sync(doit) # Establish initial connection roundtrip() benchmark(roundtrip)
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_endpoint_can_return_just_body(): # Given this test server: server = TChannel(name='server') @server.json.register def endpoint(request): return {'resp': 'body'} server.listen() # Make a call: tchannel = TChannel(name='client') resp = yield tchannel.json( service='server', endpoint='endpoint', hostport=server.hostport, ) # verify response assert isinstance(resp, Response) assert resp.body == {'resp': 'body'}
def test_string_map(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testStringMap(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) x = { 'hello': 'there', 'my': 'name', 'is': 'shirly', } resp = yield tchannel.thrift(service.testStringMap(thing=x)) assert resp.headers == {} assert resp.body == x
def test_enum(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testEnum(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) x = ThriftTest.Numberz.FIVE resp = yield tchannel.thrift(service.testEnum(thing=x)) assert resp.headers == {} assert resp.body == x
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_void_with_headers(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testVoid(request): assert request.headers == {'req': 'header'} return Response(headers={'resp': 'header'}) server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) resp = yield tchannel.thrift( service.testVoid(), headers={'req': 'header'}, ) assert resp.headers == {'resp': 'header'} assert resp.body is None
def test_call_response_should_contain_transport_headers(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testString(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) resp = yield tchannel.thrift(service.testString('hi')) # verify response assert isinstance(resp, Response) assert resp.headers == {} assert resp.body == 'hi' # verify response transport headers assert isinstance(resp.transport, TransportHeaders) assert resp.transport.scheme == schemes.THRIFT assert resp.transport.failure_domain is None
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_i32(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testI32(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) # case #1 resp = yield tchannel.thrift(service.testI32(-1)) assert resp.headers == {} assert resp.body == -1 # case #2 resp = yield tchannel.thrift(service.testI32(1)) assert resp.headers == {} assert resp.body == 1
def test_inherited_method_names(tmpdir): thrift_file = tmpdir.join('service.thrift') thrift_file.write(''' service Base { string hello() } service Foo extends Base {} service Bar extends Base {} ''') service = thrift.load(str(thrift_file), 'myservice') server = TChannel('server') @server.thrift.register(service.Foo, method='hello') def foo_hello(request): return 'foo' @server.thrift.register(service.Bar, method='hello') def bar_hello(request): return 'bar' server.listen() client = TChannel('client') res = yield client.thrift(service.Foo.hello(), hostport=server.hostport) assert res.body == 'foo' res = yield client.thrift(service.Bar.hello(), hostport=server.hostport) assert res.body == 'bar'
def test_double(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testDouble(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) resp = yield tchannel.thrift(service.testDouble(-5.235098235)) assert resp.headers == {} assert resp.body == -5.235098235
def test_binary(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testBinary(request): return request.body.thing server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) resp = yield tchannel.thrift( service.testBinary( # this is ThriftTest.Xtruct(string_thing='hi') '\x0c\x00\x00\x0b\x00\x01\x00\x00\x00\x0bhi\x00\x00')) assert resp.headers == {} assert (resp.body == '\x0c\x00\x00\x0b\x00\x01\x00\x00\x00\x0bhi\x00\x00')
def test_struct(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testStruct(request): assert request.body.thing.string_thing == 'req string' return ThriftTest.Xtruct(string_thing="resp string") server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder(service='service', thrift_module=ThriftTest, hostport=server.hostport) resp = yield tchannel.thrift( service.testStruct(ThriftTest.Xtruct("req string"))) # verify response assert isinstance(resp, Response) assert resp.headers == {} assert resp.body == ThriftTest.Xtruct("resp string")
def test_insanity(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testInsanity(request): result = { 1: { 2: request.body.argument, 3: request.body.argument, }, 2: { 6: ThriftTest.Insanity(), }, } return result server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) x = ThriftTest.Insanity( userMap={ ThriftTest.Numberz.EIGHT: 0xffffffffffffff, }, xtructs=[ ThriftTest.Xtruct( string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=-34359738368, ), ], ) resp = yield tchannel.thrift( service.testInsanity(x) ) assert resp.headers == {} assert resp.body == { 1: { 2: x, 3: x, }, 2: { 6: ThriftTest.Insanity(), }, }
def test_parse_host_port(): tchannel = TChannel('test') tchannel.listen() tracer = TChannelZipkinTracer(tchannel) host_span = tracer.parse_host_port() assert tchannel.hostport == "%s:%d" % (host_span.ipv4, host_span.port) assert tchannel.name == host_span.service_name
def test_hostport_gets_set(): tchannel = TChannel(name='holler') tchannel.listen() host, port = tchannel.hostport.split(':') assert tchannel.host == host assert tchannel.port == int(port)
def test_no_infinite_trace_submit(): """Zipkin submissions must not trace themselves.""" def submit(request): return TResponse(True) zipkin_server = TChannel('zipkin') zipkin_server.thrift.register(TCollector, handler=submit) zipkin_server.listen() class TestTraceHook(EventHook): def __init__(self): self.tracings = [] def before_send_request(self, request): # if request.service == 'tcollector': self.tracings.append(request.tracing) server = TChannel('server', known_peers=[zipkin_server.hostport]) server.hooks.register(ZipkinTraceHook(tchannel=server, sample_rate=1)) test_trace_hook = TestTraceHook() server.hooks.register(test_trace_hook) server.thrift.register(TCollector, handler=submit) @server.raw.register @tornado.gen.coroutine def hello(request): if request.body == 'body': yield server.raw( service='server', endpoint='hello', body='boy', hostport=server.hostport, trace=True, ) raise tornado.gen.Return('hello') server.listen() client = TChannel('client') client.thrift.register(TCollector, handler=submit) yield client.raw( service='server', endpoint='hello', hostport=server.hostport, body='body', trace=True, ) # Continue yielding to the IO loop to allow our zipkin traces to be # handled. for _ in xrange(100): yield tornado.gen.moment # One trace for 'hello' and then 3 submissions to tcollector (1 time as # client, 2 times as server) assert len(test_trace_hook.tracings) == 4, test_trace_hook.tracings
def test_insanity(): # Given this test server: server = TChannel(name='server') @server.thrift.register(ThriftTest) def testInsanity(request): result = { 1: { 2: request.body.argument, 3: request.body.argument, }, 2: { 6: ThriftTest.Insanity(), }, } return result server.listen() # Make a call: tchannel = TChannel(name='client') service = thrift_request_builder( service='server', thrift_module=ThriftTest, hostport=server.hostport, ) x = ThriftTest.Insanity( userMap={ ThriftTest.Numberz.EIGHT: 0xffffffffffffff, }, xtructs=[ ThriftTest.Xtruct( string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=-34359738368, ), ], ) resp = yield tchannel.thrift(service.testInsanity(x)) assert resp.headers == {} assert resp.body == { 1: { 2: x, 3: x, }, 2: { 6: ThriftTest.Insanity(), }, }
def test_get_rank_with_outgoing(): server = TChannel('server') server.listen() connection = yield TornadoConnection.outgoing(server.hostport) peer = Peer(TChannel('test'), '10.10.101.21:230') calculator = PreferIncomingCalculator() peer.register_outgoing_conn(connection) assert PreferIncomingCalculator.TIERS[1] == calculator.get_rank(peer)
def test_get_rank_with_imcoming(): server = TChannel('server') server.listen() connection = yield TornadoConnection.outgoing(server.hostport) connection.direction = INCOMING peer = Peer(TChannel('test'), '10.10.101.21:230') calculator = PreferIncomingCalculator() peer.register_incoming_conn(connection) assert sys.maxint != calculator.get_rank(peer)