Search in sources :

Example 11 with WorkflowExecution

use of com.uber.cadence.WorkflowExecution in project cadence-client by uber-java.

the class WorkflowTestingTest method testTimerCancellation.

@Test
public void testTimerCancellation() throws TException {
    Worker worker = testEnvironment.newWorker(TASK_LIST);
    worker.registerWorkflowImplementationTypes(TestTimerCancellationWorkflow.class);
    worker.registerActivitiesImplementations(new ActivityImpl());
    worker.start();
    WorkflowClient client = testEnvironment.newWorkflowClient();
    TestWorkflow workflow = client.newWorkflowStub(TestWorkflow.class);
    WorkflowExecution execution = WorkflowClient.start(workflow::workflow1, "input1");
    WorkflowStub untyped = client.newUntypedWorkflowStub(execution, Optional.empty());
    testEnvironment.sleep(Duration.ofHours(1));
    untyped.cancel();
    try {
        untyped.getResult(String.class);
        fail("unreacheable");
    } catch (CancellationException e) {
    }
    History history = testEnvironment.getWorkflowService().GetWorkflowExecutionHistory(new GetWorkflowExecutionHistoryRequest().setExecution(execution).setDomain(client.getDomain())).getHistory();
    List<HistoryEvent> historyEvents = history.getEvents();
    assertTrue(WorkflowExecutionUtils.prettyPrintHistory(history, false), WorkflowExecutionUtils.containsEvent(historyEvents, EventType.TimerCanceled));
}
Also used : WorkflowStub(com.uber.cadence.client.WorkflowStub) CancellationException(java.util.concurrent.CancellationException) GetWorkflowExecutionHistoryRequest(com.uber.cadence.GetWorkflowExecutionHistoryRequest) Worker(com.uber.cadence.worker.Worker) WorkflowClient(com.uber.cadence.client.WorkflowClient) WorkflowExecution(com.uber.cadence.WorkflowExecution) History(com.uber.cadence.History) HistoryEvent(com.uber.cadence.HistoryEvent) Test(org.junit.Test)

Example 12 with WorkflowExecution

use of com.uber.cadence.WorkflowExecution in project cadence-client by uber-java.

the class WorkflowTestingTest method testActivityCancellation.

@Test
public void testActivityCancellation() {
    Worker worker = testEnvironment.newWorker(TASK_LIST);
    worker.registerWorkflowImplementationTypes(TestCancellationWorkflow.class);
    worker.registerActivitiesImplementations(new TestCancellationActivityImpl());
    worker.start();
    WorkflowClient client = testEnvironment.newWorkflowClient();
    TestWorkflow workflow = client.newWorkflowStub(TestWorkflow.class);
    try {
        WorkflowExecution execution = WorkflowClient.start(workflow::workflow1, "input1");
        WorkflowStub untyped = client.newUntypedWorkflowStub(execution, Optional.empty());
        // While activity is running time skipping is disabled.
        // So sleep for 1 second after it is scheduled.
        testEnvironment.sleep(Duration.ofSeconds(3601));
        untyped.cancel();
        untyped.getResult(String.class);
        fail("unreacheable");
    } catch (CancellationException e) {
    }
}
Also used : WorkflowStub(com.uber.cadence.client.WorkflowStub) CancellationException(java.util.concurrent.CancellationException) Worker(com.uber.cadence.worker.Worker) WorkflowClient(com.uber.cadence.client.WorkflowClient) WorkflowExecution(com.uber.cadence.WorkflowExecution) Test(org.junit.Test)

Example 13 with WorkflowExecution

use of com.uber.cadence.WorkflowExecution in project cadence-client by uber-java.

the class WorkflowTest method testSignalUntyped.

@Test
public void testSignalUntyped() {
    startWorkerFor(TestSignalWorkflowImpl.class);
    String workflowType = QueryableWorkflow.class.getSimpleName() + "::execute";
    AtomicReference<WorkflowExecution> execution = new AtomicReference<>();
    WorkflowStub client = workflowClient.newUntypedWorkflowStub(workflowType, newWorkflowOptionsBuilder(taskList).build());
    // To execute workflow client.execute() would do. But we want to start workflow and immediately return.
    registerDelayedCallback(Duration.ofSeconds(1), () -> {
        assertEquals("initial", client.query("QueryableWorkflow::getState", String.class));
        client.signal("testSignal", "Hello ");
        while (!"Hello ".equals(client.query("QueryableWorkflow::getState", String.class))) {
        }
        assertEquals("Hello ", client.query("QueryableWorkflow::getState", String.class));
        client.signal("testSignal", "World!");
        while (!"World!".equals(client.query("QueryableWorkflow::getState", String.class))) {
        }
        assertEquals("World!", client.query("QueryableWorkflow::getState", String.class));
        assertEquals("Hello World!", workflowClient.newUntypedWorkflowStub(execution.get(), Optional.of(workflowType)).getResult(String.class));
    });
    execution.set(client.start());
    assertEquals("Hello World!", client.getResult(String.class));
}
Also used : WorkflowStub(com.uber.cadence.client.WorkflowStub) WorkflowExecution(com.uber.cadence.WorkflowExecution) AtomicReference(java.util.concurrent.atomic.AtomicReference) DeterministicRunnerTest(com.uber.cadence.internal.sync.DeterministicRunnerTest) Test(org.junit.Test)

