예제 #1
0
    def __init__(self, name, loader=None):
        """Initialize, but do not start the WSGI server.

        :param name: The name of the WSGI server given to the loader.
        :param loader: Loads the WSGI application using the given name.
        :returns: None

        """
        self.name = name
        self.manager = self._get_manager()
        self.loader = loader or wsgi_common.Loader()
        self.app = self.loader.load_app(name)
        self.host = getattr(CONF, '%s_listen' % name, "0.0.0.0")
        self.port = getattr(CONF, '%s_listen_port' % name, 0)
        self.workers = (getattr(CONF, '%s_workers' % name, None) or
                        processutils.get_worker_count())
        if self.workers and self.workers < 1:
            worker_name = '%s_workers' % name
            msg = (_("%(worker_name)s value of %(workers)d is invalid, "
                     "must be greater than 0.") %
                   {'worker_name': worker_name,
                    'workers': self.workers})
            raise exception.InvalidInput(msg)
        setup_profiler(name, self.host)

        self.server = wsgi.Server(name,
                                  self.app,
                                  host=self.host,
                                  port=self.port)
예제 #2
0
    def test_client_socket_timeout(self):
        CONF.set_default("client_socket_timeout", 0.1)
        greetings = 'Hello, World!!!'

        def hello_world(env, start_response):
            start_response('200 OK', [('Content-Type', 'text/plain')])
            return [greetings]

        server = wsgi.Server("test_app", hello_world,
                             host="127.0.0.1", port=0)
        server.start()

        s = socket.socket()
        s.connect(("127.0.0.1", server.port))

        fd = s.makefile('rw')
        fd.write(b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        fd.flush()

        buf = fd.read()
        self.assertTrue(re.search(greetings, buf))

        s2 = socket.socket()
        s2.connect(("127.0.0.1", server.port))
        time.sleep(0.2)

        fd = s2.makefile('rw')
        fd.write(b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        fd.flush()

        buf = fd.read()
        # connection is closed so we get nothing from the server
        self.assertFalse(buf)
        server.stop()
예제 #3
0
 def test_start_random_port_with_ipv6(self):
     server = wsgi.Server("test_random_port",
                          None,
                          host="::1")
     server.start()
     self.assertEqual("::1", server.host)
     self.assertNotEqual(0, server.port)
     server.stop()
     server.wait()
예제 #4
0
 def test_server_pool_waitall(self):
     # test pools waitall method gets called while stopping server
     server = wsgi.Server("test_server", None,
                          host="127.0.0.1")
     server.start()
     with mock.patch.object(server._pool,
                            'waitall') as mock_waitall:
         server.stop()
         server.wait()
         mock_waitall.assert_called_once_with()
예제 #5
0
    def test_reset_pool_size_to_default(self):
        server = wsgi.Server("test_resize", None, host="127.0.0.1")
        server.start()

        # Stopping the server, which in turn sets pool size to 0
        server.stop()
        self.assertEqual(0, server._pool.size)

        # Resetting pool size to default
        server.reset()
        server.start()
        self.assertEqual(1000, server._pool.size)
예제 #6
0
    def test_app(self):
        greetings = 'Hello, World!!!'

        def hello_world(env, start_response):
            if env['PATH_INFO'] != '/':
                start_response('404 Not Found',
                               [('Content-Type', 'text/plain')])
                return ['Not Found\r\n']
            start_response('200 OK', [('Content-Type', 'text/plain')])
            return [greetings]

        server = wsgi.Server("test_app", hello_world,
                             host="127.0.0.1", port=0)
        server.start()

        response = open_no_proxy('http://127.0.0.1:%d/' % server.port)
        self.assertEqual(greetings, response.read())
        server.stop()
예제 #7
0
    def test_app_using_ssl(self):
        CONF.set_default("ssl_cert_file",
                         os.path.join(TEST_VAR_DIR, 'certificate.crt'))
        CONF.set_default("ssl_key_file",
                         os.path.join(TEST_VAR_DIR, 'privatekey.key'))

        greetings = 'Hello, World!!!'

        @webob.dec.wsgify
        def hello_world(req):
            return greetings

        server = wsgi.Server("test_app", hello_world,
                             host="127.0.0.1", port=0)

        server.start()

        response = open_no_proxy('https://127.0.0.1:%d/' % server.port)
        self.assertEqual(greetings, response.read())

        server.stop()
예제 #8
0
 def test_start_random_port(self):
     server = wsgi.Server("test_random_port", None, host="127.0.0.1")
     server.start()
     self.assertNotEqual(0, server.port)
     server.stop()
     server.wait()
예제 #9
0
 def test_no_app(self):
     server = wsgi.Server("test_app", None,
                          host="127.0.0.1", port=0)
     self.assertEqual("test_app", server.name)