diff --git a/cortex/tests/test_webgl_serve.py b/cortex/tests/test_webgl_serve.py new file mode 100644 index 00000000..440e8d46 --- /dev/null +++ b/cortex/tests/test_webgl_serve.py @@ -0,0 +1,31 @@ +import cortex.webgl.serve as serve + + +def test_webapp_run_uses_current_ioloop(monkeypatch): + class FakeIOLoop: + def __init__(self): + self.started = False + + def start(self): + self.started = True + + class FakeHTTPServer: + def __init__(self, application, io_loop=None): + self.application = application + self.io_loop = io_loop + self.listen_port = None + + def listen(self, port): + self.listen_port = port + + fake_ioloop = FakeIOLoop() + + monkeypatch.setattr(serve.tornado.ioloop.IOLoop, "current", lambda: fake_ioloop) + monkeypatch.setattr(serve.tornado.httpserver, "HTTPServer", FakeHTTPServer) + + app = serve.WebApp([], 0) + app.run() + + assert app.ioloop is fake_ioloop + assert fake_ioloop.started + assert app.server.listen_port == 0 diff --git a/cortex/webgl/serve.py b/cortex/webgl/serve.py index 354c665d..3bfb0822 100644 --- a/cortex/webgl/serve.py +++ b/cortex/webgl/serve.py @@ -321,11 +321,7 @@ def n_clients(self) -> int: return num def run(self): - ioloop: tornado.ioloop.IOLoop = ( - tornado.ioloop.IOLoop() - ) # why is annotation necessary? - ioloop.clear_current() - ioloop.make_current() + ioloop: tornado.ioloop.IOLoop = tornado.ioloop.IOLoop.current() self.ioloop = ioloop application = tornado.web.Application(self.handlers, gzip=True) # If tornado version is 5.0 or greater, io_loop arg does not exist