From 14f461c5d59e95e84658ea43567b3ca9f7d0a108 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 2 Apr 2018 15:47:23 +1200 Subject: [PATCH] asyncio: cleanup and lint Also fix a racy websocket test. --- mitmproxy/addons/termstatus.py | 1 - mitmproxy/master.py | 5 +++-- mitmproxy/tools/web/master.py | 1 - test/mitmproxy/proxy/protocol/test_websocket.py | 7 ++++++- test/mitmproxy/test_flow.py | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/mitmproxy/addons/termstatus.py b/mitmproxy/addons/termstatus.py index 3801f3208..c3c912838 100644 --- a/mitmproxy/addons/termstatus.py +++ b/mitmproxy/addons/termstatus.py @@ -1,4 +1,3 @@ -import sys from mitmproxy import ctx from mitmproxy.utils import human diff --git a/mitmproxy/master.py b/mitmproxy/master.py index 022590d43..372bb289c 100644 --- a/mitmproxy/master.py +++ b/mitmproxy/master.py @@ -85,7 +85,8 @@ class Master: mitmproxy_ctx.log = None mitmproxy_ctx.options = None - def tell(self, mtype, m): + # This is a vestigial function that will go away in a refactor very soon + def tell(self, mtype, m): # pragma: no cover m.reply = controller.DummyReply() self.event_queue.put((mtype, m)) @@ -106,7 +107,7 @@ class Master: mtype, obj = await self.event_queue.get() except RuntimeError: return - if mtype not in eventsequence.Events: + if mtype not in eventsequence.Events: # pragma: no cover raise exceptions.ControlException("Unknown event %s" % repr(mtype)) self.addons.handle_lifecycle(mtype, obj) self.event_queue.task_done() diff --git a/mitmproxy/tools/web/master.py b/mitmproxy/tools/web/master.py index 843190e06..b7eddccee 100644 --- a/mitmproxy/tools/web/master.py +++ b/mitmproxy/tools/web/master.py @@ -3,7 +3,6 @@ import webbrowser import tornado.httpserver import tornado.ioloop from tornado.platform.asyncio import AsyncIOMainLoop -import asyncio from mitmproxy import addons from mitmproxy import log diff --git a/test/mitmproxy/proxy/protocol/test_websocket.py b/test/mitmproxy/proxy/protocol/test_websocket.py index 014490b74..f64f500c8 100644 --- a/test/mitmproxy/proxy/protocol/test_websocket.py +++ b/test/mitmproxy/proxy/protocol/test_websocket.py @@ -331,7 +331,12 @@ class TestPong(_WebSocketTest): assert frame.header.opcode == websockets.OPCODE.PONG assert frame.payload == b'foobar' - assert self.master.has_log("Pong Received from server", "info") + for i in range(20): + if self.master.has_log("Pong Received from server", "info"): + break + time.sleep(0.01) + else: + raise AssertionError("No pong seen") class TestClose(_WebSocketTest): diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index 9f1fb213f..4042de5b0 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -7,7 +7,7 @@ import mitmproxy.io from mitmproxy import flowfilter from mitmproxy import options from mitmproxy.io import tnetstring -from mitmproxy.exceptions import FlowReadException, ReplayException, ControlException +from mitmproxy.exceptions import FlowReadException, ReplayException from mitmproxy import flow from mitmproxy import http from mitmproxy.net import http as net_http