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_zipkin_trace(trace_server): endpoint = b'endpoint1' zipkin_tracer = ZipkinTraceHook(dst=trace_buf) tchannel = TChannel(name='test') tchannel.hooks.register(zipkin_tracer) hostport = 'localhost:%d' % trace_server.port response = yield tchannel.request(hostport).send(InMemStream(endpoint), InMemStream(hostport), InMemStream(), traceflag=True) header = yield response.get_header() body = yield response.get_body() assert header == "from handler1" assert body == "from handler2" traces = [] for trace in trace_buf.getvalue().split("\n"): if trace: traces.append(json.loads(trace)) trace_id = traces[0][0][u'trace_id'] for trace in traces: assert trace_id == trace[0][u'trace_id'] if trace[0][u'name'] == u'endpoint2': parent_span_id = trace[0][u'parent_span_id'] else: span_id = trace[0][u'span_id'] assert parent_span_id == span_id
def call(mock_server, use_old_api): if use_old_api: from tchannel.tornado import TChannel channel = TChannel('test-client') def old_f(endpoint, body, headers=None, service=None, scheme=None): return channel.request( hostport=mock_server.hostport, service=service, arg_scheme=scheme, ).send(endpoint, headers or '', body) return old_f else: from tchannel import TChannel channel = TChannel('test-client') def new_f(endpoint, body, headers=None, service=None, scheme=None): scheme = scheme or 'raw' return channel.call( hostport=mock_server.hostport, scheme=scheme, service=service, arg1=endpoint, arg2=headers or '', arg3=body, ) return new_f
def test_retry_on_error_success(): endpoint = b'tchannelretrytest' tchannel = chain(2, endpoint) tchannel_success = TChannel(name='test', hostport='localhost:0') tchannel_success.register(endpoint, 'raw', handler_success) tchannel_success.listen() tchannel.peers.get(tchannel_success.hostport) with ( patch( 'tchannel.tornado.Request.should_retry_on_error', autospec=True) ) as mock_should_retry_on_error: mock_should_retry_on_error.return_value = True response = yield tchannel.request( score_threshold=0 ).send( endpoint, "test", "test", headers={ 're': RetryType.CONNECTION_ERROR_AND_TIMEOUT, }, ttl=0.01, attempt_times=3, retry_delay=0.01, ) header = yield response.get_header() body = yield response.get_body() assert body == "success" assert header == ""
def test_retry_on_error_success(mock_should_retry_on_error): mock_should_retry_on_error.return_value = True endpoint = b'tchannelretrytest' tchannel = yield chain(2, endpoint) hook = MyTestHook() tchannel.hooks.register(hook) tchannel_success = TChannel(name='test', hostport='localhost:0') tchannel_success.register(endpoint, 'raw', handler_success) tchannel_success.listen() tchannel.peers.get(tchannel_success.hostport) response = yield tchannel.request().send( endpoint, "test", "test", headers={ 're': retry.CONNECTION_ERROR_AND_TIMEOUT, }, ttl=1, retry_limit=2, ) header = yield response.get_header() body = yield response.get_body() assert body == "success" assert header == "" assert hook.received_response == 1 assert hook.received_error == 2
def main(): # pragma: no cover import sys parser = argparse.ArgumentParser() parser.add_argument( "--port", dest="port", default=8888, type=int, ) parser.add_argument( "--host", dest="host", default="127.0.0.1" ) args = parser.parse_args() client = TChannel('%s:%d' % (args.host, args.port)) # TODO: service=test_as_raw handler = RequestDispatcher() client.host(handler) # TODO: do we need to implement these differently? handler.register("echo", echo) handler.register("streaming_echo", echo) client.listen() print 'listening on', client.hostport sys.stdout.flush() tornado.ioloop.IOLoop.instance().start()
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_request(): channel = TChannel(name="test") hyperbahn.advertise(channel, "foo", ["127.0.0.1:23000"]) # Just want to make sure all the plumbing fits together. with pytest.raises(NoAvailablePeerError): yield channel.request(service="bar").send(arg1="baz", arg2="bam", arg3="boo", headers={"as": "qux"})
def main(): # pragma: no cover args = get_args() client = TChannel('localhost:%d' % args.port) register_example_endpoints(client) client.listen() tornado.ioloop.IOLoop.instance().start()
def test_endpoint_not_found(mock_server): endpoint = b'tchanneltest' mock_server.expect_call(endpoint).and_write(headers=endpoint, body='world') tchannel = TChannel(name='test') with pytest.raises(TChannelError): yield tchannel.request(mock_server.hostport).send( InMemStream(), InMemStream(), InMemStream())
def __init__(self, handle): self.tchannel = TChannel(name='hyperbahn') self.count = 0 # number of ad requests received @self.tchannel.register('ad', 'json') @gen.coroutine def ad(request, response): self.count += 1 yield handle(request, response)
def test_endpoint_not_found(tchannel_server): endpoint = b"tchanneltest" tchannel_server.expect_call(endpoint).and_write(headers=endpoint, body="world") tchannel = TChannel(name="test") hostport = "localhost:%d" % (tchannel_server.port) with pytest.raises(TChannelError): yield tchannel.request(hostport).send(InMemStream(), InMemStream(), InMemStream())
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)
def send_stream(arg1, arg2, arg3, host): tchannel = TChannel() response = yield tchannel.request(host).send( arg1, arg2, arg3) yield print_arg(response, 0) yield print_arg(response, 1) yield print_arg(response, 2)
def send_stream(arg1, arg2, arg3, host): tchannel = TChannel( name='stream-client', ) yield tchannel.request(host).send( arg1, arg2, arg3, )
def _run(self): self.io_loop = IOLoop() self.io_loop.make_current() self.tchannel = TChannel('proxy-server') self.tchannel.register(VCRProxy, handler=self.send) self.tchannel.listen() self._running.set() self.io_loop.start()
def test_endpoint_not_found(mock_server): endpoint = b'tchanneltest' mock_server.expect_call(endpoint).and_write( headers=endpoint, body='world' ) tchannel = TChannel(name='test') with pytest.raises(TChannelError): yield tchannel.request( mock_server.hostport ).send(InMemStream(), InMemStream(), InMemStream())
def test_request(): channel = TChannel() hyperbahn.advertise(channel, 'foo', ['127.0.0.1:23000']) # Just want to make sure all the plumbing fits together. with pytest.raises(ConnectionClosedError): yield channel.request(service='bar').send( arg1='baz', arg2='bam', arg3='boo', headers={'as': 'qux'}, )
def main(): # pragma: no cover args = get_args() client = TChannel( name='tchannel_server', hostport='%s:%d' % (args.host, args.port), ) register_example_endpoints(client) client.listen() tornado.ioloop.IOLoop.instance().start()
def __init__(self, cassette, unpatch): """ :param unpatch: A function returning a context manager which temporarily unpatches any monkey patched code so that a real request can be made. :param cassette: Cassette being played. """ self.unpatch = unpatch self.cassette = cassette self.tchannel = TChannel('proxy-server') self.tchannel.register(VCRProxy, handler=self.send)
def main(): args = get_args() tchannel = TChannel(name="raw-client") request = tchannel.request(hostport="%s:%s" % (args.host, args.port)) response = yield request.send("hi", None, None) body = yield response.get_body() print body
def test_request(): channel = TChannel(name='test') hyperbahn.advertise(channel, 'foo', ['127.0.0.1:23000']) # Just want to make sure all the plumbing fits together. with pytest.raises(ConnectionClosedError): yield channel.request(service='bar').send( arg1='baz', arg2='bam', arg3='boo', headers={'as': 'qux'}, )
def test_tchannel_call_request_fragment(mock_server, arg2, arg3): endpoint = b'tchannelpeertest' mock_server.expect_call(endpoint).and_write(headers=endpoint, body=arg3) tchannel = TChannel(name='test') response = yield tchannel.request(mock_server.hostport).send( InMemStream(endpoint), InMemStream(arg2), InMemStream(arg3)) header = yield response.get_header() body = yield response.get_body() assert header == endpoint assert body == arg3 assert response.headers['as'] == 'raw'
def test_endpoint_not_found(tchannel_server): endpoint = b'tchanneltest' tchannel_server.expect_call(endpoint).and_write( headers=endpoint, body='world' ) tchannel = TChannel() hostport = 'localhost:%d' % (tchannel_server.port) with pytest.raises(TChannelError): yield tchannel.request(hostport).send(InMemStream(), InMemStream(), InMemStream())
def test_tchannel_call_request_fragment(tchannel_server, arg2, arg3): endpoint = b"tchannelpeertest" tchannel_server.expect_call(endpoint).and_write(headers=endpoint, body=arg3) tchannel = TChannel(name="test") hostport = "localhost:%d" % (tchannel_server.port) response = yield tchannel.request(hostport).send(InMemStream(endpoint), InMemStream(arg2), InMemStream(arg3)) header = yield response.get_header() body = yield response.get_body() assert header == endpoint assert body == arg3 assert response.headers["as"] == "raw"
def test_advertise(): server = TChannel(name="test_server") @server.register('ad', 'json') @tornado.gen.coroutine def ad(request, response): body = yield request.get_body() response.write_body(body) server.listen() channel = TChannel(name='test') response = yield hyperbahn.advertise(channel, 'test', [server.hostport]) result = yield response.get_body() assert (result == '{"services": [{"serviceName": "test", "cost": 0}]}' or result == '{"services": [{"cost": 0, "serviceName": "test"}]}')
def chain(number_of_peers, endpoint): tchannel = TChannel(name='test') for i in range(number_of_peers): # Connect to these peers so that they are treated as higher priority # than other peers. yield tchannel.peers.get(server(endpoint).hostport).connect() raise tornado.gen.Return(tchannel)
def thrift_client(thrift_service, mock_server, use_old_api): if use_old_api: from tchannel.tornado import TChannel return client_for('myservice', thrift_service)( tchannel=TChannel('thrift-client'), hostport=mock_server.hostport, ) else: from tchannel import TChannel from tchannel.thrift import thrift_request_builder myservice = thrift_request_builder('myservice', thrift_service, hostport=mock_server.hostport) return mk_fake_client(TChannel('thrift-client'), myservice)
def test_advertiser_intermediate_failure(): @gen.coroutine def handle(request, response): body = yield request.get_body() if hb.count == 2: # fail the second request only raise Exception('great sadness') response.write_body(body) hb = Fakebahn(handle) try: hb.start() adv = hyperbahn.Advertiser( 'foo', TChannel('foo', known_peers=[hb.hostport]), interval_secs=0.2, interval_max_jitter_secs=0.0, ) yield adv.start() assert 1 == hb.count yield gen.sleep(0.25) assert 2 == hb.count yield gen.sleep(0.25) assert 3 == hb.count finally: hb.stop()
def main(): args = get_args() app = TChannel( name='raw-server', hostport='%s:%d' % (args.host, args.port), ) register_example_endpoints(app) app.listen() print("listening on %s" % app.hostport) sys.stdout.flush() tornado.ioloop.IOLoop.instance().start()
def test_advertiser_fail(): @gen.coroutine def fail(request, response): body = yield request.get_body() if hb.count == 1: # fail only the first request response.status_code = 1 response.write_body(body) hb = Fakebahn(fail) try: hb.start() adv = hyperbahn.Advertiser( 'foo', TChannel('foo', known_peers=[hb.hostport]), interval_secs=0.2, interval_max_jitter_secs=0.0, ) yield adv.start() assert 1 == hb.count yield gen.sleep(0.25) assert 2 == hb.count finally: hb.stop()
def test_advertise(echobahn): channel = TChannel(name='test') response = yield hyperbahn.advertise(channel, 'test', [echobahn.hostport]) result = yield response.get_body() assert (result == '{"services": [{"serviceName": "test", "cost": 0}]}' or result == '{"services": [{"cost": 0, "serviceName": "test"}]}')
def test_tchannel_call_request_fragment(mock_server, arg2, arg3): endpoint = b'tchannelpeertest' mock_server.expect_call(endpoint).and_write( headers=endpoint, body=arg3 ) tchannel = TChannel(name='test') response = yield tchannel.request(mock_server.hostport).send( InMemStream(endpoint), InMemStream(arg2), InMemStream(arg3) ) header = yield response.get_header() body = yield response.get_body() assert header == endpoint assert body == arg3 assert response.headers['as'] == 'raw'
def test_old_recording_without_tracing(mock_server, tracer): from tchannel import TChannel # an existing recording that does not contain tracing information path = os.path.join(os.path.dirname(__file__), 'data', 'old_without_tracing.yaml') ch = TChannel('client', trace=True, tracer=tracer) mock_server.expect_call('hello', 'json').and_write('world').once() with vcr.use_cassette(path, record_mode=vcr.RecordMode.NONE): response = yield ch.json( hostport=mock_server.hostport, service='hello_service', endpoint='hello', body='world', ) assert 'world' == response.body
def test_advertise_should_raise_on_invalid_router_file(): channel = TChannel(name='client') with pytest.raises(IOError): yield hyperbahn.advertise(channel, 'baz', None, None, '?~~lala') with pytest.raises(ValueError): yield hyperbahn.advertise(channel, 'baz', '?~~lala', None, '?~~lala')
def chain(number_of_peers, endpoint): tchannel = TChannel(name='test') for i in range(number_of_peers): p = tchannel.peers.get(server(endpoint).hostport) # Gaurantee error servers have score in order to pick first. p.state = FakeState() return tchannel
def test_false_result(thrift_service): # Verify that we aren't treating False as None. app = TChannel(name='app') @app.register(thrift_service) def healthy(request, response): return False app.listen() client = TChannel(name='client') response = yield client.request( hostport=app.hostport, arg_scheme='thrift' ).send('Service::healthy', '\x00\x00', '\x00') body = yield response.get_body() assert body == '\x02\x00\x00\x00\x00'
def test_old_recording_without_tracing(mock_server, tracer): from tchannel import TChannel # an existing recording that does not contain tracing information path = os.path.join( os.path.dirname(__file__), 'data', 'old_without_tracing.yaml' ) ch = TChannel('client', trace=True, tracer=tracer) mock_server.expect_call('hello', 'json').and_write('world').once() with vcr.use_cassette(path, record_mode=vcr.RecordMode.NONE): response = yield ch.json( hostport=mock_server.hostport, service='hello_service', endpoint='hello', body='world', ) assert 'world' == response.body
def main(): args = get_args() app = TChannel( name='json-server', hostport='%s:%d' % (args.host, args.port), ) register_example_endpoints(app) def say_hi_json(request, response, proxy): response.write_body({'hi': 'Hello, world!'}) app.register(endpoint="hi-json", scheme="json", handler=say_hi_json) app.listen() tornado.ioloop.IOLoop.instance().start()
def test_advertise(): server = TChannel(name="test_server") @server.register("ad", "json") @tornado.gen.coroutine def ad(request, response): body = yield request.get_body() response.write_body(body) server.listen() channel = TChannel(name="test") response = yield hyperbahn.advertise(channel, "test", [server.hostport]) result = yield response.get_body() assert ( result == '{"services": [{"serviceName": "test", "cost": 0}]}' or result == '{"services": [{"cost": 0, "serviceName": "test"}]}' )
def test_tcollector_submit(trace_server): tchannel = TChannel(name='test', known_peers=[trace_server.hostport]) trace = Trace(endpoint=Endpoint("1.0.0.1", 1111, "tcollector")) anns = [client_send()] results = yield TChannelZipkinTracer(tchannel).record([(trace, anns)]) assert results[0].ok
def test_patching_as_decorator(): chan = TChannel('client') @Patcher('localhost:4040') def f(): ops = chan.request(service='foo') assert isinstance(ops, PatchedClientOperation) assert ops.vcr_hostport == 'localhost:4040' f()
def test_patching_as_decorator(vcr_client): chan = TChannel('client') @Patcher(vcr_client) def f(): ops = chan.request(service='foo') assert isinstance(ops, PatchedClientOperation) assert ops.vcr_client is vcr_client f()
def _run(self): self.io_loop = IOLoop() self.io_loop.make_current() self.tchannel = TChannel("proxy-server") self.tchannel.register(VCRProxy, handler=self.send) self.tchannel.listen() self._running.set() self.io_loop.start()
def test_close_stops_listening(): server = TChannel(name='server') server.listen() host = server.host port = server.port # Can connect sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, port)) sock.close() server.close() # Can't connect sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) with pytest.raises(socket.error): sock.connect((host, port))
def test_advertiser_start_twice(echobahn): adv = hyperbahn.Advertiser( 'foo', TChannel('foo', known_peers=[echobahn.hostport]), ) yield adv.start() with pytest.raises(Exception) as exc_info: yield adv.start() assert 'already running' in str(exc_info) assert 1 == echobahn.count
class Fakebahn(object): def __init__(self, handle): self.tchannel = TChannel(name='hyperbahn') self.count = 0 # number of ad requests received @self.tchannel.register('ad', 'json') @gen.coroutine def ad(request, response): self.count += 1 yield handle(request, response) @property def hostport(self): return self.tchannel.hostport def start(self): self.tchannel.listen() def stop(self): self.tchannel.close()
def call(mock_server): channel = TChannel('test-client') def f(endpoint, body, headers=None, service=None, scheme=None): return channel.request( hostport=mock_server.hostport, service=service, arg_scheme=scheme, ).send(endpoint, headers or '', body) return f
def test_json_server(json_server, sample_json): endpoint = "json_echo" tchannel = TChannel(name='test') hostport = 'localhost:%d' % json_server.port client = tchannel.request(hostport) header = sample_json body = sample_json resp = yield ArgSchemeBroker(JsonArgScheme()).send( client, endpoint, header, body, ) # compare header's json rheader = yield resp.get_header() assert rheader == header # compare body's json rbody = yield resp.get_body() assert rbody == body
def test_retry_on_error_success(): mock_should_retry_on_error = mock.patch('tchannel.tornado.Request.should_retry_on_error') # noqa mock_should_retry_on_error.return_value = True endpoint = 'tchannelretrytest' tchannel = yield chain(2, endpoint) hook = MyTestHook() tchannel.hooks.register(hook) tchannel_success = TChannel(name='test', hostport='localhost:0') tchannel_success.register(endpoint, 'raw', handler_success) tchannel_success.listen() tchannel.peers.get(tchannel_success.hostport) response = yield tchannel.request().send( endpoint, "test", "test", headers={ 're': retry.CONNECTION_ERROR_AND_TIMEOUT, }, ttl=1, retry_limit=2, ) header = yield response.get_header() body = yield response.get_body() assert body == b"success" assert header == b"" assert hook.received_response == 1 assert hook.received_error == 2
def test_vcr_with_tracing(tmpdir, mock_server, tracer, tracing_before, tracing_after): from tchannel import TChannel mock_server.expect_call('hello', 'json').and_write('world').once() path = tmpdir.join('data.yaml') if tracing_before: ch = TChannel('client', trace=True, tracer=tracer) else: ch = TChannel('client') with vcr.use_cassette(str(path)) as cass: response = yield ch.json( hostport=mock_server.hostport, service='hello_service', endpoint='hello', body='world', ) assert 'world' == response.body assert cass.play_count == 0 assert path.check(file=True) if tracing_after: ch = TChannel('client', trace=True, tracer=tracer) else: ch = TChannel('client') with vcr.use_cassette(str(path), record_mode=vcr.RecordMode.NONE) as cass: response = yield ch.json( hostport=mock_server.hostport, service='hello_service', endpoint='hello', body='world', ) assert 'world' == response.body assert cass.play_count == 1
def __init__(self, cassette, unpatch): """ :param unpatch: A function returning a context manager which temporarily unpatches any monkey patched code so that a real request can be made. :param cassette: Cassette being played. """ self.unpatch = unpatch self.cassette = cassette self.tchannel = TChannel("proxy-server") self.tchannel.register(VCRProxy, handler=self.send)
def test_json_server(json_server, sample_json): endpoint = "json_echo" tchannel = TChannel(name='test') client = tchannel.request(json_server.hostport) header = sample_json body = sample_json resp = yield ArgSchemeBroker(JsonArgScheme()).send( client, endpoint, header, body, ) # check protocol header assert resp.headers['as'] == 'json' # compare header's json rheader = yield resp.get_header() assert rheader == header # compare body's json rbody = yield resp.get_body() assert rbody == body
def test_new_client_establishes_peers(): routers = ['127.0.0.1:2300' + str(i) for i in xrange(5)] # TChannel knows about one of the peers already. channel = TChannel('test', known_peers=['127.0.0.1:23002']) hyperbahn.advertise( channel, 'baz', routers, ) for router in routers: assert channel.peers.lookup(router)
def test_new_client_establishes_peers_from_file(): host_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../data/hosts.json', ) # TChannel knows about one of the peers already. channel = TChannel('test', known_peers=['127.0.0.1:23002']) hyperbahn.advertise(channel, 'baz', None, None, host_path) with open(host_path, 'r') as json_data: routers = json.load(json_data) for router in routers: assert channel.peers.lookup(router)
def test_unexpected_error_from_handler(mock_server): # test for invalid call request message tchannel = TChannel(name='test') connection = yield StreamConnection.outgoing( hostport=mock_server.hostport, tchannel=tchannel, ) callrequest = CallRequestMessage(flags=FlagsType.fragment, args=[ 'endpoint1', '', '', ]) # set a wrong checksum callrequest.checksum = (ChecksumType.crc32c, 1) with pytest.raises(ProtocolError): yield connection.send(callrequest)
def test_vcr_with_tracing( tmpdir, mock_server, tracer, tracing_before, tracing_after ): from tchannel import TChannel mock_server.expect_call('hello', 'json').and_write('world').once() path = tmpdir.join('data.yaml') if tracing_before: ch = TChannel('client', trace=True, tracer=tracer) else: ch = TChannel('client') with vcr.use_cassette(str(path)) as cass: response = yield ch.json( hostport=mock_server.hostport, service='hello_service', endpoint='hello', body='world', ) assert 'world' == response.body assert cass.play_count == 0 assert path.check(file=True) if tracing_after: ch = TChannel('client', trace=True, tracer=tracer) else: ch = TChannel('client') with vcr.use_cassette(str(path), record_mode=vcr.RecordMode.NONE) as cass: response = yield ch.json( hostport=mock_server.hostport, service='hello_service', endpoint='hello', body='world', ) assert 'world' == response.body assert cass.play_count == 1