Improve code readability in functional test for the WSGIServer

Change-Id: I43c484da4d33740f1356a69d7cfcc69704e2c210
Closes-Bug: #1470941
This commit is contained in:
Alexey Galkin 2015-07-02 21:53:34 +03:00
parent 737be8dd51
commit 1512368af6

View File

@ -15,7 +15,6 @@
"""Tests for `glance.wsgi`."""
import re
import socket
import time
@ -42,25 +41,15 @@ class TestWSGIServer(testtools.TestCase):
server = wsgi.Server()
server.start(hello_world, 0)
port = server.sock.getsockname()[1]
sock1 = socket.socket()
sock1.connect(("127.0.0.1", port))
fd = sock1.makefile('rw')
fd.write(b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
fd.flush()
def get_request(delay=0.0):
sock = socket.socket()
sock.connect(('127.0.0.1', port))
time.sleep(delay)
sock.send('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
return sock.recv(1024)
buf = fd.read()
# Should succeed - no timeout
self.assertTrue(re.search(greetings, buf))
sock2 = socket.socket()
sock2.connect(("127.0.0.1", port))
time.sleep(0.2)
fd = sock2.makefile('rw')
fd.write(b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
fd.flush()
buf = fd.read()
self.assertTrue(greetings in get_request())
# Should fail - connection timed out so we get nothing from the server
self.assertFalse(buf)
self.assertFalse(get_request(delay=0.2))