From fa6097e931f21acb37bc821b5bd37a7e2356c590 Mon Sep 17 00:00:00 2001 From: Benjamin Wohlwend Date: Mon, 16 Jul 2018 14:29:31 +0200 Subject: [PATCH] try to fix flaky appveyor tests test_urllib3 is quite flaky on appveyor. The assumption is that due to the slow executor machine, the http server used in this test isn't quick enough to boot up. Now we wait for the server to boot up. Signed-off-by: Benjamin Wohlwend --- conftest.py | 3 ++- tests/fixtures.py | 21 +++++++++++++++++++++ tests/instrumentation/urllib3_tests.py | 6 +++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/conftest.py b/conftest.py index cf60ddd95..5a80ef308 100644 --- a/conftest.py +++ b/conftest.py @@ -3,7 +3,8 @@ from os.path import abspath, dirname, join from tests.fixtures import (elasticapm_client, instrument, not_so_random, - sending_elasticapm_client, validating_httpserver) + sending_elasticapm_client, validating_httpserver, + waiting_httpserver) from tests.utils.compat import middleware_setting try: diff --git a/tests/fixtures.py b/tests/fixtures.py index 286a0df71..33cc5181b 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -2,6 +2,8 @@ import json import os import random +import socket +import time import zlib import jsonschema @@ -94,10 +96,17 @@ def elasticapm_client(request): client.close() +@pytest.fixture() +def waiting_httpserver(httpserver): + wait_for_http_server(httpserver) + return httpserver + + @pytest.fixture() def validating_httpserver(request): server = ValidatingWSGIApp() server.start() + wait_for_http_server(server) request.addfinalizer(server.stop) return server @@ -140,3 +149,15 @@ def instrument(): elasticapm.instrument() yield elasticapm.uninstrument() + + +def wait_for_http_server(httpserver, timeout=30): + start_time = time.time() + while True: + try: + sock = socket.create_connection(httpserver.server_address, timeout=0.1) + sock.close() + break + except socket.error: + if time.time() - start_time > timeout: + raise TimeoutError() diff --git a/tests/instrumentation/urllib3_tests.py b/tests/instrumentation/urllib3_tests.py index 929a01b2d..911f2c01e 100644 --- a/tests/instrumentation/urllib3_tests.py +++ b/tests/instrumentation/urllib3_tests.py @@ -6,10 +6,10 @@ @mock.patch("elasticapm.traces.TransactionsStore.should_collect") -def test_urllib3(should_collect, instrument, elasticapm_client, httpserver): +def test_urllib3(should_collect, instrument, elasticapm_client, waiting_httpserver): should_collect.return_value = False - httpserver.serve_content('') - url = httpserver.url + '/hello_world' + waiting_httpserver.serve_content('') + url = waiting_httpserver.url + '/hello_world' parsed_url = urlparse.urlparse(url) elasticapm_client.begin_transaction("transaction") expected_sig = 'GET {0}'.format(parsed_url.netloc)