async def test_tasks_with_context_child(): import asyncio from buvar import context context.add("foo") context.add(123) async def task_1(): await asyncio.sleep(0.02) context.add("bar") await asyncio.sleep(0.01) assert context.get(str) == "bar" assert context.get(int) == 123 async def task_2(): await asyncio.sleep(0.01) context.add("baz") await asyncio.sleep(0.02) assert context.get(str) == "baz" assert context.get(int) == 123 with context.child(): fut_1 = asyncio.create_task(task_1()) with context.child(): fut_2 = asyncio.create_task(task_2()) await fut_1 await fut_2 assert context.get(str) == "foo"
async def test_tasks_context_auto_stacking(event_loop): import asyncio from buvar import context context.set_task_factory(loop=event_loop) try: context.add("foo") context.add(123) async def task_1(): await asyncio.sleep(0.02) context.add("bar") await asyncio.sleep(0.01) assert context.get(str) == "bar" assert context.get(int) == 123 async def task_2(): await asyncio.sleep(0.01) context.add("baz") await asyncio.sleep(0.02) assert context.get(str) == "baz" assert context.get(int) == 123 fut_1 = asyncio.create_task(task_1()) fut_2 = asyncio.create_task(task_2()) await fut_1 await fut_2 finally: event_loop.get_task_factory().reset() assert event_loop.get_task_factory() is None
async def test(): context.add(Foo()) context.add(Foo(name="bar"), name="bar") baz, bum = await adapters.nject(Baz, Bum, bum=Bum()) assert isinstance(baz, Baz) assert baz == { "baz": True, "bam": { "foo": True, "name": None }, "foo": { "foo": True, "name": None }, "bim": "default", "bum": { "bum": True }, "bar": { "bar": True, "foo": { "foo": True, "name": "bar" } }, } assert bum == {"bum": True}
async def test_plugin(load: plugin.Loader): assert context.get(str) == "foo" context.add("bar", name="bar") async def task1(): assert context.get(str, name="bar") == "bar" asyncio.sleep(0.02) context.add("task1", name="bar") asyncio.sleep(0.01) assert context.get(str, name="bar") == "task1" async def task2(): assert context.get(str, name="bar") == "bar" asyncio.sleep(0.01) context.add("task2", name="bar") asyncio.sleep(0.02) assert context.get(str, name="bar") == "task2" yield task1() yield task2()
def test_global_context_child(): from buvar import context context.add("foo") context.add(123) with context.child(): context.add("bar") assert context.get(str) == "bar" assert context.get(int) == 123 assert context.get(str) == "foo"
async def prepare(): aiohttp_config = await di.nject(AioHttpConfig) aiohttp_app = context.add( aiohttp.web.Application(middlewares=[ aiohttp.web.normalize_path_middleware(), request_context ])) sl.info("Running web server", host=aiohttp_config.host, port=aiohttp_config.port) # return server task yield aiohttp.web._run_app( # noqa: W0212 aiohttp_app, host=aiohttp_config.host, port=aiohttp_config.port, print=None, access_log=aiohttp_config.access_log, )
async def prepare(): from buvar import context context.add("foobar")
async def task2(): assert context.get(str, name="bar") == "bar" asyncio.sleep(0.01) context.add("task2", name="bar") asyncio.sleep(0.02) assert context.get(str, name="bar") == "task2"
async def request_context(request, handler): with context.child(): context.add(request) return await handler(request)
async def _sub2(): context.add("bar") await asyncio.sleep(0) assert "bar" == context.get(str)
async def task_2(): await asyncio.sleep(0.01) context.add("baz") await asyncio.sleep(0.02) assert context.get(str) == "baz" assert context.get(int) == 123
async def plugin_bar(include: plugin.Loader): context.add("bar", "bar") yield bar() yield baz()
import aiohttp.web from buvar import fork, plugin, di, context from buvar_aiohttp import AioHttpConfig async def hello(request): return aiohttp.web.Response(body=b"Hello, world") async def prepare_aiohttp(load: plugin.Loader): await load("buvar_aiohttp") app = await di.nject(aiohttp.web.Application) app.router.add_route("GET", "/", hello) context.add(AioHttpConfig(host="0.0.0.0", port=5678)) fork.stage(prepare_aiohttp, forks=0, sockets=["tcp://:5678"])
async def prepare(teardown: plugin.Teardown): jobs = context.add(Jobs()) teardown.add(jobs.shutdown())
async def _sub1(): context.add("foo") await asyncio.sleep(0) assert "foo" == context.get(str)
async def prepare(include: plugin.Loader): await include(".bar_plugin:plugin_bar") context.add("foo", name="foo") return foo()
async def foo(): ctx = context.find(str) context.add(ctx, "foo_plugin") return ctx
async def prepare(include: plugin.Loader): di.register(Something, SomeService) await include(".server") app = context.get(aiohttp.web.Application) app.add_routes(routes) context.add(SomeComponent(foo="bar", bar=1.23))