Skip to content

Commit

Permalink
fix: task factory was reset to early
Browse files Browse the repository at this point in the history
Task were not stacking context.
  • Loading branch information
diefans committed Feb 22, 2021
1 parent 98b2811 commit ba62b16
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.41.5
current_version = 0.41.6
commit = False
tag = False

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.41.5
0.41.6
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def finalize_options(self):

setup_kwargs = {
"name": "buvar",
"version": "0.41.5",
"version": "0.41.6",
"description": "Asyncio plugins, components, dependency injection and configs",
"long_description": description,
"long_description_content_type": "text/x-rst",
Expand Down
2 changes: 1 addition & 1 deletion src/buvar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.41.5"
__version__ = "0.41.6"
__version_info__ = tuple(__version__.split("."))


Expand Down
36 changes: 18 additions & 18 deletions src/buvar/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,30 @@ async def run(tasks, *, evt_cancel=None):
if evt_cancel is None:
evt_cancel = context.add(Cancel())

# we elevate context stack for tasks
# we automatically elevate context stack for tasks
factory = context.set_task_factory()
try:
fut_tasks = asyncio.gather(*map(asyncio.create_task, tasks))

# stop staging if we finish in any way
fut_tasks.add_done_callback(lambda _: evt_cancel.set())

# wait for exit
await evt_cancel.wait()

if not fut_tasks.done():
# we were explicitelly stopped by cancel event
fut_tasks.cancel()
try:
await fut_tasks
except asyncio.CancelledError:
# silence our shutdown
pass
else:
return fut_tasks.result()
finally:
factory.reset()

# stop staging if we finish in any way
fut_tasks.add_done_callback(lambda _: evt_cancel.set())

# wait for exit
await evt_cancel.wait()

if not fut_tasks.done():
# we were explicitelly stopped by cancel event
fut_tasks.cancel()
try:
await fut_tasks
except asyncio.CancelledError:
# silence our shutdown
pass
else:
return fut_tasks.result()


def collect_plugin_args(plugin):
hints = typing.get_type_hints(plugin)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_di.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,30 @@ def adapt(cls, str: str) -> "Foo":

foo = await adapters.nject(Foo)
assert isinstance(foo, Foo)


@pytest.mark.asyncio
async def test_nject_deep_dependency_by_arg(adapters):
class Foo:
...

class Bar:
def __init__(self, foo):
self.foo = foo

@classmethod
def adapt_foo(cls, foo: Foo) -> "Bar":
return cls(foo=foo)

class Baz:
def __init__(self, bar):
self.bar = bar

@classmethod
def adapt_bar(cls, bar: Bar) -> "Baz":
return cls(bar=bar)

adapters.register(Bar.adapt_foo, Baz.adapt_bar)

baz = await adapters.nject(Baz, foo=Foo())
assert isinstance(baz, Baz)
25 changes: 25 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@ def test_run(event_loop):
assert e.value.args[1] == "foo_plugin"


def test_run_task_factory(event_loop):
import asyncio
from buvar import plugin, context

async def prepare():
async def task():
async def _sub1():
context.add("foo")
await asyncio.sleep(0)
assert "foo" == context.get(str)

async def _sub2():
context.add("bar")
await asyncio.sleep(0)
assert "bar" == context.get(str)

await asyncio.gather(
asyncio.create_task(_sub1()), asyncio.create_task(_sub2())
)

yield task()

result = plugin.stage(prepare, loop=event_loop)


def test_run_relative_out_of_packages(event_loop):
from buvar import plugin

Expand Down

0 comments on commit ba62b16

Please sign in to comment.