def test_tornado_client_with_server_not_there(): with pytest.raises(NetworkError): yield StreamConnection.outgoing( # Try a random port that we're not listening on. # This should fail. 'localhost:41942' )
def test_close_callback_is_called(): server = TChannel('server') server.listen() close_cb = mock.Mock() conn = yield StreamConnection.outgoing(server.hostport, tchannel=mock.MagicMock()) conn.set_close_callback(close_cb) conn.close() close_cb.assert_called_once_with()
def test_close_callback_is_called(): server = TChannel('server') server.listen() close_cb = mock.Mock() conn = yield StreamConnection.outgoing( server.hostport, tchannel=mock.MagicMock() ) conn.set_close_callback(close_cb) conn.close() close_cb.assert_called_once_with()
def test_close_callback_is_called(): server = TChannel('server') server.listen() cb_future = tornado.gen.Future() conn = yield StreamConnection.outgoing( server.hostport, tchannel=mock.MagicMock() ) conn.set_close_callback(lambda: cb_future.set_result(True)) conn.close() assert (yield cb_future)
def main(): args = get_args() conn = yield StreamConnection.outgoing('%s:%d' % (args.host, args.port)) conn.tchannel = TChannel() N = 10000 before = time.time() batch_size = 100 for _ in xrange(N / batch_size): yield [conn.ping() for _ in xrange(batch_size)] after = time.time() elapsed = (after - before) * 1000 print("Finish %d iterations in %d ms" % (N, elapsed)) print("%.4f ops/s" % ((N / elapsed) * 1000))
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_invalid_message_during_streaming(tchannel_server): # test for invalid call request message hostport = 'localhost:%d' % tchannel_server.port tchannel = TChannel() connection = yield StreamConnection.outgoing( hostport=hostport, tchannel=tchannel, ) callrequest = CallRequestMessage( flags=FlagsType.fragment, args=[ 'endpoint2', 'a', 'a', ], headers={'as': 'raw'}, id=1, ) callreqcontinue = CallRequestContinueMessage( flags=FlagsType.fragment, args=[ 'a', ], id=1, ) resp_future = connection.send(callrequest) for _ in xrange(10): yield connection.write(callreqcontinue) # bypass the default checksum calculation # set a wrong checksum callreqcontinue.checksum = (ChecksumType.crc32c, 1) yield connection._write(callreqcontinue) with pytest.raises(ProtocolError) as e: resp = yield resp_future yield resp.get_header() yield resp.get_body() assert e.value.message == u"Checksum does not match!"
def test_unexpected_error_from_handler(tchannel_server): # test for invalid call request message hostport = 'localhost:%d' % tchannel_server.port tchannel = TChannel(name='test') connection = yield StreamConnection.outgoing( hostport=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_continue_message_error(mock_server): # test for invalid call request message tchannel = TChannel(name='test') connection = yield StreamConnection.outgoing( hostport=mock_server.hostport, tchannel=tchannel, ) callreqcontinue = CallRequestContinueMessage( flags=FlagsType.fragment, args=[ 'a', ], ) with pytest.raises(FatalProtocolError) as e: yield connection.send(callreqcontinue) assert 'missing call message after receiving continue message' in str(e)
def test_invalid_message_during_streaming(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=[ 'endpoint2', 'a', 'a', ], headers={'as': 'raw'}, id=1, ) callreqcontinue = CallRequestContinueMessage( flags=FlagsType.fragment, args=[ 'a', ], id=1, ) resp_future = connection.send(callrequest) for _ in xrange(10): yield connection.write(callreqcontinue) # bypass the default checksum calculation # set a wrong checksum callreqcontinue.checksum = (ChecksumType.crc32c, 1) yield connection._write(callreqcontinue) with pytest.raises(ProtocolError) as e: resp = yield resp_future yield resp.get_header() yield resp.get_body() assert e.value.message == u"Checksum does not match!"
def test_continue_message_error(tchannel_server): # test for invalid call request message hostport = 'localhost:%d' % tchannel_server.port tchannel = TChannel() connection = yield StreamConnection.outgoing( hostport=hostport, tchannel=tchannel, ) callreqcontinue = CallRequestContinueMessage( flags=FlagsType.fragment, args=[ 'a', ], ) with pytest.raises(ProtocolError) as e: yield connection.send(callreqcontinue) assert (e.value.message == u"missing call message after receiving continue message")
def test_tornado_client_with_server_not_there(random_open_port): with pytest.raises(ConnectionClosedError): yield StreamConnection.outgoing("localhost:%d" % random_open_port)