From a6821aad8e9296640c3efd4275e8922dd7c6e43b Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 14 Jul 2016 14:39:07 +1200 Subject: Zap old scripts infrastructure, fix concurrency tests --- test/mitmproxy/builtins/test_script.py | 13 +------ .../data/addonscripts/concurrent_decorator.py | 6 +++ .../data/addonscripts/concurrent_decorator_err.py | 6 +++ .../mitmproxy/data/scripts/concurrent_decorator.py | 7 ---- .../data/scripts/concurrent_decorator_err.py | 6 --- test/mitmproxy/mastertest.py | 10 +++++ test/mitmproxy/script/test_concurrent.py | 43 ++++++++++++++++------ 7 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 test/mitmproxy/data/addonscripts/concurrent_decorator.py create mode 100644 test/mitmproxy/data/addonscripts/concurrent_decorator_err.py delete mode 100644 test/mitmproxy/data/scripts/concurrent_decorator.py delete mode 100644 test/mitmproxy/data/scripts/concurrent_decorator_err.py (limited to 'test') diff --git a/test/mitmproxy/builtins/test_script.py b/test/mitmproxy/builtins/test_script.py index d3366189..2447c8ea 100644 --- a/test/mitmproxy/builtins/test_script.py +++ b/test/mitmproxy/builtins/test_script.py @@ -47,15 +47,6 @@ def test_load_script(): assert ns["configure"] -class RecordingMaster(master.FlowMaster): - def __init__(self, *args, **kwargs): - master.FlowMaster.__init__(self, *args, **kwargs) - self.event_log = [] - - def add_event(self, e, level): - self.event_log.append((level, e)) - - class TestScript(mastertest.MasterTest): def test_simple(self): s = state.State() @@ -77,7 +68,7 @@ class TestScript(mastertest.MasterTest): def test_reload(self): s = state.State() - m = RecordingMaster(options.Options(), None, s) + m = mastertest.RecordingMaster(options.Options(), None, s) with tutils.tmpdir(): with open("foo.py", "w"): pass @@ -94,7 +85,7 @@ class TestScript(mastertest.MasterTest): def test_exception(self): s = state.State() - m = RecordingMaster(options.Options(), None, s) + m = mastertest.RecordingMaster(options.Options(), None, s) sc = script.Script( tutils.test_data.path("data/addonscripts/error.py") ) diff --git a/test/mitmproxy/data/addonscripts/concurrent_decorator.py b/test/mitmproxy/data/addonscripts/concurrent_decorator.py new file mode 100644 index 00000000..a56c2af1 --- /dev/null +++ b/test/mitmproxy/data/addonscripts/concurrent_decorator.py @@ -0,0 +1,6 @@ +import time +from mitmproxy.script import concurrent + +@concurrent +def request(flow): + time.sleep(0.1) diff --git a/test/mitmproxy/data/addonscripts/concurrent_decorator_err.py b/test/mitmproxy/data/addonscripts/concurrent_decorator_err.py new file mode 100644 index 00000000..756869c8 --- /dev/null +++ b/test/mitmproxy/data/addonscripts/concurrent_decorator_err.py @@ -0,0 +1,6 @@ +from mitmproxy.script import concurrent + + +@concurrent +def start(): + pass diff --git a/test/mitmproxy/data/scripts/concurrent_decorator.py b/test/mitmproxy/data/scripts/concurrent_decorator.py deleted file mode 100644 index 162c00f4..00000000 --- a/test/mitmproxy/data/scripts/concurrent_decorator.py +++ /dev/null @@ -1,7 +0,0 @@ -import time -from mitmproxy.script import concurrent - - -@concurrent -def request(flow): - time.sleep(0.1) diff --git a/test/mitmproxy/data/scripts/concurrent_decorator_err.py b/test/mitmproxy/data/scripts/concurrent_decorator_err.py deleted file mode 100644 index 756869c8..00000000 --- a/test/mitmproxy/data/scripts/concurrent_decorator_err.py +++ /dev/null @@ -1,6 +0,0 @@ -from mitmproxy.script import concurrent - - -@concurrent -def start(): - pass diff --git a/test/mitmproxy/mastertest.py b/test/mitmproxy/mastertest.py index 9754d3a9..240f6a73 100644 --- a/test/mitmproxy/mastertest.py +++ b/test/mitmproxy/mastertest.py @@ -3,6 +3,7 @@ import mock from . import tutils import netlib.tutils +from mitmproxy.flow import master from mitmproxy import flow, proxy, models, controller @@ -39,3 +40,12 @@ class MasterTest: t = tutils.tflow(resp=True) fw.add(t) f.close() + + +class RecordingMaster(master.FlowMaster): + def __init__(self, *args, **kwargs): + master.FlowMaster.__init__(self, *args, **kwargs) + self.event_log = [] + + def add_event(self, e, level): + self.event_log.append((level, e)) diff --git a/test/mitmproxy/script/test_concurrent.py b/test/mitmproxy/script/test_concurrent.py index 57eeca19..d5243bcb 100644 --- a/test/mitmproxy/script/test_concurrent.py +++ b/test/mitmproxy/script/test_concurrent.py @@ -1,28 +1,47 @@ -from mitmproxy.script import Script from test.mitmproxy import tutils from mitmproxy import controller +from mitmproxy.builtins import script +from mitmproxy import options +from mitmproxy.flow import master +from mitmproxy.flow import state import time +from .. import mastertest, tutils class Thing: def __init__(self): self.reply = controller.DummyReply() + self.live = True -@tutils.skip_appveyor -def test_concurrent(): - with Script(tutils.test_data.path("data/scripts/concurrent_decorator.py")) as s: - f1, f2 = Thing(), Thing() - s.run("request", f1) - s.run("request", f2) +class TestConcurrent(mastertest.MasterTest): + @tutils.skip_appveyor + def test_concurrent(self): + s = state.State() + m = master.FlowMaster(options.Options(), None, s) + sc = script.Script( + tutils.test_data.path( + "data/addonscripts/concurrent_decorator.py" + ) + ) + m.addons.add(sc) + f1, f2 = tutils.tflow(), tutils.tflow() + self.invoke(m, "request", f1) + self.invoke(m, "request", f2) start = time.time() while time.time() - start < 5: if f1.reply.acked and f2.reply.acked: return raise ValueError("Script never acked") - -def test_concurrent_err(): - s = Script(tutils.test_data.path("data/scripts/concurrent_decorator_err.py")) - with tutils.raises("Concurrent decorator not supported for 'start' method"): - s.load() + def test_concurrent_err(self): + s = state.State() + m = mastertest.RecordingMaster(options.Options(), None, s) + sc = script.Script( + tutils.test_data.path( + "data/addonscripts/concurrent_decorator_err.py" + ) + ) + with m.handlecontext(): + sc.configure(options.Options()) + assert "decorator not supported" in m.event_log[0][1] -- cgit v1.2.3