Skip to content

Commit 7dbb30a

Browse files
committed
Fix failing AppVeyor Python2.7 tests
Since #885 the tests in TestUpdater and TestKeyRevocation fail on Appveyor Python 2.7 builds. After some live debugging, it turns out that the tests fail due to the extra amount of http requests to the simple http server (see tests/simple_server.py) that were added in #885. The simple server runs in a subprocess and is re-used for the entire TestCase. After a certain amount of requests it becomes unresponsive. Note that neither the subprocess exits (ps -W), nor does the port get closed (netstat -a). It just doesn't serve the request, making it time out and fail the test. The following script can be used to reproduce the issue (run in tests directory): ```python import subprocess import requests import random counter = 0 port = random.randint(30000, 45000) command = ['python', 'simple_server.py', str(port)] server_process = subprocess.Popen(command, stderr=subprocess.PIPE) url = 'http://localhost:'+str(port) + '/' sess = requests.Session() try: while True: sess.get(url, timeout=3) counter +=1 finally: print(counter) server_process.kill() ``` It fails repeatedly on the 69th request, but only if `stderr=subprocess.PIPE` is passed to Popen. Given that for each request the simple server writes about ~60 characters to stderr, e.g. ... ``` 127.0.0.1 - - [24/Feb/2020 12:01:23] "GET / HTTP/1.1" 200 - ``` ... it looks a lot like a full pipe buffer of size 4096. Note that the `bufsize` argument to Popen does not change anything. As a simple work around we silence the test server on Windows/Python2 to not fill the buffer. Signed-off-by: Lukas Puehringer <[email protected]>
1 parent a044d23 commit 7dbb30a

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

tests/simple_server.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535

3636
import sys
3737
import random
38+
import platform
3839

3940
import six
41+
from six.moves.SimpleHTTPServer import SimpleHTTPRequestHandler
4042

4143
PORT = 0
4244

@@ -55,7 +57,22 @@ def _port_gen():
5557
else:
5658
PORT = _port_gen()
5759

58-
Handler = six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler
59-
httpd = six.moves.socketserver.TCPServer(('', PORT), Handler)
60+
61+
class QuietHTTPRequestHandler(SimpleHTTPRequestHandler):
62+
"""A SimpleHTTPRequestHandler that does not write incoming requests to
63+
stderr. """
64+
def log_request(self, code='-', size='-'):
65+
pass
66+
67+
# NOTE: On Windows/Python2 tests that use this simple_server.py in a
68+
# subprocesses hang after a certain amount of requests (~68), if a PIPE is
69+
# passed as Popen's stderr argument. As a simple workaround we silence the
70+
# server on those Windows/Py2 to not fill the buffer.
71+
if six.PY2 and platform.system() == 'Windows':
72+
handler = QuietHTTPRequestHandler
73+
else:
74+
handler = SimpleHTTPRequestHandler
75+
76+
httpd = six.moves.socketserver.TCPServer(('', PORT), handler)
6077

6178
httpd.serve_forever()

0 commit comments

Comments
 (0)