add cleanup_after fixture

function-scoped fixture for shutting down servers

avoids servers leaking into neighbor tests without having to teardown the app itself after every test
This commit is contained in:
Min RK
2018-04-09 14:24:29 +02:00
parent 6d6e48f434
commit 2952f62726

View File

@@ -3,11 +3,12 @@
# Copyright (c) Jupyter Development Team. # Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License. # Distributed under the terms of the Modified BSD License.
import asyncio
import os import os
import logging import logging
from getpass import getuser from getpass import getuser
from subprocess import TimeoutExpired from subprocess import TimeoutExpired
import time
from unittest import mock from unittest import mock
from pytest import fixture, raises from pytest import fixture, raises
from tornado import ioloop, gen from tornado import ioloop, gen
@@ -25,6 +26,7 @@ import jupyterhub.services.service
# global db session object # global db session object
_db = None _db = None
@fixture @fixture
def db(): def db():
"""Get a db session""" """Get a db session"""
@@ -53,6 +55,26 @@ def io_loop(request):
return io_loop return io_loop
@fixture(autouse=True)
def cleanup_after(request):
"""function-scoped fixture to shutdown user servers
allows cleanup of servers between tests
without having to launch a whole new app
"""
try:
yield
finally:
if not MockHub.initialized():
return
app = MockHub.instance()
loop = asyncio.new_event_loop()
for uid, user in app.users.items():
for name, spawner in list(user.spawners.items()):
if spawner.active:
loop.run_until_complete(user.stop(name))
@fixture(scope='module') @fixture(scope='module')
def app(request, io_loop): def app(request, io_loop):
"""Mock a jupyterhub app for testing""" """Mock a jupyterhub app for testing"""