Ejemplo n.º 1
0
def test_sync_data():
    ctx = contextlib.get_call_context()
    ctx.set_data('foo', 23, sync=False)

    rv = []
    rv.append(contextlib.get_call_context().get_data('foo', default=None))

    def test_thread():
        rv.append(contextlib.get_call_context().get_data('foo', default=None))

    t = threading.Thread(target=test_thread)
    t.start()
    t.join()

    assert rv == [23, None]
Ejemplo n.º 2
0
def test_context_nesting():
    ctx = contextlib.get_call_context()
    with ctx.nested():
        ctx.set_data('bar', 23)

    with pytest.raises(LookupError):
        ctx.get_data('bar')
Ejemplo n.º 3
0
 def better_ensure_future(coro_or_future, *, loop=None):
     ctx = contextlib.get_call_context()
     task = ensure_future(coro_or_future, loop=loop)
     new_ctx = contextlib.new_call_context(name='Task-0x%x' % id(task),
                                           parent=ctx)
     set_task_call_context(task, new_ctx)
     return task
Ejemplo n.º 4
0
def test_basic_context():
    ctx = contextlib.get_call_context()
    ctx.set_data('foo', 42)
    assert ctx.get_data('foo') == 42
    ctx.del_data('foo')

    with pytest.raises(LookupError):
        ctx.get_data('foo')
Ejemplo n.º 5
0
def test_async():
    ctx = contextlib.get_call_context()
    ctx.set_data('__locale__', 'en_US')

    rv = []

    async def x(val):
        ctx = contextlib.get_call_context()
        rv.append(ctx.get_data('__locale__'))
        ctx.set_data('__locale__', val)
        rv.append(ctx.get_data('__locale__'))

    asyncio.get_event_loop().run_until_complete(x('de_DE'))
    asyncio.get_event_loop().run_until_complete(x('fr_FR'))
    rv.append(ctx.get_data('__locale__'))

    assert rv == ['en_US', 'de_DE', 'en_US', 'fr_FR', 'en_US']
Ejemplo n.º 6
0
def test_local_context_behavior():
    ctx = contextlib.get_call_context()
    ctx.set_data('foo', 23, local=True)

    with ctx.nested():
        assert ctx.get_data('foo') == 23

    rv = []
    def test_thread():
        rv.append(contextlib.get_call_context().get_data('foo', default=None))

    t = threading.Thread(target=test_thread)
    t.start()
    t.join()

    with contextlib.isolated_call_context():
        t = threading.Thread(target=test_thread)
        t.start()
        t.join()

    assert rv == [23, None]
Ejemplo n.º 7
0
def isolated_call_context(isolate=True):
    """Context manager that temporarily isolates the call context.  This means
    that new contexts created out of the current context until the end of the
    context manager will be created isolated from the current one.  All values
    that are marked as "local" will be unavailable in the newly created call
    context.

    When a context is created while the parent is isolated a new logical call
    context will be created.

    Example::

        import contextlib

        with contextlib.isolated_call_context():
            ...
    """
    ctx = contextlib.get_call_context()
    old = ctx.isolates
    ctx.isolates = isolate
    try:
        yield
    finally:
        ctx.isolates = old
Ejemplo n.º 8
0
 async def x(val):
     ctx = contextlib.get_call_context()
     rv.append(ctx.get_data('__locale__'))
     ctx.set_data('__locale__', val)
     rv.append(ctx.get_data('__locale__'))
Ejemplo n.º 9
0
 def test_thread():
     rv.append(contextlib.get_call_context().get_data('foo', default=None))
Ejemplo n.º 10
0
 def better_thread_init(self, *args, **kwargs):
     self.__outer_call_ctx = contextlib.get_call_context()
     return thread_init(self, *args, **kwargs)