Example 14 with WorkflowExecution

use of com.uber.cadence.WorkflowExecution in project cadence-client by uber-java.

the class TestWorkflowService method startWorkflowExecutionImpl.

StartWorkflowExecutionResponse startWorkflowExecutionImpl(StartWorkflowExecutionRequest startRequest, Optional<TestWorkflowMutableState> parent) throws BadRequestError, WorkflowExecutionAlreadyStartedError, InternalServiceError {
    String requestWorkflowId = requireNotNull("WorkflowId", startRequest.getWorkflowId());
    String domain = requireNotNull("Domain", startRequest.getDomain());
    WorkflowId workflowId = new WorkflowId(domain, requestWorkflowId);
    TestWorkflowMutableState running;
    lock.lock();
    try {
        running = openExecutions.get(workflowId);
    } finally {
        lock.unlock();
    }
    if (running != null) {
        WorkflowExecutionAlreadyStartedError error = new WorkflowExecutionAlreadyStartedError();
        WorkflowExecution execution = running.getExecutionId().getExecution();
        error.setMessage(String.format("Workflow execution already running. WorkflowId: %s, " + "RunId: %s", execution.getWorkflowId(), execution.getRunId()));
        error.setRunId(execution.getRunId());
        error.setStartRequestId(startRequest.getRequestId());
        throw error;
    }
    return startWorkflowExecutionNoRunningCheck(startRequest, parent, workflowId);
}
Also used : WorkflowExecutionAlreadyStartedError(com.uber.cadence.WorkflowExecutionAlreadyStartedError) WorkflowExecution(com.uber.cadence.WorkflowExecution)

Example 15 with WorkflowExecution

use of com.uber.cadence.WorkflowExecution in project cadence-client by uber-java.

the class WorkflowClientInternal method newWorkflowStub.

@Override
public <T> T newWorkflowStub(Class<T> workflowInterface, String workflowId, Optional<String> runId) {
    checkAnnotation(workflowInterface, WorkflowMethod.class, QueryMethod.class);
    if (Strings.isNullOrEmpty(workflowId)) {
        throw new IllegalArgumentException("workflowId is null or empty");
    }
    WorkflowExecution execution = new WorkflowExecution();
    execution.setWorkflowId(workflowId);
    if (runId.isPresent()) {
        execution.setRunId(runId.get());
    }
    WorkflowInvocationHandler invocationHandler = new WorkflowInvocationHandler(workflowInterface, genericClient, execution, dataConverter, interceptors);
    @SuppressWarnings("unchecked") T result = (T) Proxy.newProxyInstance(WorkflowInternal.class.getClassLoader(), new Class<?>[] { workflowInterface }, invocationHandler);
    return result;
}
Also used : WorkflowExecution(com.uber.cadence.WorkflowExecution)

Aggregations

WorkflowExecution (com.uber.cadence.WorkflowExecution)30 CancellationException (java.util.concurrent.CancellationException)11 SignalExternalWorkflowException (com.uber.cadence.workflow.SignalExternalWorkflowException)8 ChildWorkflowTerminatedException (com.uber.cadence.workflow.ChildWorkflowTerminatedException)7 ChildWorkflowTimedOutException (com.uber.cadence.workflow.ChildWorkflowTimedOutException)7 StartChildWorkflowFailedException (com.uber.cadence.workflow.StartChildWorkflowFailedException)7 HistoryEvent (com.uber.cadence.HistoryEvent)6 Test (org.junit.Test)6 TimeoutException (java.util.concurrent.TimeoutException)5 TException (org.apache.thrift.TException)5 WorkflowStub (com.uber.cadence.client.WorkflowStub)4 GetWorkflowExecutionHistoryRequest (com.uber.cadence.GetWorkflowExecutionHistoryRequest)3 History (com.uber.cadence.History)3 StartChildWorkflowExecutionFailedEventAttributes (com.uber.cadence.StartChildWorkflowExecutionFailedEventAttributes)3 WorkflowExecutionAlreadyStartedError (com.uber.cadence.WorkflowExecutionAlreadyStartedError)3 WorkflowType (com.uber.cadence.WorkflowType)3 DeterministicRunnerTest (com.uber.cadence.internal.sync.DeterministicRunnerTest)3 ChildWorkflowExecutionCanceledEventAttributes (com.uber.cadence.ChildWorkflowExecutionCanceledEventAttributes)2 ChildWorkflowExecutionCompletedEventAttributes (com.uber.cadence.ChildWorkflowExecutionCompletedEventAttributes)2 ChildWorkflowExecutionFailedCause (com.uber.cadence.ChildWorkflowExecutionFailedCause)2