예제 #1
0
def test_fill_exception_last_context():
    try:
        with Context(data=1):
            with Context(data=2):
                raise Exception('error')
    except Exception as exc:
        assert exc.args == ('error', {'data': 2})
예제 #2
0
def test_context_passing_to_thread_with_separate_context():
    with Context(data=1):
        with ContextVarExecutor() as executor:
            wrapped_task = Context()(sync_task)
            executor.submit(wrapped_task).result()

        assert current_context['data'] == 1
예제 #3
0
def test_current_context():
    assert current_context == {}

    with Context(data='outer', outer=''):
        assert current_context == {'data': 'outer', 'outer': ''}

        with Context(data='inner', inner=''):
            assert current_context == {
                'data': 'inner',
                'outer': '',
                'inner': '',
            }

        assert current_context == {'data': 'outer', 'outer': ''}

    assert current_context == {}
예제 #4
0
def test_multithreading_context_leaking():
    with Context(data='start'):
        with ContextVarExecutor() as executor:
            for _ in executor.map(not_safe_long_task, range(5)):
                pass

        assert current_context['data'] == 'start'
예제 #5
0
def test_execution_time(mocker):
    mocker.spy(logger, 'info')
    ctx = Context()

    with ctx:
        pass

    logger.info.assert_called_once_with(  # type: ignore
        '%s: executed in %s', ctx.name, ANY)
예제 #6
0
def test_log_record(caplog):
    setup_log_record()

    logging.info('test')
    assert caplog.records[-1].context == ''

    with Context(data=1):
        logging.info('test')
        assert caplog.records[-1].context == {'data': 1}
예제 #7
0
def test_writing_to_context_info():
    assert current_context == {}

    with Context():
        assert current_context == {}

        current_context['test'] = 'test'
        assert current_context == {'test': 'test'}

    assert current_context == {}
예제 #8
0
def test_context_with_start_finish():
    ctx = Context(data='data')
    assert current_context == {}

    ctx.start()
    assert current_context == {'data': 'data'}

    ctx.finish()
    assert current_context == {}
예제 #9
0
def test_default_name():
    assert 'test_context_logging.py' in Context().name
예제 #10
0
def test_context_passing_to_thread_with_context_leaking():
    with Context(data=1):
        with ContextVarExecutor() as executor:
            executor.submit(sync_task).result()

        assert current_context['data'] == 2
예제 #11
0
def test_context_passing_to_thread_without_context_var_executor():
    with Context(data=1):
        with ThreadPoolExecutor() as executor:
            with pytest.raises(KeyError):
                executor.submit(sync_task).result()
예제 #12
0
async def test_context_passing_to_asyncio_task_with_separate_context():
    with Context(data=1):
        wrapped_task = Context()(async_task)
        await asyncio.create_task(wrapped_task())  # type: ignore
        assert current_context['data'] == 1
예제 #13
0
async def test_context_passing_to_asyncio_task_with_context_leaking():
    with Context(data=1):
        await asyncio.create_task(async_task())
        assert current_context['data'] == 2
예제 #14
0
def test_fill_exception_context():
    try:
        with Context(data=1):
            raise Exception('error')
    except Exception as exc:
        assert exc.args == ('error', {'data': 1})  # noqa:PT017