From 8ce4bc71427d2250cea10df623f5dcfe04a29b55 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Sun, 20 Dec 2020 00:50:40 +0100 Subject: [PATCH] additional test speedups --- mitmproxy/test/taddons.py | 5 +++- test/mitmproxy/addons/test_script.py | 5 ++-- test/mitmproxy/data/scripts/all.py | 37 ---------------------------- test/mitmproxy/io/test_tnetstring.py | 6 ++--- 4 files changed, 10 insertions(+), 43 deletions(-) delete mode 100644 test/mitmproxy/data/scripts/all.py diff --git a/mitmproxy/test/taddons.py b/mitmproxy/test/taddons.py index 03d56e3f7..fc2935d53 100644 --- a/mitmproxy/test/taddons.py +++ b/mitmproxy/test/taddons.py @@ -39,11 +39,14 @@ class RecordingMaster(mitmproxy.master.Master): return False async def await_log(self, txt, level=None): + # start with a sleep(0), which lets all other coroutines advance. + # often this is enough to not sleept at all. + await asyncio.sleep(0) for i in range(20): if self.has_log(txt, level): return True else: - await asyncio.sleep(0.1) + await asyncio.sleep(0.001) return False def clear(self): diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py index 6ad8a04f5..8e81109ea 100644 --- a/test/mitmproxy/addons/test_script.py +++ b/test/mitmproxy/addons/test_script.py @@ -246,6 +246,7 @@ class TestScriptLoader: os.remove(tdata.path("mitmproxy/data/addonscripts/dummy.py")) await tctx.master.await_log("Removing") + await asyncio.sleep(0.1) assert not tctx.options.scripts assert not sl.addons @@ -302,7 +303,7 @@ class TestScriptLoader: ] ) - await tctx.master.await_log("c configure") + await tctx.master.await_log("b configure") debug = [i.msg for i in tctx.master.logs if i.level == "debug"] assert debug == [ 'c configure', @@ -318,7 +319,7 @@ class TestScriptLoader: "%s/a.py" % rec, ] ) - await tctx.master.await_log("Loading") + await tctx.master.await_log("e configure") debug = [i.msg for i in tctx.master.logs if i.level == "debug"] assert debug == [ 'c done', diff --git a/test/mitmproxy/data/scripts/all.py b/test/mitmproxy/data/scripts/all.py deleted file mode 100644 index 95edfe977..000000000 --- a/test/mitmproxy/data/scripts/all.py +++ /dev/null @@ -1,37 +0,0 @@ -import mitmproxy -record = [] - - -def clientconnect(cc): - mitmproxy.ctx.log("XCLIENTCONNECT") - record.append("clientconnect") - - -def serverconnect(cc): - mitmproxy.ctx.log("XSERVERCONNECT") - record.append("serverconnect") - - -def request(f): - mitmproxy.ctx.log("XREQUEST") - record.append("request") - - -def response(f): - mitmproxy.ctx.log("XRESPONSE") - record.append("response") - - -def responseheaders(f): - mitmproxy.ctx.log("XRESPONSEHEADERS") - record.append("responseheaders") - - -def clientdisconnect(cc): - mitmproxy.ctx.log("XCLIENTDISCONNECT") - record.append("clientdisconnect") - - -def error(cc): - mitmproxy.ctx.log("XERROR") - record.append("error") diff --git a/test/mitmproxy/io/test_tnetstring.py b/test/mitmproxy/io/test_tnetstring.py index a3556eb0b..3f5469e5b 100644 --- a/test/mitmproxy/io/test_tnetstring.py +++ b/test/mitmproxy/io/test_tnetstring.py @@ -75,13 +75,13 @@ class Test_Format(unittest.TestCase): self.assertEqual((expect, b''), tnetstring.pop(data)) def test_roundtrip_format_random(self): - for _ in range(500): + for _ in range(10): v = get_random_object() self.assertEqual(v, tnetstring.loads(tnetstring.dumps(v))) self.assertEqual((v, b""), tnetstring.pop(tnetstring.dumps(v))) def test_roundtrip_format_unicode(self): - for _ in range(500): + for _ in range(10): v = get_random_object() self.assertEqual(v, tnetstring.loads(tnetstring.dumps(v))) self.assertEqual((v, b''), tnetstring.pop(tnetstring.dumps(v))) @@ -111,7 +111,7 @@ class Test_FileLoading(unittest.TestCase): self.assertEqual(b'OK', s.read()) def test_roundtrip_file_random(self): - for _ in range(500): + for _ in range(10): v = get_random_object() s = io.BytesIO() tnetstring.dump(v, s)