def test_record_success(tmpdir, mock_server): path = tmpdir.join('data.yaml') mock_server.expect_call('hello').and_write('world').once() client = TChannel('test') with vcr.use_cassette(str(path)) as cass: response = client.raw( 'hello_service', 'hello', 'world', hostport=mock_server.hostport, ).result(timeout=1) assert 'world' == response.body assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: response = client.raw( 'hello_service', 'hello', 'world', hostport=mock_server.hostport, ).result(timeout=1) assert 'world' == response.body assert cass.play_count == 1
def test_record_success_no_hostport_new_channels(tmpdir, mock_server): path = tmpdir.join('data.yaml') mock_server.expect_call('hello').and_write('world').once() with vcr.use_cassette(str(path)) as cass: client = TChannel('test', known_peers=[mock_server.hostport]) response = client.raw( 'hello_service', 'hello', 'world', ).result(timeout=1) assert 'world' == response.body assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: client = TChannel('test', known_peers=[mock_server.hostport]) response = client.raw( 'hello_service', 'hello', 'world', ).result(timeout=1) assert 'world' == response.body assert cass.play_count == 1
def test_record_success_thrift(tmpdir, thrift_service, mock_server): myservice = thrift_request_builder('myservice', thrift_service, hostport=mock_server.hostport) path = tmpdir.join('data.yaml') expected_item = thrift_service.Item( 'foo', thrift_service.Value(stringValue='bar')) mock_server.expect_call(thrift_service, method='getItem').and_result(expected_item).once() client = TChannel('test') with vcr.use_cassette(str(path)) as cass: response = client.thrift(myservice.getItem('foo')).result(1) item = response.body assert item == expected_item assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: response = client.thrift(myservice.getItem('foo')).result(1) item = response.body assert item == expected_item assert cass.play_count == 1
def test_record_success_thrift(tmpdir, thrift_service, mock_server): myservice = thrift_request_builder( 'myservice', thrift_service, hostport=mock_server.hostport ) path = tmpdir.join('data.yaml') expected_item = thrift_service.Item( 'foo', thrift_service.Value(stringValue='bar') ) mock_server.expect_call(thrift_service, method='getItem').and_result( expected_item ).once() client = TChannel('test') with vcr.use_cassette(str(path)) as cass: response = client.thrift(myservice.getItem('foo')).result(1) item = response.body assert item == expected_item assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: response = client.thrift(myservice.getItem('foo')).result(1) item = response.body assert item == expected_item assert cass.play_count == 1
def test_call(mock_server, thrift_service): expected = thrift_service.Item( key='foo', value=thrift_service.Value(integerValue=42) ) mock_server.expect_call( thrift_service, 'thrift', method='getItem', ).and_result(expected) thrift_service = thrift_request_builder( service='thrift-service', thrift_module=thrift_service, hostport=mock_server.hostport, ) tchannel = TChannel('test-client') future = tchannel.thrift( thrift_service.getItem('foo') ) result = future.result() assert expected == result.body
def test_all_not_batched(benchmark): from tchannel.sync import TChannel ch = TChannel(name='foo') f = ch.advertise(routers=['127.0.0.1:21300', '127.0.0.1:21301']) f.result() tracer = Tracer.default_tracer(ch, service_name='benchmark', sampler=ConstSampler(True)) tracer.reporter.batch_size = 1 # 250 micros for request execution benchmark(_generate_spans, tracer, sleep=0.00025)
def test_all_not_batched(benchmark): from tchannel.sync import TChannel ch = TChannel(name='foo') f = ch.advertise(routers=["127.0.0.1:21300", "127.0.0.1:21301"]) f.result() tracer = Tracer.default_tracer(ch, service_name='benchmark', sampler=ConstSampler(True)) tracer.reporter.batch_size = 1 # 250 micros for request execution benchmark(_generate_spans, tracer, sleep=0.00025)
def test_failing_advertise_should_raise(mock_server): mock_server.expect_call('ad', 'json').and_raise(Exception('great sadness')) routers = [mock_server.tchannel.hostport] client = TChannel('test-client') with pytest.raises(TimeoutError): future = client.advertise(routers, timeout=0.1) future.result()
def test_sync_register(register_endpoint, make_request): sync_client = TChannel('test-client') register_endpoint(sync_client) sync_client.listen() async_client = AsyncTchannel('async') with pytest.raises(BadRequestError): yield make_request(async_client, sync_client.hostport) with pytest.raises(BadRequestError): yield make_request(sync_client, sync_client.hostport)
def test_failing_advertise_should_raise(mock_server): mock_server.expect_call('ad', 'json').and_raise( Exception('great sadness') ) routers = [mock_server.tchannel.hostport] client = TChannel('test-client') with pytest.raises(TimeoutError): future = client.advertise(routers, timeout=0.1) future.result()
def __init__( self, tchannel, logger, client_name=None, headers={}, timeout_seconds=30, reconfigure_interval_seconds=10, deployment_str='prod', hyperbahn_host='', ): self.logger = logger self.headers = headers self.deployment_str = deployment_str self.headers['user-name'] = util.get_username() self.headers['host-name'] = socket.gethostname() self.timeout_seconds = timeout_seconds self.reconfigure_interval_seconds = reconfigure_interval_seconds if not tchannel: if not client_name: raise Exception( "Client name is needed when tchannel not provided") elif not hyperbahn_host: raise Exception( "Hyperbahn host is needed when tchannel not provided") else: self.tchannel = TChannelSyncClient(name=client_name) self.tchannel.advertise(router_file=hyperbahn_host) else: self.tchannel = tchannel
def test_call(mock_server, thrift_service): expected = thrift_service.Item(key="foo", value=thrift_service.Value(integerValue=42)) mock_server.expect_call(thrift_service, "thrift", method="getItem").and_result(expected) thrift_service = thrift_request_builder( service="thrift-service", thrift_module=thrift_service, hostport=mock_server.hostport ) tchannel = TChannel("test-client") future = tchannel.thrift(thrift_service.getItem("foo")) result = future.result() assert expected == result.body
def test_sync_client_should_get_raw_response(mock_server): endpoint = 'health' mock_server.expect_call(endpoint).and_write(headers="", body="OK") client = TChannel('test-client') future = client.raw( service='foo', hostport=mock_server.hostport, endpoint=endpoint, ) response = future.result() assert response.headers == "" assert response.body == "OK"
def test_sync_client_with_injected_threadloop(mock_server, loop): endpoint = 'health' mock_server.expect_call(endpoint).and_write(headers="", body="OK") client = TChannel('test-client', threadloop=loop) future = client.raw( service='foo', hostport=mock_server.hostport, endpoint=endpoint, ) response = future.result() assert response.headers == "" assert response.body == "OK"
def test_protocol_exception(tmpdir, mock_server): path = tmpdir.join('data.yaml') mock_server.expect_call('hello').and_raise( Exception('great sadness')).once() with pytest.raises(UnexpectedError): with vcr.use_cassette(str(path)): client = TChannel('test') client.raw( 'hello_service', 'hello', 'world', hostport=mock_server.hostport, ).result(1) assert not path.check() # nothing should've been recorded
def test_advertise_should_result_in_peer_connections(mock_server): body = {"hello": "world"} mock_server.expect_call('ad', 'json').and_write( headers="", body=body, ) routers = [mock_server.tchannel.hostport] client = TChannel('test-client') future = client.advertise(routers) result = future.result() assert result.body == body assert client._dep_tchannel.peers.hosts == routers
def test_protocol_exception(tmpdir, mock_server): path = tmpdir.join('data.yaml') mock_server.expect_call('hello').and_raise( Exception('great sadness') ).once() with pytest.raises(UnexpectedError): with vcr.use_cassette(str(path)): client = TChannel('test') client.raw( 'hello_service', 'hello', 'world', hostport=mock_server.hostport, ).result(1) assert not path.check() # nothing should've been recorded
def test_advertise_should_result_in_peer_connections(mock_server): body = {"hello": "world"} mock_server.expect_call('ad', 'json').and_write( headers="", body=body, ) routers = [ mock_server.tchannel.hostport ] client = TChannel('test-client') future = client.advertise(routers) result = future.result() assert result.body == body assert client._dep_tchannel.peers.hosts == routers
def test_sync_client_should_get_raw_response(mock_server): endpoint = 'health' mock_server.expect_call(endpoint).and_write( headers="", body="OK" ) client = TChannel('test-client') future = client.raw( service='foo', hostport=mock_server.hostport, endpoint=endpoint, ) response = future.result() assert response.headers == "" assert response.body == "OK"
def test_sync_client_with_injected_threadloop(mock_server, loop): endpoint = 'health' mock_server.expect_call(endpoint).and_write( headers="", body="OK" ) client = TChannel('test-client', threadloop=loop) future = client.raw( service='foo', hostport=mock_server.hostport, endpoint=endpoint, ) response = future.result() assert response.headers == "" assert response.body == "OK"
def test_record_thrift_exception(tmpdir, mock_server, thrift_service): path = tmpdir.join('data.yaml') myservice = thrift_request_builder( 'myservice', thrift_service, hostport=mock_server.hostport ) mock_server.expect_call(thrift_service, method='getItem').and_raise( thrift_service.ItemDoesNotExist('foo') ).once() client = TChannel('test') with vcr.use_cassette(str(path)) as cass: with pytest.raises(thrift_service.ItemDoesNotExist): client.thrift(myservice.getItem('foo')).result(1) assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: with pytest.raises(thrift_service.ItemDoesNotExist): client.thrift(myservice.getItem('foo')).result(1) assert cass.play_count == 1
def test_call(mock_server, thrift_service): expected = thrift_service.Item(key='foo', value=thrift_service.Value(integerValue=42)) mock_server.expect_call( thrift_service, 'thrift', method='getItem', ).and_result(expected) thrift_service = thrift_request_builder( service='thrift-service', thrift_module=thrift_service, hostport=mock_server.hostport, ) tchannel = TChannel('test-client') future = tchannel.thrift(thrift_service.getItem('foo')) result = future.result() assert expected == result.body
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. from __future__ import absolute_import from __future__ import print_function import json from tchannel import thrift from tchannel.sync import TChannel from six.moves import range tchannel = TChannel('thrift-client') service = thrift.load( path='tests/data/idls/ThriftTest.thrift', service='thrift-server', hostport='localhost:54498', ) def make_requests(): # Fan-out futures = [ tchannel.thrift( request=service.ThriftTest.testString(thing="req"), headers={ 'req': 'header',
class Client(object): tchannel = None headers = None timeout_seconds = 30 # reconfigure_interval_seconds: this parameter controls how frequent we try to get the latest hosts # that are serving the destination or consumer group in our background thread # # deployment_str: controls the deployment the client will connect to. # use 'dev' to connect to dev server # use 'prod' to connect to production # use 'staging' or 'staging2' to connect to staging/staging2 # # hyperbahn_host: the path to the hyperbahn host file # # By default client will connect to cherami via hyperbahn. If you want to connect to cherami via a # specific ip/port(for example during local test), you can create a tchannel object with the ip/port # as known peers # For example: # tchannel = TChannelSyncClient(name='my_service', known_peers=['172.17.0.2:4922']) # client = Client(tchannel, logger) def __init__( self, tchannel, logger, client_name=None, headers={}, timeout_seconds=30, reconfigure_interval_seconds=10, deployment_str='prod', hyperbahn_host='', ): self.logger = logger self.headers = headers self.deployment_str = deployment_str self.headers['user-name'] = util.get_username() self.headers['host-name'] = socket.gethostname() self.timeout_seconds = timeout_seconds self.reconfigure_interval_seconds = reconfigure_interval_seconds if not tchannel: if not client_name: raise Exception( "Client name is needed when tchannel not provided") elif not hyperbahn_host: raise Exception( "Hyperbahn host is needed when tchannel not provided") else: self.tchannel = TChannelSyncClient(name=client_name) self.tchannel.advertise(router_file=hyperbahn_host) else: self.tchannel = tchannel # close the client connection def close(self): pass # create a consumer # Note consumer object should be a singleton # pre_fetch_count: This controls how many messages we can pre-fetch in total # ack_message_buffer_size: This controls the ack messages buffer size.i.e.count of pending ack messages # ack_message_thread_count: This controls how many threads we can have to send ack messages to Cherami. def create_consumer( self, path, consumer_group_name, pre_fetch_count=50, ack_message_buffer_size=50, ack_message_thread_count=4, ): return consumer.Consumer( logger=self.logger, deployment_str=self.deployment_str, path=path, consumer_group_name=consumer_group_name, tchannel=self.tchannel, headers=self.headers, pre_fetch_count=pre_fetch_count, timeout_seconds=self.timeout_seconds, ack_message_buffer_size=ack_message_buffer_size, ack_message_thread_count=ack_message_thread_count, reconfigure_interval_seconds=self.reconfigure_interval_seconds, ) # create a publisher # Note publisher object should be a singleton def create_publisher(self, path): if not path: raise Exception("Path is needed") return publisher.Publisher( logger=self.logger, path=path, deployment_str=self.deployment_str, tchannel=self.tchannel, headers=self.headers, timeout_seconds=self.timeout_seconds, reconfigure_interval_seconds=self.reconfigure_interval_seconds) def create_destination(self, create_destination_request): return util.execute_frontend(self.tchannel, self.deployment_str, self.headers, self.timeout_seconds, 'createDestination', create_destination_request) def read_destination(self, read_destination_request): return util.execute_frontend(self.tchannel, self.deployment_str, self.headers, self.timeout_seconds, 'readDestination', read_destination_request) def create_consumer_group(self, create_consumer_group_request): return util.execute_frontend(self.tchannel, self.deployment_str, self.headers, self.timeout_seconds, 'createConsumerGroup', create_consumer_group_request) def read_consumer_group(self, read_consumer_group_request): return util.execute_frontend(self.tchannel, self.deployment_str, self.headers, self.timeout_seconds, 'readConsumerGroup', read_consumer_group_request) def purge_DLQ_for_consumer_group(self, purge_DLQ_for_consumer_group_request): return util.execute_frontend(self.tchannel, self.deployment_str, self.headers, self.timeout_seconds, 'purgeDLQForConsumerGroup', purge_DLQ_for_consumer_group_request) def merge_DLQ_for_consumer_group(self, merge_DLQ_for_consumer_group_request): return util.execute_frontend(self.tchannel, self.deployment_str, self.headers, self.timeout_seconds, 'mergeDLQForConsumerGroup', merge_DLQ_for_consumer_group_request)
from __future__ import absolute_import, unicode_literals from kombu.log import get_logger from tchannel.sync import TChannel from cherami_client.client import Client from tornado import ioloop # run cherami-server locally tchannel = TChannel(name='test_service', known_peers=['127.0.0.1:4922']) logger = get_logger('kombu.transport.cherami') # cherami-client needs to make tchannel calls ioloop.IOLoop.current() client = Client(tchannel, logger, timeout_seconds=3, reconfigure_interval_seconds=10) print 'created client'
(%d, '%s', %s, %d, '%s', %d)\ """ root_dir = path.dirname(__file__) saf_path = path.join(root_dir, 'fast-track-user.json') gc = pygsheets.authorize(service_account_file=saf_path) # gsheet_name = 'LatAm Fast-Track - Actions Control Center' gsheet_key = '1FvqZ0mmLtaTmJ1xOUFHCUU3F1nPLkMofand0FK6TFPk' # gs = gc.open(gsheet_name) gs = gc.open_by_key(gsheet_key) # initializing populous services with open("/etc/uber/hyperbahn/hosts.json") as f: known_peers = json.load(f) global tchannel tchannel = TChannel(name="tcurl", known_peers=known_peers) global populous_service populous_service = thrift.load(path=path.join(root_dir, 'populous.thrift'), service="populous") def decode_value(value): if type(value) == str: return value.decode('UTF-8') else: return value # check if fast track is switched off def is_fast_track():
def test_should_discover_ip(): client = TChannel('test-client') hostport = client.hostport assert '0.0.0.0:0' != hostport