diff options
| author | Maximilian Hils <git@maximilianhils.com> | 2016-03-18 20:36:57 +0100 | 
|---|---|---|
| committer | Maximilian Hils <git@maximilianhils.com> | 2016-03-19 03:04:55 +0100 | 
| commit | fb0b17ee93f463054c06f035f0d68a4f55c9b2af (patch) | |
| tree | 703ab97f210c7a31a35edc93b560814e31189bc3 | |
| parent | 7e49b8c186c45bd2284f45fc181192aa0025be87 (diff) | |
| download | mitmproxy-fb0b17ee93f463054c06f035f0d68a4f55c9b2af.tar.gz mitmproxy-fb0b17ee93f463054c06f035f0d68a4f55c9b2af.tar.bz2 mitmproxy-fb0b17ee93f463054c06f035f0d68a4f55c9b2af.zip | |
simplify tests for @concurrent
| -rw-r--r-- | test/mitmproxy/script/test_concurrent.py | 32 | ||||
| -rw-r--r-- | test/mitmproxy/scripts/concurrent_decorator.py | 26 | ||||
| -rw-r--r-- | test/mitmproxy/test_script.py | 64 | 
3 files changed, 33 insertions, 89 deletions
| diff --git a/test/mitmproxy/script/test_concurrent.py b/test/mitmproxy/script/test_concurrent.py new file mode 100644 index 00000000..a9c9e153 --- /dev/null +++ b/test/mitmproxy/script/test_concurrent.py @@ -0,0 +1,32 @@ +from threading import Event + +from mitmproxy.script import Script +from test.mitmproxy import tutils + + +class Dummy: +    def __init__(self, reply): +        self.reply = reply + + +@tutils.skip_appveyor +def test_concurrent(): +    with Script(tutils.test_data.path("scripts/concurrent_decorator.py"), None) as s: +        def reply(): +            reply.acked.set() +        reply.acked = Event() + +        f1, f2 = Dummy(reply), Dummy(reply) +        s.run("request", f1) +        f1.reply() +        s.run("request", f2) +        f2.reply() +        assert f1.reply.acked == reply.acked +        assert not reply.acked.is_set() +        assert reply.acked.wait(10) + + +def test_concurrent_err(): +    s = Script(tutils.test_data.path("scripts/concurrent_decorator_err.py"), None) +    with tutils.raises("Concurrent decorator not supported for 'start' method"): +        s.load() diff --git a/test/mitmproxy/scripts/concurrent_decorator.py b/test/mitmproxy/scripts/concurrent_decorator.py index 6651c811..cf3ab512 100644 --- a/test/mitmproxy/scripts/concurrent_decorator.py +++ b/test/mitmproxy/scripts/concurrent_decorator.py @@ -1,32 +1,6 @@  import time  from mitmproxy.script import concurrent - -@concurrent -def clientconnect(context, cc): -    context.log("clientconnect") - - -@concurrent -def serverconnect(context, sc): -    context.log("serverconnect") - -  @concurrent  def request(context, flow):      time.sleep(0.1) - - -@concurrent -def response(context, flow): -    context.log("response") - - -@concurrent -def error(context, err): -    context.log("error") - - -@concurrent -def clientdisconnect(context, dc): -    context.log("clientdisconnect") diff --git a/test/mitmproxy/test_script.py b/test/mitmproxy/test_script.py index b38ea041..f321d15c 100644 --- a/test/mitmproxy/test_script.py +++ b/test/mitmproxy/test_script.py @@ -1,6 +1,4 @@ -import time -import mock -from mitmproxy import script, flow +from mitmproxy import flow  from . import tutils @@ -13,63 +11,3 @@ def test_duplicate_flow():      assert fm.state.flow_count() == 2      assert not fm.state.view[0].request.is_replay      assert fm.state.view[1].request.is_replay - - -@tutils.skip_appveyor -def test_concurrent(): -    s = flow.State() -    fm = flow.FlowMaster(None, s) -    fm.load_script(tutils.test_data.path("scripts/concurrent_decorator.py")) - -    with mock.patch("mitmproxy.controller.DummyReply.__call__") as m: -        f1, f2 = tutils.tflow(), tutils.tflow() -        t_start = time.time() -        fm.handle_request(f1) -        f1.reply() -        fm.handle_request(f2) -        f2.reply() - -        # Two instantiations -        assert m.call_count == 0  # No calls yet. -        assert (time.time() - t_start) < 0.1 - - -def test_concurrent2(): -    s = flow.State() -    fm = flow.FlowMaster(None, s) -    s = script.Script( -        tutils.test_data.path("scripts/concurrent_decorator.py"), -        script.ScriptContext(fm)) -    s.load() -    m = mock.Mock() - -    class Dummy: - -        def __init__(self): -            self.response = self -            self.error = self -            self.reply = m - -    t_start = time.time() - -    for hook in ("clientconnect", -                 "serverconnect", -                 "response", -                 "error", -                 "clientconnect"): -        d = Dummy() -        s.run(hook, d) -        d.reply() -    while (time.time() - t_start) < 20 and m.call_count <= 5: -        if m.call_count == 5: -            return -        time.sleep(0.001) -    assert False - - -def test_concurrent_err(): -    s = flow.State() -    fm = flow.FlowMaster(None, s) -    with tutils.raises("Concurrent decorator not supported for 'start' method"): -        s = script.Script(tutils.test_data.path("scripts/concurrent_decorator_err.py"), fm) -        s.load()
\ No newline at end of file | 
