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.
This commit is contained in:
Aldo Cortesi 2018-04-23 13:19:20 +12:00
parent 44016a0de5
commit 0ba10b6109
4 changed files with 13 additions and 7 deletions

View File

@ -35,7 +35,7 @@ class RecordingMaster(mitmproxy.master.Master):
for i in self.logs: for i in self.logs:
print("%s: %s" % (i.level, i.msg), file=outf) 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: for i in self.logs:
if level and i.level != level: if level and i.level != level:
continue continue
@ -45,7 +45,7 @@ class RecordingMaster(mitmproxy.master.Master):
async def await_log(self, txt, level=None): async def await_log(self, txt, level=None):
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.1)

View File

@ -1,3 +1,4 @@
import asyncio
import os import os
import sys import sys
import traceback import traceback
@ -123,8 +124,13 @@ class TestScript:
assert await tctx.master.await_log("Loading") assert await tctx.master.await_log("Loading")
tctx.master.clear() tctx.master.clear()
for i in range(20):
f.write("\n") f.write("\n")
assert await tctx.master.await_log("Loading") if tctx.master.has_log("Loading"):
break
await asyncio.sleep(0.1)
else:
raise AssertionError("No reload seen")
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_exception(self): async def test_exception(self):

View File

@ -827,7 +827,7 @@ class TestServerConnect(tservers.HTTPProxyTest):
self.set_addons(AFakeResponse()) self.set_addons(AFakeResponse())
assert self.pathod("200").status_code == 200 assert self.pathod("200").status_code == 200
asyncio.sleep(0.1) asyncio.sleep(0.1)
assert not self.proxy.tmaster._has_log("serverconnect") assert not self.proxy.tmaster.has_log("serverconnect")
class AKillRequest: class AKillRequest:

View File

@ -10,10 +10,10 @@ from mitmproxy import ctx
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_recordingmaster(): async def test_recordingmaster():
with taddons.context() as tctx: 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") assert not tctx.master.has_event("nonexistent")
ctx.log.error("foo") 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") assert await tctx.master.await_log("foo", level="error")