def test_chunked_close(self): # test case in which chunks spread read-callback processing # over several ioloop iterations, but the connection is already closed. port = get_unused_port() (sock,) = netutil.bind_sockets(port, address="127.0.0.1") 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"))
def make_iostream_pair(self, **kwargs): port = get_unused_port() [listener] = netutil.bind_sockets(port, '127.0.0.1', family=socket.AF_INET) 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 make_iostream_pair(self, **kwargs): port = get_unused_port() [listener] = netutil.bind_sockets(port, '127.0.0.1', family=socket.AF_INET) streams = [None, None] def accept_callback(connection, address): streams[0] = IOStream(connection, io_loop=self.io_loop, **kwargs) 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 = IOStream(socket.socket(), io_loop=self.io_loop, **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 main(): """Creates a Metadata object, fetches the default dictionary of metadata values, and prints them. If --mock was specified on the command line, creates an http server for testing. """ query_ip = Metadata._QUERY_IP # If a mock server was requested for testing, start it here. options.parse_command_line() if options.options.mock: from tornado import testing port = testing.get_unused_port() class Handler(web.RequestHandler): def get(self, path): self.write(path.split("/")[-1]) application = web.Application([ (r"/(.*)", Handler), ]) application.listen(port) query_ip = "localhost:{0}".format(port) def _MetadataCallback(metadata): print metadata ioloop.IOLoop.current().stop() Metadata(callback=_MetadataCallback, query_ip=query_ip) ioloop.IOLoop.current().start() return 0
def setUp(self): AsyncTestCase.setUp(self) self.response_callback = SpyMethod() self.server = PServer(self.response_callback, NetStringConn, io_loop = self.io_loop) self.server.handle_stream = SpyMethod() self.port = get_unused_port() self.server.listen(self.port)
def test_100_continue(self): # testing if httpclient is able to skip 100 continue responses. # to test without httpserver implementation, using # raw response as same as httpclient_test.test_chunked_close. port = get_unused_port() (sock,) = netutil.bind_sockets(port, address="127.0.0.1") with closing(sock): def write_response(stream, request_data): stream.write(b("""\ HTTP/1.1 100 Continue HTTP/1.1 200 OK Content-Length: 6 hjkl """).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, headers={"Expect": "100-continue"}) resp = self.wait() resp.rethrow() self.assertEqual(resp.body, b("hjkl\r\n"))
def start_twisted_server(self): class HelloResource(Resource): isLeaf = True def render_GET(self, request): return "Hello from twisted!" site = Site(HelloResource()) self.twisted_port = get_unused_port() self.reactor.listenTCP(self.twisted_port, site, interface='127.0.0.1')
def start_tornado_server(self): class HelloHandler(RequestHandler): def get(self): self.write("Hello from tornado!") app = Application([('/', HelloHandler)], log_function=lambda x: None) self.tornado_port = get_unused_port() app.listen(self.tornado_port, address='127.0.0.1', io_loop=self.io_loop)
def __init__(self, video_filename): self.port = get_unused_port() app = Application([ ('/api/Episode/(\d+)', FakeSonarr.EpisodeHandler), ('/api/EpisodeFile/(\d+)', FakeSonarr.EpisodeFileHandler, dict(video_filename=video_filename)) ]) self.server = HTTPServer(app) self.server.listen(self.port) self.url = 'http://localhost:%s' % (self.port,)
def get_app(self): (_, self.temp_file_path) = tempfile.mkstemp( prefix='eventlogging-service-test', text=True, ) writers = ['file://' + self.temp_file_path] application = EventLoggingService(writers, None, get_unused_port()) return application
def test_multiple_add(self): [sock] = bind_sockets(get_unused_port(), "127.0.0.1", family=socket.AF_INET) 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: sock.close()
def get_app(self): config = Config() config.from_module(gottwall.default_config) config.update({"TASKS_CHUNK": 15, "BACKENDS": {"gottwall.backends.tcpip.TCPIPBackend": { "PORT": get_unused_port()}}}) app = AggregatorApplication(config) app.configure_app(tornado.ioloop.IOLoop().instance()) return app
def setUp(self): AsyncTestCase.setUp(self) port = get_unused_port() sock = netutil.bind_sockets(port, 'localhost', family=socket.AF_INET)[0] setattr(self, '_AsyncHTTPTestCase__port', port) self.__port = port self.http_client = self.get_http_client() self._app = self.get_app() self.http_server = self.get_http_server() self.http_server.add_sockets([sock])
def __init__(self, video_filename): self.port = get_unused_port() self.movie_id = 'tt9876543' app = Application([ ('/api/'+API_KEY+'/media.get', FakeCouchPotato.MediaDotGetHandler, dict(owner=self)) ]) self.server = HTTPServer(app) self.server.listen(self.port) self.url = 'http://localhost:%s' % (self.port,) self.video_filename = video_filename self.return_files = True
def test_endpoint_document(self, mock_ioloop, mock_parse_args): mock_ioloop.time = mock.Mock(return_value=time()) mock_ioloop.current.return_value = mock_ioloop args = mock.MagicMock() args.port = get_unused_port() args.settings = "{}" args.endpoints = ["document"] mock_parse_args.return_value = args start_server()
def test_server_custom(self, mock_ioloop, mock_parse_args): mock_ioloop.time = mock.Mock(return_value=time()) mock_ioloop.current.return_value = mock_ioloop args = mock.MagicMock() args.name = "test" args.port = get_unused_port() args.settings = "{}" args.endpoints = None mock_parse_args.return_value = args start_server()
def get_app(self): config = Config() config.from_module(gottwall.default_config) config.update({"BACKENDS": {"gottwall.backends.tcpip.TCPIPBackend": { "PORT": get_unused_port()}}, "STORAGE": "gottwall.storages.MemoryStorage", "PROJECTS": {"test_project": "secretkey"}, "PRIVATE_KEY": "myprivatekey"}) self.app = AggregatorApplication(config) self.app.configure_app(self.io_loop) return self.app
def get_app(self): config = Config() config.from_module(gottwall.default_config) config.update({"BACKENDS": { "gottwall.backends.http.HTTPBackend": { "PORT": get_unused_port()} }, "PROJECTS": {"test_project": "secretkey"}, "SECRET_KEY": "myprivatekey"}) self.app = AggregatorApplication(config) self.app.configure_app(self.io_loop) return self.app
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) port = get_unused_port() stream = IOStream(socket.socket(), self.io_loop) self.connect_called = False def connect_callback(): self.connect_called = True stream.set_close_callback(self.stop) stream.connect(("localhost", port), connect_callback) self.wait() self.assertFalse(self.connect_called)
def test_multiple_add(self): [sock] = bind_sockets(get_unused_port(), '127.0.0.1', family=socket.AF_INET) 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: sock.close()
def test_connect_timeout(self): # create a socket and bind it to a port, but don't # call accept so the connection will timeout. #get_unused_port() port = get_unused_port() with closing(socket.socket()) as sock: sock.bind(('', port)) self.http_client.fetch("http://localhost:%d/" % port, self.stop, connect_timeout=0.1) response = self.wait() self.assertEqual(response.code, 599) self.assertEqual(str(response.error), "HTTP 599: Timeout")
def get_app(self): config = Config() config.from_module(gottwall.default_config) config.update({ "TASKS_CHUNK": 15, "BACKENDS": { "gottwall.backends.tcpip.TCPIPBackend": { "PORT": get_unused_port() } } }) app = AggregatorApplication(config) app.configure_app(tornado.ioloop.IOLoop().instance()) return app
def test_patch_passes_through_unknown_requests(self): class Handler(RequestHandler): def get(self): self.finish("REAL RESPONSE") port = get_unused_port() app = Application([("/", Handler)]) app.listen(port) with self.mock_client.patch(): client = AsyncHTTPClient() response = yield client.fetch("http://localhost:{}/".format(port)) self.assertEqual(200, response.code) self.assertEqual("REAL RESPONSE", response.body.decode("utf-8"))
def test_connection_refused(self): port = get_unused_port() self.http_client.fetch("http://localhost:%d/" % port, self.stop) response = self.wait() self.assertEqual(599, response.code) if sys.platform != 'cygwin': # cygwin returns EPERM instead of ECONNREFUSED here self.assertTrue( str(errno.ECONNREFUSED) in str(response.error), response.error) # This is usually "Connection refused". # On windows, strerror is broken and returns "Unknown error". expected_message = os.strerror(errno.ECONNREFUSED) self.assertTrue(expected_message in str(response.error), response.error)
def test_connection_refused(self): port = get_unused_port() self.http_client.fetch("http://localhost:%d/" % port, self.stop) response = self.wait() self.assertEqual(599, response.code) if sys.platform != 'cygwin': # cygwin returns EPERM instead of ECONNREFUSED here self.assertTrue(str(errno.ECONNREFUSED) in str(response.error), response.error) # This is usually "Connection refused". # On windows, strerror is broken and returns "Unknown error". expected_message = os.strerror(errno.ECONNREFUSED) self.assertTrue(expected_message in str(response.error), response.error)
def setUp(self): AsyncTestCase.setUp(self) self.response_callback = echo_handler self.server = PServer(self.response_callback, NetStringConn, io_loop = self.io_loop) #self.server = proxy_spy(self.server) self.port = get_unused_port() self.server.listen(self.port) conn_info = socket.getaddrinfo('127.0.0.1', self.port, socket.AF_UNSPEC, socket.SOCK_STREAM) af, socktype, proto, conname, saddress = conn_info[0] # print(conn_info) # print(socket.AF_INET, socket.SOCK_STREAM) self.server_address = saddress self.sock = socket.socket(af, socktype, 6) # In the examples from net, the protocol version is 0 self.stream = iostream.IOStream(self.sock, self.io_loop) # client must have the same event loop, otherwise it will create new one self.server.start()
def test_connect_timeout(self): # create a socket and bind it to a port, but don't # call accept so the connection will timeout. #get_unused_port() port = get_unused_port() with closing(socket.socket()) as sock: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('127.0.0.1', port)) sock.listen(1) self.http_client.fetch("http://localhost:%d/" % port, self.stop, connect_timeout=0.1) response = self.wait() self.assertEqual(response.code, 599) self.assertEqual(int(response.request_time * 10), 1) self.assertEqual(str(response.error), "HTTP 599: Timeout")
def setUp(self): super(ZMQTestCase, self).setUp() self.registry = StaticRegistry('Test') self.context = zmq.Context(1) self.groups = [] for i in xrange(3): port = get_unused_port() endpoint = '127.0.0.1:{}'.format(port) handler = SimpleKVHandler('{}'.format(i)) pool = Pool(context=self.context, io_loop=self.io_loop) server = Server(handler, endpoint, self.context, io_loop=self.io_loop) group = Group(server, self.registry, pool, SimpleKVHandler.hash_key) group.start() self.groups.append(group) self.group = self.groups[0]
def setUp(self): self.v_display = Xvfb(width=1280, height=720) self.v_display.start() self.driver = webdriver.Firefox() self.port=get_unused_port() self.home_page = 'index.html' self.definitions_link_xpath = '//*[@id="menu"]/div[1]/div/a[1]' self.definitions_link_css = 'a.item[href*=definitions]' self.overview_link_xpath = '//*[@id="menu"]/div[1]/div/a[2]' self.overview_link_css = 'a.item[href*=overview]' self.script_xpath = '/html/body/script[2]' self.introduction_block_css = 'div.introduction' self.collections_link_css = 'a.icon.item[href*=collection]' self.URL = "http://localhost:%s/%s" % (self.port, self.home_page) self.process_thread = threading.Thread(target=semantic_server.start_semantic_ui_server, args=(self.port,)) self.process_thread.start()
def test_endpoint_nonregistered(self, mock_ioloop, mock_parse_args, mock_exit): mock_ioloop.time = mock.Mock(return_value=time()) mock_ioloop.current.return_value = mock_ioloop args = mock.MagicMock() args.port = get_unused_port() args.settings = "{}" args.endpoints = ["nonregistered"] mock_parse_args.return_value = args def side_effect(value): raise Exception(value) mock_exit.side_effect = side_effect with self.assertRaisesRegexp(Exception, str(errno.EINTR)): start_server()
def make_iostream_pair(self): port = get_unused_port() [listener] = netutil.bind_sockets(port, '127.0.0.1', family=socket.AF_INET) streams = [None, None] def accept_callback(connection, address): streams[0] = IOStream(connection, io_loop=self.io_loop) 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 = IOStream(socket.socket(), io_loop=self.io_loop) client_stream.connect(('127.0.0.1', port), callback=connect_callback) self.wait(condition=lambda: all(streams)) return streams
def setup(self): self.stopped = False self.running = False self.failure = None self.stop_args = None self.http_client = AsyncHTTPClient(io_loop=self.io_loop) if 'get_app' in dir(self.__class__): # the app needs its own io loop self.io_loop = self.get_new_ioloop() self.port = get_unused_port() self.app = self.get_app() self.http_server = HTTPServer(self.app, io_loop=self.io_loop, **self.get_httpserver_options()) self.http_server.listen(self.port) else: # if we don't test handlers use the clients io loop self.io_loop = self.http_client.io_loop
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) port = get_unused_port() stream = IOStream(socket.socket(), self.io_loop) self.connect_called = False def connect_callback(): self.connect_called = True stream.set_close_callback(self.stop) 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': # cygwin's errnos don't match those used on native windows python self.assertEqual(stream.error.args[0], errno.ECONNREFUSED)
def test_multi_process(self): self.assertFalse(IOLoop.initialized()) port = get_unused_port() def get_url(path): return "http://127.0.0.1:%d%s" % (port, path) sockets = bind_sockets(port, "127.0.0.1") # ensure that none of these processes live too long signal.alarm(5) # master process try: id = fork_processes(3, max_restarts=3) except SystemExit, e: # if we exit cleanly from fork_processes, all the child processes # finished with status 0 self.assertEqual(e.code, 0) self.assertTrue(task_id() is None) for sock in sockets: sock.close() signal.alarm(0) return
def setUp(self): self.port = get_unused_port() self.io_loop = self.get_new_ioloop() # server self.handler = TestHandler(self) self.processor = ThriftTest.Processor(self.handler) self.pfactory = TBinaryProtocol.TBinaryProtocolFactory() self.server = TTornado.TTornadoServer(self.processor, self.pfactory) self.server.bind(self.port) self.server.start(1) # client transport = TTornado.TTornadoStreamTransport('localhost', self.port) pfactory = TBinaryProtocol.TBinaryProtocolFactory() self.client = ThriftTest.Client(transport, pfactory) transport.open(callback=self.stop) self.wait(timeout=1)
def setUp(self): super(ThriftTestCase, self).setUp() self.port = get_unused_port() # server self.handler = TestHandler(self) self.processor = ThriftTest.Processor(self.handler) self.pfactory = TBinaryProtocol.TBinaryProtocolFactory() self.server = TTornado.TTornadoServer(self.processor, self.pfactory, io_loop=self.io_loop) self.server.bind(self.port) self.server.start(1) # client transport = TTornado.TTornadoStreamTransport('localhost', self.port, io_loop=self.io_loop) pfactory = TBinaryProtocol.TBinaryProtocolFactory() self.io_loop.run_sync(transport.open) self.client = ThriftTest.Client(transport, pfactory)
def get_app(self): config = Config() config.from_module(gottwall.default_config) config.update({ "BACKENDS": { "gottwall.backends.http.HTTPBackend": { "PORT": get_unused_port() } }, "PROJECTS": { "test_project": "secretkey" }, "SECRET_KEY": "myprivatekey" }) self.aggregator = AggregatorApplication(config) self.aggregator.configure_app(self.io_loop) return self.aggregator.backends[0].web_application
def get_app(self): config = Config() config.from_module(gottwall.default_config) config.update({ "BACKENDS": { "gottwall.backends.tcpip.TCPIPBackend": { "PORT": get_unused_port() } }, "STORAGE": "gottwall.storages.MemoryStorage", "PROJECTS": { "test_project": "secretkey" }, "PRIVATE_KEY": "myprivatekey" }) self.app = AggregatorApplication(config) self.app.configure_app(self.io_loop) return self.app
def setup(self): self.stopped = False self.running = False self.failure = None self.stop_args = None if 'get_app' in dir(self.__class__): self.initialize_ioloop() self.app = self.get_app() elif hasattr(self, 'get_handler_spec') and self.get_handler_spec: spec = self.get_handler_spec() if spec: if isinstance(spec, tuple): if len(spec) == 3: (pattern, handler, kwargs) = spec elif len(spec) == 2: (pattern, handler) = spec kwargs = {} elif isinstance(spec, URLSpec): pattern = spec.regex.pattern handler = spec.handler_class kwargs = spec.kwargs # create an isolated version of the handler self.isolated_handler = type('IsolatedHandler', (handler, ), {}) self.initialize_ioloop() self.app = Application( [(pattern, self.isolated_handler, kwargs)], self.get_application_settings()) if self.app: self.port = get_unused_port() self.http_server = HTTPServer(self.app, io_loop=self.io_loop, **self.get_httpserver_options()) self.http_server.listen(self.port, address="0.0.0.0")
def test_multi_process(self): self.assertFalse(IOLoop.initialized()) port = get_unused_port() def get_url(path): return "http://127.0.0.1:%d%s" % (port, path) sockets = bind_sockets(port, "127.0.0.1") # ensure that none of these processes live too long signal.alarm(5) # master process id = fork_processes(3, max_restarts=3) if id is None: # back in the master process; everything worked! self.assertTrue(task_id() is None) for sock in sockets: sock.close() signal.alarm(0) return signal.alarm(5) # child process try: if id in (0, 1): signal.alarm(5) self.assertEqual(id, task_id()) server = HTTPServer(self.get_app()) server.add_sockets(sockets) IOLoop.instance().start() elif id == 2: signal.alarm(5) self.assertEqual(id, task_id()) for sock in sockets: sock.close() client = HTTPClient() def fetch(url, fail_ok=False): try: return client.fetch(get_url(url)) except HTTPError as e: if not (fail_ok and e.code == 599): raise # Make two processes exit abnormally fetch("/?exit=2", fail_ok=True) fetch("/?exit=3", fail_ok=True) # They've been restarted, so a new fetch will work int(fetch("/").body) # Now the same with signals # Disabled because on the mac a process dying with a signal # can trigger an "Application exited abnormally; send error # report to Apple?" prompt. #fetch("/?signal=%d" % signal.SIGTERM, fail_ok=True) #fetch("/?signal=%d" % signal.SIGABRT, fail_ok=True) #int(fetch("/").body) # Now kill them normally so they won't be restarted fetch("/?exit=0", fail_ok=True) # One process left; watch it's pid change pid = int(fetch("/").body) fetch("/?exit=4", fail_ok=True) pid2 = int(fetch("/").body) self.assertNotEqual(pid, pid2) # Kill the last one so we shut down cleanly fetch("/?exit=0", fail_ok=True) os._exit(0) except Exception: logging.error("exception in child process %d", id, exc_info=True) raise
def setUp(self): super(ClientTestMixin, self).setUp() self.server = CapServer(io_loop=self.io_loop) port = get_unused_port() self.server.listen(port, address='127.0.0.1') self.client = self.client_class(io_loop=self.io_loop, port=port)