def test_query_workflow(): factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_workflow_implementation_type(TestQueryWorkflowImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestQueryWorkflow = client.new_workflow_stub(TestQueryWorkflow) workflow_ec = WorkflowClient.start(workflow.get_greetings) assert workflow.get_message() == "initial-message" workflow.put_message("second-message") assert workflow.get_message() == "second-message" with pytest.raises(QueryFailureException) as exc_info: workflow.get_message_fail() ex = exc_info.value assert isinstance(ex.__cause__, GreetingException) workflow.put_message("done") client.wait_for_close(workflow_ec) assert workflow.get_message() == "done" print("Stopping workers") worker.stop()
def test_current_time(): factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_workflow_implementation_type(TestCurrentTimeImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestCurrentTime = client.new_workflow_stub(TestCurrentTime) workflow.get_greetings() assert len(timestamps["checkpoint-1"]) >= 3 assert len(timestamps["checkpoint-2"]) >= 2 assert len(timestamps["checkpoint-3"]) >= 1 for checkpoint, values in timestamps.items(): assert all(v == values[0] for v in values) assert (timestamps["checkpoint-2"][0] - timestamps["checkpoint-1"][0]).total_seconds() > 20 assert (timestamps["checkpoint-3"][0] - timestamps["checkpoint-2"][0]).total_seconds() > 30 print("Stopping workers") worker.stop()
def test_workflow_workflow_get_version(): global v1_hits, v2_hits factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_workflow_implementation_type(TestWorkflowGetVersionImplV1) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestWorkflowGetVersion = client.new_workflow_stub( TestWorkflowGetVersion) client.start(workflow.get_greetings) while v1_hits == 0: print(".", end="") sleep(2) worker.register_workflow_implementation_type(TestWorkflowGetVersionImplV2) while not v2_done: print(".", end="") sleep(2) assert v1_hits == 1 assert v2_hits == 1 assert version_found_in_v2_step_1_0 == DEFAULT_VERSION assert version_found_in_v2_step_1_1 == DEFAULT_VERSION assert version_found_in_v2_step_2_0 == DEFAULT_VERSION assert version_found_in_v2_step_2_1 == DEFAULT_VERSION # TODO: Assert that there are no markers recorded print("Stopping workers") worker.stop(background=True)
def test_signal_workflow(): factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_workflow_implementation_type(TestSignalWorkflowImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestSignalWorkflow = client.new_workflow_stub(TestSignalWorkflow) execution = WorkflowClient.start(workflow.get_greetings) sleep(randint(0, 20)) workflow.wait_for_name("Bob") sleep(randint(0, 20)) workflow.exit() sleep(randint(0, 20)) result = client.wait_for_close(execution) worker.stop() assert result == ["Hello Bob!"]
def test_await_condition_no_timeout(): factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_workflow_implementation_type(TestAwaitTimeoutWorkflowImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestAwaitTimeoutWorkflow = client.new_workflow_stub( TestAwaitTimeoutWorkflow) execution = WorkflowClient.start(workflow.get_greetings_no_timeout) time.sleep(10) workflow.wait_for_name("Bob") result = client.wait_for_close(execution) assert result is True print("Stopping workers") worker.stop()
def test_stub_workflow_id(): factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_workflow_implementation_type(TestStubWorkflowIdImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestStubWorkflowId = client.new_workflow_stub(TestStubWorkflowId) context = WorkflowClient.start(workflow.get_greetings) stub: TestStubWorkflowId = client.new_workflow_stub_from_workflow_id( TestStubWorkflowId, workflow_id=context.workflow_execution.workflow_id) stub.put_message("abc") assert stub.get_message() == "abc" stub.put_message("done") assert client.wait_for_close_with_workflow_id( context.workflow_execution.workflow_id) == "finished" print("Stopping workers") worker.stop()
def test_sleep_workflow(): factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_workflow_implementation_type(TestSleepWorkflowImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestSleepWorkflow = client.new_workflow_stub(TestSleepWorkflow) start_time = time.time() workflow.get_greetings() end_time = time.time() assert end_time - start_time > 50 print("Stopping workers") worker.stop()
def test_workflow_logger(): reset_counter_filter_counter() logging.config.dictConfig(LOGGING) factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_workflow_implementation_type(TestWorkflowLoggerImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestWorkflowLogger = client.new_workflow_stub(TestWorkflowLogger) workflow.get_greetings() assert get_counter_filter_counter() == 5 print("Stopping workers") worker.stop()
def test_heartbeat_workflow(): factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) activities_impl = GreetingActivitiesImpl() worker.register_activities_implementation(activities_impl, "GreetingActivities") worker.register_workflow_implementation_type(TestHeartbeatWorkflowImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestHeartbeatWorkflow = client.new_workflow_stub(TestHeartbeatWorkflow) workflow.get_greetings("Bob") assert activities_impl.details == [None, 1, 2] assert activities_impl.invocation_count == 3 print("Stopping workers") worker.stop()
def test_workflow_workflow_get_version_single(): factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_workflow_implementation_type(TestWorkflowGetVersionSingleImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestWorkflowGetVersionSingle = client.new_workflow_stub(TestWorkflowGetVersionSingle) workflow.get_greetings() assert version_found_in_step_1_0 == 2 assert version_found_in_step_1_1 == 2 assert version_found_in_step_2_0 == 2 assert version_found_in_step_2_1 == 2 # TODO: Assert that there is only a single marker recorded print("Stopping workers") worker.stop(background=True)
def test_workflow_activity_exception(): factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) activities_impl = GreetingActivitiesImpl() worker.register_activities_implementation(activities_impl, "GreetingActivities") worker.register_workflow_implementation_type(TestActivityExceptionWorkflowImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestActivityExceptionWorkflow = client.new_workflow_stub(TestActivityExceptionWorkflow) workflow_ex = None try: workflow.get_greetings("Bob") except Exception as ex: workflow_ex = ex assert isinstance(workflow_ex, WorkflowFailureException) assert isinstance(workflow_ex.__cause__, ActivityFailureException) assert isinstance(workflow_ex.__cause__.__cause__, ComposeGreetingException) assert exception_caught assert isinstance(exception_caught, ActivityFailureException) assert isinstance(exception_caught.get_cause(), ComposeGreetingException) exception_caught.set_cause() cause = exception_caught.__cause__ assert isinstance(cause, ComposeGreetingException) assert cause.args == ("Failed to compose greeting",) tb = "".join(traceback.format_exception(type(cause), cause, cause.__traceback__)) assert "SOURCE OF EXCEPTION" in tb tb = "".join(traceback.format_exception(type(exception_caught), exception_caught, exception_caught.__traceback__)) assert "SOURCE OF EXCEPTION" in tb assert "WORKFLOW METHOD INVOKING ACTIVITY" in tb tb = "".join(traceback.format_exception(type(workflow_ex), workflow_ex, workflow_ex.__traceback__)) assert "SOURCE OF EXCEPTION" in tb assert "WORKFLOW METHOD INVOKING ACTIVITY" in tb print("Stopping workers") worker.stop()
def test_workflow_random(): checkpoint_values.clear() factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_workflow_implementation_type(TestRandomWorkflowImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestRandomWorkflow = client.new_workflow_stub(TestRandomWorkflow) workflow.get_greetings() # Verify that the value is always the same at each checkpoint for checkpoint, values in checkpoint_values.items(): assert all(v == values[0] for v in values) # Verify that each checkpoint produced a unique value values = [v[0] for k, v in checkpoint_values.items()] assert len(set(values)) == 9 print("Stopping workers") worker.stop()
def start(self) -> Iterator[None]: """ Context manager for initializing execution. Creates a `dask.distributed.Client` and yields it. """ try: if self.address is None: self.kwargs.update(silence_logs=logging.CRITICAL if not self.debug else logging.WARNING) self.kwargs.update(processes=self.local_processes) self.client = WorkflowClient.new_client(domain=DOMAIN) # result = greeting_workflow.get_greeting("Python") # with Client(self.address, **self.kwargs) as client: # self.hydra_client = Client(self.control_address) # self.client = client # self.is_started = True yield self.client finally: self.client = None self.is_started = False
def test_activity_context(): factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_activities_implementation(GreetingActivitiesImpl(), "GreetingActivities") worker.register_workflow_implementation_type(TestActivityContextImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) workflow: TestActivityContext = client.new_workflow_stub( TestActivityContext) workflow.get_greetings("Bob") global task_token, workflow_execution, domain assert task_token is not None assert workflow_execution is not None assert workflow_execution.workflow_id is not None assert workflow_execution.run_id is not None assert domain is not None assert domain == DOMAIN print("Stopping workers") worker.stop()
def workflow_client(workflow_service): return WorkflowClient(service=workflow_service, domain="domain", options=None)
from cadence.workflow import workflow_method, WorkflowClient, WorkflowExecutionFailedException, \ WorkflowExecutionTimedOutException, WorkflowExecutionTerminatedException class GreetingWorkflow: @workflow_method(name='GreetingWorkflow::getGreeting', execution_start_to_close_timeout_seconds=60 * 10, task_list='python-tasklist') def get_greeting(self, name): pass if __name__ == '__main__': client = WorkflowClient.new_client(domain="sample") greeting_workflow: GreetingWorkflow = client.new_workflow_stub( GreetingWorkflow) try: result = greeting_workflow.get_greeting("Python") print(result) except WorkflowExecutionTerminatedException as ex: print(f"Workflow terminated: {ex}") except WorkflowExecutionTimedOutException as ex: print(f"Workflow timed out: {ex}") except WorkflowExecutionFailedException as ex: print(f"Workflow execution failed: {ex}")
# Workflow Implementation class GreetingWorkflowImpl(GreetingWorkflow): def __init__(self): self.greeting_activities: GreetingActivities = Workflow.new_activity_stub(GreetingActivities) async def get_greeting(self, name): print("Workflow executed args:" + name) return await self.greeting_activities.compose_greeting("Hello", name) if __name__ == '__main__': factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_activities_implementation(GreetingActivitiesImpl(), "GreetingActivities") worker.register_workflow_implementation_type(GreetingWorkflowImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) greeting_workflow: GreetingWorkflow = client.new_workflow_stub(GreetingWorkflow) execution = WorkflowClient.start(greeting_workflow.get_greeting, "Python") print("Started: workflow_id={} run_id={}".format(execution.workflow_execution.workflow_id, execution.workflow_execution.run_id)) print("Result: " + str(client.wait_for_close(execution))) print("Stopping workers....") worker.stop() print("Workers stopped...") sys.exit(0)
def test_set_execution_async(workflow_client: WorkflowClient, workflow_stub: DummyWorkflow): workflow_client.start(workflow_stub.dummy_workflow) assert workflow_stub._execution assert workflow_stub._execution.run_id == "the-run-id"
def workflow_client(workflow_service): client = WorkflowClient(service=workflow_service, domain="dummy", options=None) client.wait_for_close = Mock() return client
# Workflow Implementation class GreetingWorkflowImpl(GreetingWorkflow): def __init__(self): self.greeting_activities: GreetingActivities = Workflow.new_activity_stub( GreetingActivities) async def get_greeting(self, name): return await self.greeting_activities.compose_greeting("Hello", name) if __name__ == '__main__': factory = WorkerFactory("localhost", 7933, DOMAIN) worker = factory.new_worker(TASK_LIST) worker.register_activities_implementation(GreetingActivitiesImpl(), "GreetingActivities") worker.register_workflow_implementation_type(GreetingWorkflowImpl) factory.start() client = WorkflowClient.new_client(domain=DOMAIN) greeting_workflow: GreetingWorkflow = client.new_workflow_stub( GreetingWorkflow) result = greeting_workflow.get_greeting("Python") print(result) print("Stopping workers....") worker.stop() print("Workers stopped...") sys.exit(0)
def test_workflow_query_stub(): client = WorkflowClient(service=Mock(), domain="", options=None) stub: DummyWorkflow = client.new_workflow_stub(DummyWorkflow) assert stub.dummy_query != DummyWorkflow.dummy_query assert stub.dummy_query._query_method