Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Jan 18, 2024
1 parent ce625d5 commit 918ab3d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 86 deletions.
11 changes: 4 additions & 7 deletions features/signal/basic/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@

from harness.python.feature import Runner, register_feature

WORKFLOW_INITIAL_STATE = "1"
SIGNAL_ARG = "2"


@workflow.defn
class Workflow:
def __init__(self) -> None:
self._state = WORKFLOW_INITIAL_STATE
self._state = ""

@workflow.run
async def run(self) -> str:
await workflow.wait_condition(lambda: self._state != WORKFLOW_INITIAL_STATE)
await workflow.wait_condition(lambda: self._state != "")
return self._state

@workflow.signal
Expand All @@ -31,12 +28,12 @@ async def start(runner: Runner) -> WorkflowHandle:
task_queue=runner.task_queue,
execution_timeout=timedelta(minutes=1),
)
await handle.signal(Workflow.my_signal, SIGNAL_ARG)
await handle.signal(Workflow.my_signal, "arg")
return handle


register_feature(
workflows=[Workflow],
expect_run_result=SIGNAL_ARG,
expect_run_result="arg",
start=start,
)
10 changes: 3 additions & 7 deletions features/signal/child_workflow/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@

from harness.python.feature import Runner, register_feature

CHILD_WORKFLOW_INPUT = "child-input"
SIGNAL_ARG = "signal-arg"


@workflow.defn
class Workflow:
@workflow.run
async def run(self) -> str:
child_wf = await workflow.start_child_workflow(
ChildWorkflow.run, CHILD_WORKFLOW_INPUT
ChildWorkflow.run, "child-wf-arg"
)
await child_wf.signal(ChildWorkflow.my_signal, SIGNAL_ARG)
await child_wf.signal(ChildWorkflow.my_signal, "signal-arg")
return await child_wf


Expand All @@ -43,6 +39,6 @@ async def start(runner: Runner) -> WorkflowHandle:

register_feature(
workflows=[Workflow, ChildWorkflow],
expect_run_result=f"{CHILD_WORKFLOW_INPUT} {SIGNAL_ARG}",
expect_run_result=f"child-wf-arg signal-arg",
start=start,
)
59 changes: 0 additions & 59 deletions features/update/basic/feature.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<<<<<<< HEAD
import asyncio
from datetime import timedelta

Expand Down Expand Up @@ -38,61 +37,3 @@ async def check_result(runner: Runner, handle: WorkflowHandle) -> None:
check_result=check_result,
start=start,
)
||||||| parent of ea93e98 (Update features)
=======
from datetime import timedelta

from temporalio import workflow
from temporalio.client import WorkflowHandle, WorkflowUpdateFailedError

from harness.python.feature import Runner, register_feature

WORKFLOW_INITIAL_STATE = ""
UPDATE_ARG = "update-arg"
BAD_UPDATE_ARG = "reject-me"
UPDATE_RESULT = "update-result"


@workflow.defn(name="Workflow")
class Workflow:
"""
A workflow with a signal and signal validator.
If accepted, the signal makes a change to workflow state.
The workflow does not terminate until such a change occurs.
"""

def __init__(self) -> None:
self._state = WORKFLOW_INITIAL_STATE

@workflow.run
async def run(self) -> str:
await workflow.wait_condition(lambda: self._state != WORKFLOW_INITIAL_STATE)
return self._state

@workflow.update
async def my_update(self, arg: str) -> str:
self._state = arg
return UPDATE_RESULT

@my_update.validator
def my_validate(self, arg: str):
if arg == BAD_UPDATE_ARG:
raise ValueError("Invalid Update argument")


async def checker(_: Runner, handle: WorkflowHandle):
try:
await handle.execute_update(Workflow.my_update, BAD_UPDATE_ARG)
except WorkflowUpdateFailedError:
pass
else:
assert False, "Expected Update to be rejected due to validation failure"

update_result = await handle.execute_update(Workflow.my_update, UPDATE_ARG)
assert update_result == UPDATE_RESULT
result = await handle.result()
assert result == UPDATE_ARG


register_feature(workflows=[Workflow], check_result=checker)
>>>>>>> ea93e98 (Update features)
21 changes: 8 additions & 13 deletions features/update/basic_async/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

from harness.python.feature import Runner, register_feature

WORKFLOW_INITIAL_STATE = ""
UPDATE_ARG = "update-arg"
BAD_UPDATE_ARG = "reject-me"
UPDATE_RESULT = "update-result"


@workflow.defn
class Workflow:
Expand All @@ -20,38 +15,38 @@ class Workflow:
"""

def __init__(self) -> None:
self._state = WORKFLOW_INITIAL_STATE
self._state = ""

@workflow.run
async def run(self) -> str:
await workflow.wait_condition(lambda: self._state != WORKFLOW_INITIAL_STATE)
await workflow.wait_condition(lambda: self._state != "")
return self._state

@workflow.update
async def my_update(self, arg: str) -> str:
self._state = arg
return UPDATE_RESULT
return "update-result"

@my_update.validator
def my_validate(self, arg: str):
if arg == BAD_UPDATE_ARG:
if arg == "bad-update-arg":
raise ValueError("Invalid Update argument")


async def checker(_: Runner, handle: WorkflowHandle):
bad_update_handle = await handle.start_update(Workflow.my_update, BAD_UPDATE_ARG)
bad_update_handle = await handle.start_update(Workflow.my_update, "bad-update-arg")
try:
await bad_update_handle.result()
except WorkflowUpdateFailedError:
pass
else:
assert False, "Expected Update to be rejected due to validation failure"

update_handle = await handle.start_update(Workflow.my_update, UPDATE_ARG)
update_handle = await handle.start_update(Workflow.my_update, "update-arg")
update_result = await update_handle.result()
assert update_result == UPDATE_RESULT
assert update_result == "update-result"
result = await handle.result()
assert result == UPDATE_ARG
assert result == "update-arg"


register_feature(workflows=[Workflow], check_result=checker)

0 comments on commit 918ab3d

Please sign in to comment.