Skip to content
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

chore: Add test for async foreign function #8

Merged
merged 3 commits into from
Feb 25, 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
6 changes: 3 additions & 3 deletions wandelscript/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ForeignFunction:
"""References a function that can be used inside a Wandelscript.

Args:
function: Handle to an arbitrary function.
function: Handle to an arbitrary function, even an async function.
pass_context (bool): Whether or not the function takes the current wandelscript context
as the first parameter. Defaults to False.
"""
Expand All @@ -27,10 +27,10 @@ def __post_init__(self):


def ff(fn: Callable, pass_context: bool = False) -> ForeignFunction:
"""Shortcut method to create a foreign function.
"""Shortcut method to create a ForeignFunction.

Args:
function: Handle to an arbitrary function.
function: Handle to an arbitrary function, even an async function.
pass_context (bool): Whether or not the function takes the current wandelscript context
as the first parameter. Defaults to False.
"""
Expand Down
27 changes: 25 additions & 2 deletions wandelscript/tests/test_runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import sys
import time
import uuid
Expand All @@ -11,9 +12,8 @@

from wandelscript import ProgramRun, ProgramRunner, ProgramRunState, run
from wandelscript.exception import NameError_, ProgramSyntaxError
from wandelscript.ffi import ForeignFunction
from wandelscript.ffi import ForeignFunction, ff
from wandelscript.runtime import ExecutionContext
from wandelscript.serializer import Vector3d
from wandelscript.simulation import get_robot_controller
from wandelscript.utils.runtime import Tee

Expand Down Expand Up @@ -92,6 +92,29 @@ def test_run():
assert not isinstance(sys.stdout, Tee)


async def custom_async_function() -> int:
await asyncio.sleep(0.1)
return 42


def test_run_with_foreign_async_function():
"""Showcase that Wandelscript supports also async foreign functions."""
code = """
a = custom_async_function()
print(a)
"""

runner = run(
code,
robot_cell,
default_robot="0@controller",
default_tcp="flange",
foreign_functions={"custom_async_function": ff(custom_async_function)},
)
stdout = runner.program_run.stdout
assert "42\n" == stdout


def test_program_runner():
program_runner = ProgramRunner("move via p2p() to [100, 0, 300, 0, pi, 0]", robot_cell)
assert uuid.UUID(str(program_runner.id)) is not None
Expand Down