Skip to content

Set and get dependency #196

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 2 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 6 additions & 4 deletions resonate/resonate.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ def get(self, id: str) -> Handle[Any]:
fp.result()
return Handle(fv)

def set_dependency(self, name: str, obj: Any) -> None:
self._dependencies.add(name, obj)


# Context
class Context:
Expand All @@ -220,10 +223,6 @@ def __init__(self, id: str, info: Info, opts: Options, registry: Registry, depen
def id(self) -> str:
return self._id

@property
def deps(self) -> Dependencies:
return self._dependencies

@property
def info(self) -> Info:
return self._info
Expand Down Expand Up @@ -289,6 +288,9 @@ def sleep(self, secs: int) -> RFC:
self._counter += 1
return RFC(f"{self.id}.{self._counter}", Sleep(secs))

def get_dependency(self, name: str) -> Any:
return self._dependencies.get(name)

def _lfi_func(self, f: str | Callable) -> tuple[Callable, int, dict[int, Callable] | None]:
match f:
case str():
Expand Down
12 changes: 12 additions & 0 deletions tests/test_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def add_one(ctx: Context, n: int) -> int:
return n + 1


def get_dependency(ctx: Context) -> int:
return ctx.get_dependency("foo") + 1


def rfi_add_one_by_name(ctx: Context, n: int) -> Generator[Any, Any, int]:
v = yield ctx.rfc("add_one", n)
return v
Expand Down Expand Up @@ -137,11 +141,19 @@ def resonate_instance(request: pytest.FixtureRequest) -> Generator[Resonate, Non
resonate.register(sleep)
resonate.register(add_one)
resonate.register(rfi_add_one_by_name)
resonate.register(get_dependency)
resonate.start()
yield resonate
resonate.stop()


def test_get_dependency(resonate_instance: Resonate) -> None:
timestamp = int(time.time())
resonate_instance.set_dependency("foo", 1)
handle = resonate_instance.run(f"get-dependency-{timestamp}", get_dependency)
assert handle.result() == 2


def test_basic_lfi(resonate_instance: Resonate) -> None:
timestamp = int(time.time())
handle = resonate_instance.run(f"foo-lfi-{timestamp}", foo_lfi)
Expand Down
Loading