From 1512368af614919747d9fb4e8f68c48d2b2734e3 Mon Sep 17 00:00:00 2001 From: Alexey Galkin Date: Thu, 2 Jul 2015 21:53:34 +0300 Subject: [PATCH] Improve code readability in functional test for the WSGIServer Change-Id: I43c484da4d33740f1356a69d7cfcc69704e2c210 Closes-Bug: #1470941 --- glance/tests/functional/test_wsgi.py | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/glance/tests/functional/test_wsgi.py b/glance/tests/functional/test_wsgi.py index ddf71b15e3..c6f9c93028 100644 --- a/glance/tests/functional/test_wsgi.py +++ b/glance/tests/functional/test_wsgi.py @@ -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))