diff --git a/test/test_app.py b/test/test_app.py index 1ae81fc7d..1046be20b 100644 --- a/test/test_app.py +++ b/test/test_app.py @@ -1,6 +1,6 @@ -import mock, socket, os, sys +import mock, socket, os, time from libmproxy import dump -from netlib import certutils +from netlib import certutils, tcp from libpathod.pathoc import Pathoc import tutils @@ -14,9 +14,16 @@ def get_free_port(): class AppTestMixin(object): def request(self, spec): - p = Pathoc(("127.0.0.1", self.port)) - p.connect() - return p.request(spec) + t_start = time.time() + while (time.time() - t_start) < 5: + try: + p = Pathoc(("127.0.0.1", self.port)) + p.connect() # might fail as the server might not be online yet. + return p.request(spec) + except tcp.NetLibError: + time.sleep(0.01) + assert False + def test_basic(self): assert self.request("get:/").status_code == 200 diff --git a/test/test_protocol_http.py b/test/test_protocol_http.py index 9e043049b..bf569fb7d 100644 --- a/test/test_protocol_http.py +++ b/test/test_protocol_http.py @@ -80,7 +80,7 @@ class TestHTTPResponse: assert HTTPResponse.from_stream(s, "GET").code == 204 s = StringIO(_s) - r = HTTPResponse.from_stream(s, "HEAD") + r = HTTPResponse.from_stream(s, "HEAD") # HEAD must not have content by spec. We should leave it on the pipe. assert r.code == 200 assert r.content == "" - tutils.raises("Invalid server response: 'content", HTTPResponse.from_stream, s, "GET") \ No newline at end of file + tutils.raises("Invalid server response: 'content", HTTPResponse.from_stream, s, "GET") diff --git a/test/test_script.py b/test/test_script.py index 2999a910c..139030668 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -86,13 +86,17 @@ class TestScript: f.reply = f.request.reply with mock.patch("libmproxy.controller.DummyReply.__call__") as m: + t_start = time.time() s.run("clientconnect", f) s.run("serverconnect", f) s.run("response", f) s.run("error", f) s.run("clientdisconnect", f) - time.sleep(0.1) - assert m.call_count == 5 + while (time.time() - t_start) < 1 and m.call_count <= 5: + if m.call_count == 5: + return + time.sleep(0.001) + assert False def test_concurrent_err(self): s = flow.State()