Exemple #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})
Exemple #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
Exemple #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 == {}
Exemple #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'
Exemple #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)
Exemple #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}
Exemple #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 == {}
Exemple #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 == {}
Exemple #9
0
def test_default_name():
    assert 'test_context_logging.py' in Context().name
Exemple #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
Exemple #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()
Exemple #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
Exemple #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
Exemple #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