def _build_transport_factory(transport): if transport == Transport.framed: return TFramedTransportFactory() elif transport == Transport.buffered: return TBufferedTransportFactory() else: raise Exception("Unknown transport")
def test(thrift_server, thriftpy_test_module, request_xtruct): client = make_client(thriftpy_test_module.ThriftTest, host='127.0.0.1', port=6000, proto_factory=TBinaryProtocolFactory(), trans_factory=TFramedTransportFactory()) response = client.testStruct(request_xtruct) assert response == utils.build_xtruct(thriftpy_test_module, "string_thingy2", 1, 17, 33, 65)
class FramedTransportTestCase(TestCase): TRANSPORT_FACTORY = TFramedTransportFactory() PROTOCOL_FACTORY = TBinaryProtocolFactory() def mk_server(self): sock = self.server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('127.0.0.1', 0)) sock.setblocking(0) self.port = sock.getsockname()[-1] self.server_thread = threading.Thread(target=self.listen) self.server_thread.setDaemon(True) self.server_thread.start() def listen(self): self.server_sock.listen(128) if asyncio: # In Tornado 5.0+, the asyncio event loop will be used # automatically by default asyncio.set_event_loop(asyncio.new_event_loop()) self.io_loop = ioloop.IOLoop.current() server = make_server(addressbook.AddressBookService, Dispatcher(self.io_loop), io_loop=self.io_loop) server.add_socket(self.server_sock) self.io_loop.start() def mk_client(self): return make_client(addressbook.AddressBookService, '127.0.0.1', self.port, proto_factory=self.PROTOCOL_FACTORY, trans_factory=self.TRANSPORT_FACTORY) def setUp(self): self.mk_server() time.sleep(0.1) self.client = self.mk_client() def tearDown(self): self.io_loop.stop() @pytest.mark.skipif(sys.version_info[:2] == (2, 6), reason="not support") def test_able_to_communicate(self): dennis = addressbook.Person(name='Dennis Ritchie') success = self.client.add(dennis) assert success success = self.client.add(dennis) assert not success @pytest.mark.skipif(sys.version_info[:2] == (2, 6), reason="not support") def test_zero_length_string(self): dennis = addressbook.Person(name='') success = self.client.add(dennis) assert success success = self.client.get(name='') assert success
def init_broker(): try: global client client = make_client(broker_thrift.TradeService, config.BROKER_HOST, config.BROKER_PORT, proto_factory=TBinaryProtocolFactory(), trans_factory=TFramedTransportFactory(), timeout=60000) except Exception as e: logging.warn("make_client exception") traceback.print_exc()
class FramedTransportTestCase(TestCase): TRANSPORT_FACTORY = TFramedTransportFactory() PROTOCOL_FACTORY = TBinaryProtocolFactory() def mk_server(self): self.io_loop = ioloop.IOLoop() server = make_server(addressbook.AddressBookService, Dispatcher(self.io_loop), io_loop=self.io_loop) self.server = server sock = self.server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('localhost', 0)) sock.setblocking(0) self.port = sock.getsockname()[-1] self.server_thread = threading.Thread(target=self.listen) self.server_thread.setDaemon(True) self.server_thread.start() def listen(self): self.server_sock.listen(128) self.server.add_socket(self.server_sock) self.io_loop.start() def mk_client(self): return make_client(addressbook.AddressBookService, '127.0.0.1', self.port, proto_factory=self.PROTOCOL_FACTORY, trans_factory=self.TRANSPORT_FACTORY) def setUp(self): self.mk_server() time.sleep(0.1) self.client = self.mk_client() def test_able_to_communicate(self): dennis = addressbook.Person(name='Dennis Ritchie') success = self.client.add(dennis) assert success success = self.client.add(dennis) assert not success def test_zero_length_string(self): dennis = addressbook.Person(name='') success = self.client.add(dennis) assert success success = self.client.get(name='') assert success