def test_run(event_loop): from buvar import plugin, context, ComponentLookupError result = plugin.stage("tests.foo_plugin", loop=event_loop) assert result == [{"foo": "foo"}, None, None] with pytest.raises(ComponentLookupError) as e: context.get("foo_plugin") assert e.value.args[1] == "foo_plugin"
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 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 index(request): # we need to define a string, since `foo` has no default in `Something` service = await di.nject(SomeService, somestring="bar") assert service.something.foo == "bar" # but a named string is preferred service = await di.nject(SomeService, somestring="bar", foo="foo") assert service.something.foo == "foo" assert service.request is request assert service.something.component is context.get(SomeComponent) return json_response({"hello": "world", "something": service})
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()
async def test_bg_error(log_output, Anything): # TODO XXX FIXME without buvar_stage, I get # --- Logging error --- # Traceback (most recent call last): # File "/home/olli/.pyenv/versions/3.7.4/lib/python3.7/logging/__init__.py", line 1028, in emit # stream.write(msg + self.terminator) # ValueError: I/O operation on closed file. from buvar.plugins import bg from buvar import context async def make_error(): raise Exception("foobar") jobs = context.get(bg.Jobs) jobs.add(make_error()) await jobs assert { "event": "Background job failed", "exc_info": Anything, "log_level": "error", } in log_output.entries
async def test_bg_semaphore(): import asyncio from buvar.plugins import bg from buvar import context state = {"counter": 0, "sync": []} k = 3 sem = asyncio.Semaphore(k) async def count(): state["counter"] += 1 await asyncio.sleep(0) state["sync"].append(state["counter"] % k) jobs = context.get(bg.Jobs) i = k * 10 for _ in range(i): jobs.add(count(), sync=sem) await jobs assert state == {"counter": i, "sync": [0] * i}
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 bar(): context.get("bar")
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))
async def _sub2(): context.add("bar") await asyncio.sleep(0) assert "bar" == context.get(str)
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 _sub1(): context.add("foo") await asyncio.sleep(0) assert "foo" == context.get(str)
async def test_wrapped_stage_context_load(buvar_load): from buvar import context, plugin await buvar_load(prepare) assert context.get(str) == "foobar" assert context.get(plugin.Cancel)
async def test_wrapped_stage_context(): from buvar import context, plugin assert context.get(str) == "foobar" assert context.get(plugin.Cancel)