From 0ba10b61090274a1f2ca70a8fb1d68830cb44607 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 23 Apr 2018 13:19:20 +1200 Subject: addons/script: improve relability of reload test The granularity of mtime is surprisingly bad. Make the tests more robust against this, and promote has_log back to a public method, now that we have a few legitimate examples. --- mitmproxy/test/taddons.py | 4 ++-- test/mitmproxy/addons/test_script.py | 10 ++++++++-- test/mitmproxy/proxy/test_server.py | 2 +- test/mitmproxy/test_taddons.py | 4 ++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/mitmproxy/test/taddons.py b/mitmproxy/test/taddons.py index e0e4fef9..0505f9f7 100644 --- a/mitmproxy/test/taddons.py +++ b/mitmproxy/test/taddons.py @@ -35,7 +35,7 @@ class RecordingMaster(mitmproxy.master.Master): for i in self.logs: print("%s: %s" % (i.level, i.msg), file=outf) - def _has_log(self, txt, level=None): + def has_log(self, txt, level=None): for i in self.logs: if level and i.level != level: continue @@ -45,7 +45,7 @@ class RecordingMaster(mitmproxy.master.Master): async def await_log(self, txt, level=None): for i in range(20): - if self._has_log(txt, level): + if self.has_log(txt, level): return True else: await asyncio.sleep(0.1) diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py index fad514ae..ea942d34 100644 --- a/test/mitmproxy/addons/test_script.py +++ b/test/mitmproxy/addons/test_script.py @@ -1,3 +1,4 @@ +import asyncio import os import sys import traceback @@ -123,8 +124,13 @@ class TestScript: assert await tctx.master.await_log("Loading") tctx.master.clear() - f.write("\n") - assert await tctx.master.await_log("Loading") + for i in range(20): + f.write("\n") + if tctx.master.has_log("Loading"): + break + await asyncio.sleep(0.1) + else: + raise AssertionError("No reload seen") @pytest.mark.asyncio async def test_exception(self): diff --git a/test/mitmproxy/proxy/test_server.py b/test/mitmproxy/proxy/test_server.py index d7836104..3cb35138 100644 --- a/test/mitmproxy/proxy/test_server.py +++ b/test/mitmproxy/proxy/test_server.py @@ -827,7 +827,7 @@ class TestServerConnect(tservers.HTTPProxyTest): self.set_addons(AFakeResponse()) assert self.pathod("200").status_code == 200 asyncio.sleep(0.1) - assert not self.proxy.tmaster._has_log("serverconnect") + assert not self.proxy.tmaster.has_log("serverconnect") class AKillRequest: diff --git a/test/mitmproxy/test_taddons.py b/test/mitmproxy/test_taddons.py index 39dd65c2..67f64463 100644 --- a/test/mitmproxy/test_taddons.py +++ b/test/mitmproxy/test_taddons.py @@ -10,10 +10,10 @@ from mitmproxy import ctx @pytest.mark.asyncio async def test_recordingmaster(): with taddons.context() as tctx: - assert not tctx.master._has_log("nonexistent") + assert not tctx.master.has_log("nonexistent") assert not tctx.master.has_event("nonexistent") ctx.log.error("foo") - assert not tctx.master._has_log("foo", level="debug") + assert not tctx.master.has_log("foo", level="debug") assert await tctx.master.await_log("foo", level="error") -- cgit v1.2.3