Skip to content

timeout caped to parent #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions resonate/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ def _eval(self, node: Node[State]) -> list[Function | Delayed[Function | Retry]
match cmd, child.value:
case LFI(conv, func, args, kwargs, opts), Enabled(Suspended(Init(next=None))):
cls = Coro if isgeneratorfunction(func) else Lfnc
next = cls(conv.id, self.id, conv, clock.time() + conv.timeout, func, args, kwargs, opts, self.ctx)
next = cls(conv.id, self.id, conv, min(clock.time() + conv.timeout, c.timeout), func, args, kwargs, opts, self.ctx)

node.add_edge(child)
node.add_edge(child, "waiting[p]")
Expand All @@ -802,7 +802,7 @@ def _eval(self, node: Node[State]) -> list[Function | Delayed[Function | Retry]
return []

case RFI(conv), Enabled(Suspended(Init(next=None))):
next = Rfnc(conv.id, self.id, conv, clock.time() + conv.timeout)
next = Rfnc(conv.id, self.id, conv, min(clock.time() + conv.timeout, c.timeout))

node.add_edge(child)
node.add_edge(child, "waiting[p]")
Expand Down
24 changes: 21 additions & 3 deletions tests/test_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ def info2(ctx: Context, *args: Any, **kwargs: Any) -> Generator[Yieldable, Any,
yield (yield ctx.detached(info1, f"{ctx.id}.5", {"resonate:scope": "global", "resonate:invoke": "poll://default"}, 0, 1))


def parent_bound(ctx: Context, parent_timeout_rel: int, child_timeout_rel: int) -> Generator[Yieldable, Any, None]:
yield ctx.lfc(child_bounded, ctx.info.timeout, parent_timeout_rel <= child_timeout_rel).options(timeout=child_timeout_rel)


def child_bounded(ctx: Context, parent_timeout_abs: float, parent_bound: bool) -> None:
if parent_bound:
assert pytest.approx(ctx.info.timeout) == parent_timeout_abs
else:
assert ctx.info.timeout < parent_timeout_abs


@pytest.fixture(scope="module")
def resonate_instance(store: Store, message_source: MessageSource) -> Generator[Resonate, None, None]:
resonate = Resonate(store=store, message_source=message_source)
Expand All @@ -167,6 +178,8 @@ def resonate_instance(store: Store, message_source: MessageSource) -> Generator[
resonate.register(random_generation)
resonate.register(info1, name="info", version=1)
resonate.register(info2, name="info", version=2)
resonate.register(parent_bound)
resonate.register(child_bounded)
resonate.start()
yield resonate
resonate.stop()
Expand All @@ -176,6 +189,11 @@ def resonate_instance(store: Store, message_source: MessageSource) -> Generator[
time.sleep(3)


@pytest.mark.parametrize(("parent_timeout", "child_timeout"), [(1100, 10), (10, 1100), (10, 10), (10, 11), (11, 10)])
def test_timeout_bound_by_parent(resonate_instance: Resonate, parent_timeout: int, child_timeout: int) -> None:
resonate_instance.options(timeout=parent_timeout).run(f"parent-bound-timeout-{uuid.uuid4()}", parent_bound, parent_timeout, child_timeout).result()


def test_random_generation(resonate_instance: Resonate) -> None:
timestamp = int(time.time())
handle = resonate_instance.run(f"random-gen-{timestamp}", random_generation)
Expand All @@ -185,10 +203,10 @@ def test_random_generation(resonate_instance: Resonate) -> None:

@pytest.mark.parametrize("id", ["foo", None])
def test_hitl(resonate_instance: Resonate, id: str | None) -> None:
timestamp = int(time.time())
handle = resonate_instance.run(f"hitl-{timestamp}", hitl, id)
uid = uuid.uuid4()
handle = resonate_instance.run(f"hitl-{uid}", hitl, id)
time.sleep(1)
resonate_instance.promises.resolve(id=id or f"hitl-{timestamp}.1", data=1)
resonate_instance.promises.resolve(id=id or f"hitl-{uid}.1", data=1)
assert handle.result() == 1


Expand Down
Loading