async def test_proxy_subscription(self): socket_id = uuid.uuid1() async with AsyncExitStack() as exit_stack: remote_sock, remote_port = testing.bind_unused_port() remote_server = testing_utils.EchoServer() remote_server.add_sockets([remote_sock]) # Register the remote server to stop on exit. exit_stack.callback(remote_server.stop) # Make a request to proxy traffic from a local port to the remote # server. state = await self.ws_connect() async with AsyncExitStack() as sub_stack: local_proxy = BufferedProxyContext(state, socket_id) await sub_stack.enter_async_context(local_proxy) sub = json_protocol.setup_subscription( state, "proxy_socket", dict(host="localhost", port=remote_port, protocol="tcp", socket_id=socket_id.hex, buffsize=1)) await sub_stack.enter_async_context(sub) msg = await sub.next() self.assertIn('connection_status', msg) self.assertEqual('connected', msg['connection_status']) self.assertIsNotNone(msg['socket_id']) self.assertEqual(0, msg['count'])
def test_websocket_network_fail(self): sock, port = bind_unused_port() sock.close() with self.assertRaises(HTTPError) as cm: with ExpectLog(gen_log, ".*"): yield websocket_connect("ws://localhost:%d/" % port, io_loop=self.io_loop, connect_timeout=3600) self.assertEqual(cm.exception.code, 599)
def test_handler_callback_file_object(self): """The handler callback receives the same fd object it passed in.""" server_sock, port = bind_unused_port() fds = [] def handle_connection(fd, events): fds.append(fd) conn, addr = server_sock.accept() conn.close() self.stop() self.io_loop.add_handler(server_sock, handle_connection, IOLoop.READ) with contextlib.closing(socket.socket()) as client_sock: client_sock.connect(('127.0.0.1', port)) self.wait() self.io_loop.remove_handler(server_sock) self.io_loop.add_handler(server_sock.fileno(), handle_connection, IOLoop.READ) with contextlib.closing(socket.socket()) as client_sock: client_sock.connect(('127.0.0.1', port)) self.wait() self.assertIs(fds[0], server_sock) self.assertEqual(fds[1], server_sock.fileno()) self.io_loop.remove_handler(server_sock.fileno()) server_sock.close()
def test_handle_stream_coroutine_logging(self): # handle_stream may be a coroutine and any exception in its # Future will be logged. class TestServer(TCPServer): @gen.coroutine def handle_stream(self, stream, address): yield gen.moment stream.close() 1 / 0 server = client = None try: sock, port = bind_unused_port() with NullContext(): server = TestServer() server.add_socket(sock) client = IOStream(socket.socket()) with ExpectLog(app_log, "Exception in callback"): yield client.connect(('localhost', port)) yield client.read_until_close() yield gen.moment finally: if server is not None: server.stop() if client is not None: client.close()
def test_close_file_object(self): """When a file object is used instead of a numeric file descriptor, the object should be closed (by IOLoop.close(all_fds=True), not just the fd. """ # Use a socket since they are supported by IOLoop on all platforms. # Unfortunately, sockets don't support the .closed attribute for # inspecting their close status, so we must use a wrapper. class SocketWrapper(object): def __init__(self, sockobj): self.sockobj = sockobj self.closed = False def fileno(self): return self.sockobj.fileno() def close(self): self.closed = True self.sockobj.close() sockobj, port = bind_unused_port() socket_wrapper = SocketWrapper(sockobj) io_loop = IOLoop() io_loop.add_handler(socket_wrapper, lambda fd, events: None, IOLoop.READ) io_loop.close(all_fds=True) self.assertTrue(socket_wrapper.closed)
def test_chunked_close(self): # test case in which chunks spread read-callback processing # over several ioloop iterations, but the connection is already closed. sock, port = bind_unused_port() with closing(sock): def write_response(stream, request_data): stream.write(b("""\ HTTP/1.1 200 OK Transfer-Encoding: chunked 1 1 1 2 0 """).replace(b("\n"), b("\r\n")), callback=stream.close) def accept_callback(conn, address): # fake an HTTP server using chunked encoding where the final chunks # and connection close all happen at once stream = IOStream(conn, io_loop=self.io_loop) stream.read_until(b("\r\n\r\n"), functools.partial(write_response, stream)) netutil.add_accept_handler(sock, accept_callback, self.io_loop) self.http_client.fetch("http://127.0.0.1:%d/" % port, self.stop) resp = self.wait() resp.rethrow() self.assertEqual(resp.body, b("12")) self.io_loop.remove_handler(sock.fileno())
def test_multi_line_headers(self): # Multi-line http headers are rare but rfc-allowed # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 sock, port = bind_unused_port() with closing(sock): @gen.coroutine def accept_callback(conn, address): stream = IOStream(conn) request_data = yield stream.read_until(b"\r\n\r\n") if b"HTTP/1." not in request_data: self.skipTest("requires HTTP/1.x") yield stream.write( b"""\ HTTP/1.1 200 OK X-XSS-Protection: 1; \tmode=block """.replace( b"\n", b"\r\n" ) ) stream.close() netutil.add_accept_handler(sock, accept_callback) # type: ignore resp = self.fetch("http://127.0.0.1:%d/" % port) resp.rethrow() self.assertEqual(resp.headers["X-XSS-Protection"], "1; mode=block") self.io_loop.remove_handler(sock.fileno())
def setUp(self): super(WebTest, self).setUp() self.application = Application() self.server = HTTPServer(self.application) self.socket, self.port = testing.bind_unused_port() self.server.add_socket(self.socket) self.static_path = os.path.join(os.path.dirname(wsrpc.__file__), 'static')
def test_websocket_network_fail(self): sock, port = bind_unused_port() sock.close() with self.assertRaises(IOError): with ExpectLog(gen_log, ".*"): yield websocket_connect("ws://127.0.0.1:%d/" % port, connect_timeout=3600)
def test_connection_refused(self): # When a connection is refused, the connect callback should not # be run. (The kqueue IOLoop used to behave differently from the # epoll IOLoop in this respect) server_socket, port = bind_unused_port() server_socket.close() stream = IOStream(socket.socket(), self.io_loop) self.connect_called = False def connect_callback(): self.connect_called = True stream.set_close_callback(self.stop) # log messages vary by platform and ioloop implementation with ExpectLog(gen_log, ".*", required=False): stream.connect(("localhost", port), connect_callback) self.wait() self.assertFalse(self.connect_called) self.assertTrue(isinstance(stream.error, socket.error), stream.error) if sys.platform != 'cygwin': _ERRNO_CONNREFUSED = (errno.ECONNREFUSED, ) if hasattr(errno, "WSAECONNREFUSED"): _ERRNO_CONNREFUSED += (errno.WSAECONNREFUSED, ) # cygwin's errnos don't match those used on native windows python self.assertTrue(stream.error.args[0] in _ERRNO_CONNREFUSED)
def make_iostream_pair(self, **kwargs): listener, port = bind_unused_port() streams = [None, None] def accept_callback(connection, address): streams[0] = self._make_server_iostream(connection, **kwargs) if isinstance(streams[0], SSLIOStream): # HACK: The SSL handshake won't complete (and # therefore the client connect callback won't be # run)until the server side has tried to do something # with the connection. For these tests we want both # sides to connect before we do anything else with the # connection, so we must cause some dummy activity on the # server. If this turns out to be useful for real apps # it should have a cleaner interface. streams[0]._add_io_state(IOLoop.READ) self.stop() def connect_callback(): streams[1] = client_stream self.stop() netutil.add_accept_handler(listener, accept_callback, io_loop=self.io_loop) client_stream = self._make_client_iostream(socket.socket(), **kwargs) client_stream.connect(('127.0.0.1', port), callback=connect_callback) self.wait(condition=lambda: all(streams)) self.io_loop.remove_handler(listener.fileno()) listener.close() return streams
def test_multi_line_headers(self): # Multi-line http headers are rare but rfc-allowed # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 sock, port = bind_unused_port() with closing(sock): def write_response(stream, request_data): stream.write(b"""\ HTTP/1.1 200 OK X-XSS-Protection: 1; \tmode=block """.replace(b"\n", b"\r\n"), callback=stream.close) def accept_callback(conn, address): stream = IOStream(conn, io_loop=self.io_loop) stream.read_until(b"\r\n\r\n", functools.partial(write_response, stream)) netutil.add_accept_handler(sock, accept_callback, self.io_loop) self.http_client.fetch("http://127.0.0.1:%d/" % port, self.stop) resp = self.wait() resp.rethrow() self.assertEqual(resp.headers['X-XSS-Protection'], "1; mode=block") self.io_loop.remove_handler(sock.fileno())
def test_multi_line_headers(self): # Multi-line http headers are rare but rfc-allowed # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 sock, port = bind_unused_port() with closing(sock): def write_response(stream, request_data): if b"HTTP/1." not in request_data: self.skipTest("requires HTTP/1.x") stream.write(b"""\ HTTP/1.1 200 OK X-XSS-Protection: 1; \tmode=block """.replace(b"\n", b"\r\n"), callback=stream.close) def accept_callback(conn, address): stream = IOStream(conn, io_loop=self.io_loop) stream.read_until(b"\r\n\r\n", functools.partial(write_response, stream)) netutil.add_accept_handler(sock, accept_callback, self.io_loop) self.http_client.fetch("http://127.0.0.1:%d/" % port, self.stop) resp = self.wait() resp.rethrow() self.assertEqual(resp.headers['X-XSS-Protection'], "1; mode=block") self.io_loop.remove_handler(sock.fileno())
def test_handle_stream_coroutine_logging(self): # handle_stream may be a coroutine and any exception in its # Future will be logged. class TestServer(TCPServer): @gen.coroutine def handle_stream(self, stream, address): yield stream.read_bytes(len(b'hello')) stream.close() 1 / 0 server = client = None try: sock, port = bind_unused_port() server = TestServer() server.add_socket(sock) client = IOStream(socket.socket()) with ExpectLog(app_log, "Exception in callback"): yield client.connect(('localhost', port)) yield client.write(b'hello') yield client.read_until_close() yield gen.moment finally: if server is not None: server.stop() if client is not None: client.close()
def test_connection_refused(self): # When a connection is refused, the connect callback should not # be run. (The kqueue IOLoop used to behave differently from the # epoll IOLoop in this respect) server_socket, port = bind_unused_port() server_socket.close() stream = IOStream(socket.socket(), self.io_loop) self.connect_called = False def connect_callback(): self.connect_called = True self.stop() stream.set_close_callback(self.stop) # log messages vary by platform and ioloop implementation with ExpectLog(gen_log, ".*", required=False): stream.connect(("127.0.0.1", port), connect_callback) self.wait() self.assertFalse(self.connect_called) self.assertTrue(isinstance(stream.error, socket.error), stream.error) if sys.platform != 'cygwin': _ERRNO_CONNREFUSED = (errno.ECONNREFUSED,) if hasattr(errno, "WSAECONNREFUSED"): _ERRNO_CONNREFUSED += (errno.WSAECONNREFUSED,) # cygwin's errnos don't match those used on native windows python self.assertTrue(stream.error.args[0] in _ERRNO_CONNREFUSED)
def test_multi_line_headers(self): # Multi-line http headers are rare but rfc-allowed # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 sock, port = bind_unused_port() with closing(sock): @gen.coroutine def accept_callback(conn, address): stream = IOStream(conn) request_data = yield stream.read_until(b"\r\n\r\n") if b"HTTP/1." not in request_data: self.skipTest("requires HTTP/1.x") yield stream.write(b"""\ HTTP/1.1 200 OK X-XSS-Protection: 1; \tmode=block """.replace(b"\n", b"\r\n")) stream.close() netutil.add_accept_handler(sock, accept_callback) # type: ignore resp = self.fetch("http://127.0.0.1:%d/" % port) resp.rethrow() self.assertEqual(resp.headers["X-XSS-Protection"], "1; mode=block") self.io_loop.remove_handler(sock.fileno())
def test_chunked_close(self): # test case in which chunks spread read-callback processing # over several ioloop iterations, but the connection is already closed. sock, port = bind_unused_port() with closing(sock): @gen.coroutine def accept_callback(conn, address): # fake an HTTP server using chunked encoding where the final chunks # and connection close all happen at once stream = IOStream(conn) request_data = yield stream.read_until(b"\r\n\r\n") if b"HTTP/1." not in request_data: self.skipTest("requires HTTP/1.x") yield stream.write(b"""\ HTTP/1.1 200 OK Transfer-Encoding: chunked 1 1 1 2 0 """.replace(b"\n", b"\r\n")) stream.close() netutil.add_accept_handler(sock, accept_callback) # type: ignore resp = self.fetch("http://127.0.0.1:%d/" % port) resp.rethrow() self.assertEqual(resp.body, b"12") self.io_loop.remove_handler(sock.fileno())
def test_chunked_close(self): # test case in which chunks spread read-callback processing # over several ioloop iterations, but the connection is already closed. sock, port = bind_unused_port() with closing(sock): @gen.coroutine def accept_callback(conn, address): # fake an HTTP server using chunked encoding where the final chunks # and connection close all happen at once stream = IOStream(conn) request_data = yield stream.read_until(b"\r\n\r\n") if b"HTTP/1." not in request_data: self.skipTest("requires HTTP/1.x") yield stream.write(b"""\ HTTP/1.1 200 OK Transfer-Encoding: chunked 1 1 1 2 0 """.replace(b"\n", b"\r\n")) stream.close() netutil.add_accept_handler(sock, accept_callback) resp = self.fetch("http://127.0.0.1:%d/" % port) resp.rethrow() self.assertEqual(resp.body, b"12") self.io_loop.remove_handler(sock.fileno())
def test_chunked_close(self): # test case in which chunks spread read-callback processing # over several ioloop iterations, but the connection is already closed. sock, port = bind_unused_port() with closing(sock): def write_response(stream, request_data): if b"HTTP/1." not in request_data: self.skipTest("requires HTTP/1.x") stream.write(b"""\ HTTP/1.1 200 OK Transfer-Encoding: chunked 1 1 1 2 0 """.replace(b"\n", b"\r\n"), callback=stream.close) def accept_callback(conn, address): # fake an HTTP server using chunked encoding where the final chunks # and connection close all happen at once stream = IOStream(conn, io_loop=self.io_loop) stream.read_until(b"\r\n\r\n", functools.partial(write_response, stream)) netutil.add_accept_handler(sock, accept_callback, self.io_loop) self.http_client.fetch("http://127.0.0.1:%d/" % port, self.stop) resp = self.wait() resp.rethrow() self.assertEqual(resp.body, b"12") self.io_loop.remove_handler(sock.fileno())
def get_app(self): path = StandardDeviationSearch.StandardDeviationSearchHandlerImpl.path algorithm = AlgorithmModuleWrapper(StandardDeviationSearch.StandardDeviationSearchHandlerImpl) thread_pool = ThreadPool(processes=1) return Application( [(path, ModularNexusHandlerWrapper, dict(clazz=algorithm, algorithm_config=None, thread_pool=thread_pool))], default_host=bind_unused_port() )
def get_app(self): path = LongitudeLatitudeMap.LongitudeLatitudeMapHandlerImpl.path algorithm = AlgorithmModuleWrapper(LongitudeLatitudeMap.LongitudeLatitudeMapHandlerImpl) thread_pool = ThreadPool(processes=1) return Application( [(path, ModularNexusHandlerWrapper, dict(clazz=algorithm, algorithm_config=None, thread_pool=thread_pool))], default_host=bind_unused_port() )
def setUp(self): super(BaseAsyncTest, self).setUp() Channels.reset() application = make_application() server = httpserver.HTTPServer(application) socket, self.port = testing.bind_unused_port() server.add_socket(socket)
def setUp(self): super(AsyncSMTPTestCase, self).setUp() sock, port = bind_unused_port() self.__port = port self._request_callback = self.get_request_callback() self.smtp_server = self.get_smtp_server() self.smtp_server.add_sockets([sock])
def test_websocket_network_fail(self): sock, port = bind_unused_port() sock.close() with self.assertRaises(IOError): with ExpectLog(gen_log, ".*"): yield websocket_connect('ws://localhost:%d/' % port, io_loop=self.io_loop, connect_timeout=3600)
def test_remove_without_add(self): # remove_handler should not throw an exception if called on an fd # was never added. sock, port = bind_unused_port() try: self.io_loop.remove_handler(sock.fileno()) finally: sock.close()
def __init__(self, handler): sock, port = bind_unused_port() self._port = port self._app = tornado.web.Application([(r".*", SimpleHttpServerHandler)], debug=True, actual_handler=handler) self.http_server = HTTPServer(self._app) self.http_server.add_sockets([sock])
def test_websocket_network_fail(self): sock, port = bind_unused_port() sock.close() with self.assertRaises(IOError): with ExpectLog(gen_log, ".*"): yield websocket_connect( "ws://127.0.0.1:%d/" % port, connect_timeout=3600 )
def _start_server(self): self.app = application self.app.crew = Client() self.io_loop.add_callback(self.app.crew.connect) self.server = httpserver.HTTPServer(self.app) socket, self.port = testing.bind_unused_port() self.server.add_socket(socket) self.server.start()
def get_app(self): path = StandardDeviationSearch.StandardDeviationSearchCalcHandlerImpl.path algorithm = StandardDeviationSearch.StandardDeviationSearchCalcHandlerImpl thread_pool = tornado.concurrent.futures.ThreadPoolExecutor(1) #thread_pool = ThreadPool(processes=1) return tornado.web.Application( [(path, NexusRequestHandler, dict(clazz=algorithm, thread_pool=thread_pool))], default_host=bind_unused_port())
def __init__(self): self._app = self.get_app() sock, port = bind_unused_port() self._port = port self.io_loop = self.get_new_ioloop() self.http_server = self.get_http_server() self.http_server.add_sockets([sock]) self.timeout = 5 self._remove_timeout = None
async def client(app: Application) -> AsyncIterable[AsyncClient]: """ Return a client to be used in async tests """ http_server = HTTPServer(app) port = bind_unused_port()[1] http_server.listen(port) async with AsyncClient(base_url=f"http://localhost:{port}") as _client: yield _client
def setUp(self): super(AsyncHTTPTestCaseTest, self).setUp() # Bind a second port. sock, port = bind_unused_port() app = Application() server = HTTPServer(app, **self.get_httpserver_options()) server.add_socket(sock) self.second_port = port self.second_server = server
def test_websocket_network_fail(self): sock, port = bind_unused_port() sock.close() with self.assertRaises(HTTPError) as cm: with ExpectLog(gen_log, ".*"): yield websocket_connect('ws://localhost:%d/' % port, io_loop=self.io_loop, connect_timeout=3600) self.assertEqual(cm.exception.code, 599)
def get_app(self): path = LongitudeLatitudeMap.LongitudeLatitudeMapCalcHandlerImpl.path algorithm = LongitudeLatitudeMap.LongitudeLatitudeMapCalcHandlerImpl thread_pool = ThreadPool(processes=1) return Application([(path, NexusRequestHandler, dict(clazz=algorithm, algorithm_config=None, thread_pool=thread_pool))], default_host=bind_unused_port())
def setUp(self): sock, port = bind_unused_port() self.__port = port self.io_loop = self.get_new_ioloop() self.io_loop.make_current() self.http_server = HTTPServer(self.application, io_loop=self.io_loop) self.http_client = AsyncHTTPClient(io_loop=self.io_loop) self.http_server.add_sockets([sock])
def start_tornado_server(self): class HelloHandler(RequestHandler): def get(self): self.write("Hello from tornado!") app = Application([('/', HelloHandler)], log_function=lambda x: None) server = HTTPServer(app) sock, self.tornado_port = bind_unused_port() server.add_sockets([sock])
def test_websocket_network_fail(self): sock, port = bind_unused_port() sock.close() with self.assertRaises(IOError): with ExpectLog(gen_log, ".*"): yield websocket_connect( 'ws://localhost:%d/' % port, io_loop=self.io_loop, connect_timeout=3600)
def test_reuse_port(self): socket, port = bind_unused_port(reuse_port=True) try: sockets = bind_sockets(port, "localhost", reuse_port=True) self.assertTrue(all(s.getsockname()[1] == port for s in sockets)) finally: socket.close() for sock in sockets: sock.close()
def test_reuse_port(self): socket, port = bind_unused_port(reuse_port=True) try: sockets = bind_sockets(port, 'localhost', reuse_port=True) self.assertTrue(all(s.getsockname()[1] == port for s in sockets)) finally: socket.close() for sock in sockets: sock.close()
def start_tornado_server(self): class HelloHandler(RequestHandler): def get(self): self.write("Hello from tornado!") app = Application([("/", HelloHandler)], log_function=lambda x: None) server = HTTPServer(app, io_loop=self.io_loop) sock, self.tornado_port = bind_unused_port() server.add_sockets([sock])
def test_distributed_registry_logs_stuff(): sock, http_port = bind_unused_port() sock.close() aggregator = RegistryAggregator(HTTPReporter(http_port)) aggregator.start() # allow the aggregator to start up to get all the messages http_up = False for _ in range(10): try: requests.get('http://localhost:%d/' % http_port) http_up = True break except requests.ConnectionError: sleep(0.5) if http_up: # http is up, wait a bit for the actual socket sleep(1) else: raise AssertionError('Failed to start registry aggregator') registry = DistributedRegistry() registry.connect() counter = registry.counter('my.counter') meter = registry.meter('my.meter') try: num_messages = 1000 for _ in range(num_messages): meter.mark() counter.increment() eventually_consistent = False for _ in range(10): try: response = requests.get('http://localhost:%d/' % http_port) assert response.status_code == 200 stats = response.json() # TODO: 0MQ batches the sends, and there's no reliable way to test this afaik. Just make sure it's no # more than 200 messages behind... assert abs(stats['my']['counter']['value'] - num_messages) < 200 assert abs(stats['my']['meter']['count'] - num_messages) < 200 eventually_consistent = True except AssertionError: sleep(1) if not eventually_consistent: raise AssertionError( 'Counts are inconsistent: {} sent, {} received; dropping messages?' .format(num_messages, stats['my']['counter']['value'])) finally: aggregator.stop() registry.close()
def test_reuse_port(self): sockets = [] # type: List[socket.socket] socket, port = bind_unused_port(reuse_port=True) try: sockets = bind_sockets(port, "127.0.0.1", reuse_port=True) self.assertTrue(all(s.getsockname()[1] == port for s in sockets)) finally: socket.close() for sock in sockets: sock.close()
def test_multiple_add(self): sock, port = bind_unused_port() try: self.io_loop.add_handler(sock.fileno(), lambda fd, events: None, IOLoop.READ) # Attempting to add the same handler twice fails # (with a platform-dependent exception) self.assertRaises(Exception, self.io_loop.add_handler, sock.fileno(), lambda fd, events: None, IOLoop.READ) finally: self.io_loop.remove_handler(sock.fileno()) sock.close()
def setUp(self): super(TestBrowserSession, self).setUp() self._ioloop = IOLoop() self._app = Application([ ("/", IndexHandler), ("/timeout", TimeoutHandler), ("/auth", AuthHandler) ], ioloop=self._ioloop, cookie_secret="foobar") self._server = HTTPServer(self._app, io_loop=self._ioloop) _, self._port = bind_unused_port()
def setUp(self): class HeaderAuth(AbstractAuthorization): def authorize(self, handler): if 'Authorization' in handler.request.headers: return handler.request.headers['Authorization'] == 'Test' return False super(WebSocketHeaderAuthTest, self).setUp() server = httpserver.HTTPServer(WebSocketServer.make_app(HeaderAuth())) socket, self.port = testing.bind_unused_port() server.add_socket(socket)
def test_mixed_fd_fileobj(self): server_sock, port = bind_unused_port() def f(fd, events): pass self.io_loop.add_handler(server_sock, f, IOLoop.READ) with self.assertRaises(Exception): # The exact error is unspecified - some implementations use # IOError, others use ValueError. self.io_loop.add_handler(server_sock.fileno(), f, IOLoop.READ) self.io_loop.remove_handler(server_sock.fileno()) server_sock.close()
def get_app(self): self.dbfile = tempfile.NamedTemporaryFile(suffix=".db") self.dburi = "dbm://{}".format(self.dbfile.name) self.connection = DBMConnection.from_uri(self.dburi) self.store = self.connection.get_store() _, self.mailgun_port = bind_unused_port() self.mailgun_url = "http://localhost:{0}/v2/account".format( self.mailgun_port) mailgun = MailGunClient(self.mailgun_url, "KEY", self.io_loop) queue = mock.Mock() return Application(store=self.store, mailgun=mailgun, queue=queue)
def test_unregistered_torrent(self): listener, port = bind_unused_port() peer_broker = SocketClient(self.SERVER_PEER_ID, listener, self.io_loop, port) peer_broker.listen() _, _, metainfo = make_metainfo([('a.txt', 'hello world')], 4) handshake = PeerHandshake.make(metainfo, peer_id=self.CLIENT_PEER_ID) client_stream = IOStream(socket.socket(), io_loop=self.io_loop) yield gen.Task(client_stream.connect, ('localhost', port)) yield gen.Task(client_stream.write, handshake) self.io_loop.clear_current() assert peer_broker._failed_handshakes == 1
def __init__(self, ioloop, port=None): self.ioloop = ioloop if port is None: self.socket, self.port = bind_unused_port() elif isinstance(port, tuple): self.socket, self.port = port else: self.socket, self.port = None, port self.host = "localhost:{0}".format(self.port) self.protocol = "http" self.base_url = "http://" + self.host self.routes = {} self._listening = False self._service = None
def setUp(self): super(TestBase, self).setUp() self._serial = 0 self._futures = {} self.application = Application() self.server = HTTPServer(self.application) self.socket, self.port = testing.bind_unused_port() self.server.add_socket(self.socket) self.connection = None connection = websocket.websocket_connect('ws://localhost:{0.port}{0.URI}'.format(self)) connection.add_done_callback(self._set_conn)
def make_iostream_pair(self, **kwargs): listener, port = bind_unused_port() server_stream_fut = Future() def accept_callback(connection, address): server_stream_fut.set_result(self._make_server_iostream(connection, **kwargs)) netutil.add_accept_handler(listener, accept_callback) client_stream = self._make_client_iostream(socket.socket(), **kwargs) connect_fut = client_stream.connect(('127.0.0.1', port)) server_stream, client_stream = yield [server_stream_fut, connect_fut] self.io_loop.remove_handler(listener.fileno()) listener.close() raise gen.Return((server_stream, client_stream))
def setUp(self): try: super(TestIOStreamStartTLS, self).setUp() self.listener, self.port = bind_unused_port() self.server_stream = None self.server_accepted = Future() netutil.add_accept_handler(self.listener, self.accept) self.client_stream = IOStream(socket.socket()) self.io_loop.add_future(self.client_stream.connect(("127.0.0.1", self.port)), self.stop) self.wait() self.io_loop.add_future(self.server_accepted, self.stop) self.wait() except Exception as e: print(e) raise