Ejemplo n.º 1
0
def threads_not_lingering(request):
    try:
        yield
    finally:
        threads_then = request.session._threads_at_startup
        threads_now = alive_threads()
        if threads_then != threads_now:
            request.session._threads_at_startup = threads_now
            raise DirtyTest(
                'Left over threads', os.environ.get('PYTEST_CURRENT_TEST'),
                _compare_eq_set(threads_now, threads_then, verbose=2))
Ejemplo n.º 2
0
def tasks_not_lingering(request, loop, event_loop, _recorded_tasks_at_startup):
    allow_lingering_tasks = False
    allow_count = 0
    marks = request.node.get_closest_marker('allow_lingering_tasks')
    if marks:
        allow_lingering_tasks = True
        allow_count = marks.kwargs.get('count', 0)
    try:
        yield
    finally:
        tasks_then = request.node._tasks_at_startup
        tasks_now = set(all_tasks(loop=loop))
        if tasks_then != tasks_now:
            request.node._tasks_at_startup = tasks_now
            pending = {task for task in tasks_now if task and not task.done()}
            if pending:
                diff = len(pending - tasks_then)
                if not allow_lingering_tasks or diff > allow_count:
                    raise DirtyTest(
                        'Left over tasks',
                        os.environ.get('PYTEST_CURRENT_TEST'),
                        _compare_eq_set(tasks_now, tasks_then, verbose=2))