From cd17214ce49fa9bc453ed0abe23a4d7e9d37661c Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Thu, 6 Dec 2012 16:12:32 -0500 Subject: [PATCH] Improve a test that can fail due to a race condition. --- pecan/tests/test_scaffolds.py | 30 ++++++++++++++++++++---------- pecan/tests/test_secure.py | 10 ++++++++-- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/pecan/tests/test_scaffolds.py b/pecan/tests/test_scaffolds.py index 44e6e99..0dc916c 100644 --- a/pecan/tests/test_scaffolds.py +++ b/pecan/tests/test_scaffolds.py @@ -4,10 +4,10 @@ import tempfile import shutil import subprocess import pkg_resources -import httplib import urllib2 import time from cStringIO import StringIO + import pecan if sys.version_info < (2, 7): @@ -220,9 +220,8 @@ class TestTemplateBuilds(unittest.TestCase): ]) def poll(self, proc): - limit = 5 + limit = 30 for i in range(limit): - time.sleep(1) proc.poll() # Make sure it's running @@ -230,6 +229,7 @@ class TestTemplateBuilds(unittest.TestCase): break elif i == limit: # pragma: no cover raise RuntimeError("pecan serve config.py didn't start.") + time.sleep(.1) @unittest.skipUnless(has_internet(), 'Internet connectivity unavailable.') @unittest.skipUnless( @@ -248,13 +248,23 @@ class TestTemplateBuilds(unittest.TestCase): try: self.poll(proc) - - # ...and that it's serving (valid) content... - conn = httplib.HTTPConnection('localhost:8080') - conn.request('GET', '/') - resp = conn.getresponse() - assert resp.status == 200 - assert 'This is a sample Pecan project.' in resp.read() + retries = 30 + while True: + retries -= 1 + if retries < 0: # pragma: nocover + raise RuntimeError( + "The HTTP server has not replied within 3 seconds." + ) + try: + # ...and that it's serving (valid) content... + resp = urllib2.urlopen('http://localhost:8080/') + assert resp.getcode() == 200 + assert 'This is a sample Pecan project.' in resp.read() + except urllib2.URLError: + pass + else: + break + time.sleep(.1) finally: proc.terminate() diff --git a/pecan/tests/test_secure.py b/pecan/tests/test_secure.py index abd4518..749d045 100644 --- a/pecan/tests/test_secure.py +++ b/pecan/tests/test_secure.py @@ -1,8 +1,14 @@ -import unittest +import sys + +if sys.version_info < (2, 7): + import unittest2 as unittest +else: + import unittest # noqa + +from webtest import TestApp from pecan import expose, make_app from pecan.secure import secure, unlocked, SecureController -from webtest import TestApp try: set()