additional test speedups

This commit is contained in:
Maximilian Hils 2020-12-20 00:50:40 +01:00
parent 2f725e5f02
commit 8ce4bc7142
4 changed files with 10 additions and 43 deletions

View File

@ -39,11 +39,14 @@ class RecordingMaster(mitmproxy.master.Master):
return False return False
async def await_log(self, txt, level=None): 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): for i in range(20):
if self.has_log(txt, level): if self.has_log(txt, level):
return True return True
else: else:
await asyncio.sleep(0.1) await asyncio.sleep(0.001)
return False return False
def clear(self): def clear(self):

View File

@ -246,6 +246,7 @@ class TestScriptLoader:
os.remove(tdata.path("mitmproxy/data/addonscripts/dummy.py")) os.remove(tdata.path("mitmproxy/data/addonscripts/dummy.py"))
await tctx.master.await_log("Removing") await tctx.master.await_log("Removing")
await asyncio.sleep(0.1)
assert not tctx.options.scripts assert not tctx.options.scripts
assert not sl.addons 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"] debug = [i.msg for i in tctx.master.logs if i.level == "debug"]
assert debug == [ assert debug == [
'c configure', 'c configure',
@ -318,7 +319,7 @@ class TestScriptLoader:
"%s/a.py" % rec, "%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"] debug = [i.msg for i in tctx.master.logs if i.level == "debug"]
assert debug == [ assert debug == [
'c done', 'c done',

View File

@ -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")

View File

@ -75,13 +75,13 @@ class Test_Format(unittest.TestCase):
self.assertEqual((expect, b''), tnetstring.pop(data)) self.assertEqual((expect, b''), tnetstring.pop(data))
def test_roundtrip_format_random(self): def test_roundtrip_format_random(self):
for _ in range(500): for _ in range(10):
v = get_random_object() v = get_random_object()
self.assertEqual(v, tnetstring.loads(tnetstring.dumps(v))) self.assertEqual(v, tnetstring.loads(tnetstring.dumps(v)))
self.assertEqual((v, b""), tnetstring.pop(tnetstring.dumps(v))) self.assertEqual((v, b""), tnetstring.pop(tnetstring.dumps(v)))
def test_roundtrip_format_unicode(self): def test_roundtrip_format_unicode(self):
for _ in range(500): for _ in range(10):
v = get_random_object() v = get_random_object()
self.assertEqual(v, tnetstring.loads(tnetstring.dumps(v))) self.assertEqual(v, tnetstring.loads(tnetstring.dumps(v)))
self.assertEqual((v, b''), tnetstring.pop(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()) self.assertEqual(b'OK', s.read())
def test_roundtrip_file_random(self): def test_roundtrip_file_random(self):
for _ in range(500): for _ in range(10):
v = get_random_object() v = get_random_object()
s = io.BytesIO() s = io.BytesIO()
tnetstring.dump(v, s) tnetstring.dump(v, s)