"Fossies" - the Fresh Open Source Software Archive

Member "glance-20.0.1/glance/tests/functional/test_wsgi.py" (12 Aug 2020, 2143 Bytes) of package /linux/misc/openstack/glance-20.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_wsgi.py": 20.0.0_vs_20.0.1.

    1 # Copyright 2014 Hewlett-Packard Development Company, L.P.
    2 # All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 
   16 """Tests for `glance.wsgi`."""
   17 
   18 import os
   19 import socket
   20 import time
   21 
   22 from glance.common import wsgi
   23 from glance.tests import functional
   24 
   25 
   26 class TestWSGIServer(functional.FunctionalTest):
   27     """WSGI server tests."""
   28     def test_client_socket_timeout(self):
   29         self.config(workers=0)
   30         self.config(client_socket_timeout=1)
   31         """Verify connections are timed out as per 'client_socket_timeout'"""
   32         greetings = b'Hello, World!!!'
   33 
   34         def hello_world(env, start_response):
   35             start_response('200 OK', [('Content-Type', 'text/plain')])
   36             return [greetings]
   37 
   38         server = wsgi.Server()
   39         server.start(hello_world, 0)
   40         port = server.sock.getsockname()[1]
   41 
   42         def get_request(delay=0.0):
   43             # Socket timeouts are handled rather inconsistently on Windows.
   44             # recv may either return nothing OR raise a ConnectionAbortedError.
   45             exp_exc = OSError if os.name == 'nt' else ()
   46 
   47             try:
   48                 sock = socket.socket()
   49                 sock.connect(('127.0.0.1', port))
   50                 time.sleep(delay)
   51                 sock.send(b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
   52                 return sock.recv(1024)
   53             except exp_exc:
   54                 return None
   55 
   56         # Should succeed - no timeout
   57         self.assertIn(greetings, get_request())
   58         # Should fail - connection timed out so we get nothing from the server
   59         self.assertFalse(get_request(delay=1.1))