From 976b2018a3fc320272ac4f588250977fc08cf9b5 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 31 Mar 2018 17:06:09 +1300 Subject: asyncio: clean up event loop acquisition We now acquire the event loop through asyncio.get_event_loop, avoiding having to pass the loop explicity in a bunch of places. This function does not return the currently running loop from within coroutines in versions of Python prior to 3.6. --- test/mitmproxy/test_controller.py | 89 ++++----------------------------------- 1 file changed, 9 insertions(+), 80 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/test_controller.py b/test/mitmproxy/test_controller.py index e27f6baf..f7c64ed9 100644 --- a/test/mitmproxy/test_controller.py +++ b/test/mitmproxy/test_controller.py @@ -1,102 +1,31 @@ import asyncio -from threading import Thread, Event -from unittest.mock import Mock import queue import pytest -import sys from mitmproxy.exceptions import Kill, ControlException from mitmproxy import controller -from mitmproxy import master -from mitmproxy import proxy from mitmproxy.test import taddons -class TMsg: - pass +@pytest.mark.asyncio +async def test_master(): + class TMsg: + pass - -def test_master(): class tAddon: def log(self, _): ctx.master.should_exit.set() - with taddons.context() as ctx: - ctx.master.addons.add(tAddon()) + with taddons.context(tAddon()) as ctx: assert not ctx.master.should_exit.is_set() async def test(): msg = TMsg() msg.reply = controller.DummyReply() - await ctx.master.event_queue.put(("log", msg)) - - ctx.master.run(inject=test) - - -# class TestMaster: -# # def test_simple(self): -# # class tAddon: -# # def log(self, _): -# # ctx.master.should_exit.set() - -# # with taddons.context() as ctx: -# # ctx.master.addons.add(tAddon()) -# # assert not ctx.master.should_exit.is_set() -# # msg = TMsg() -# # msg.reply = controller.DummyReply() -# # ctx.master.event_queue.put(("log", msg)) -# # ctx.master.run() -# # assert ctx.master.should_exit.is_set() - -# # def test_server_simple(self): -# # m = master.Master(None) -# # m.server = proxy.DummyServer() -# # m.start() -# # m.shutdown() -# # m.start() -# # m.shutdown() -# pass - - -# class TestServerThread: -# def test_simple(self): -# m = Mock() -# t = master.ServerThread(m) -# t.run() -# assert m.serve_forever.called - - -# class TestChannel: -# def test_tell(self): -# q = queue.Queue() -# channel = controller.Channel(q, Event()) -# m = Mock(name="test_tell") -# channel.tell("test", m) -# assert q.get() == ("test", m) -# assert m.reply - -# def test_ask_simple(self): -# q = queue.Queue() - -# def reply(): -# m, obj = q.get() -# assert m == "test" -# obj.reply.send(42) -# obj.reply.take() -# obj.reply.commit() - -# Thread(target=reply).start() - -# channel = controller.Channel(q, Event()) -# assert channel.ask("test", Mock(name="test_ask_simple")) == 42 - -# def test_ask_shutdown(self): -# q = queue.Queue() -# done = Event() -# done.set() -# channel = controller.Channel(q, done) -# with pytest.raises(Kill): -# channel.ask("test", Mock(name="test_ask_shutdown")) + await ctx.master.channel.tell("log", msg) + + asyncio.ensure_future(test()) + assert not ctx.master.should_exit.is_set() class TestReply: -- cgit v1.2.3