Search in sources :

Example 1 with WorkflowOptions

use of io.temporal.client.WorkflowOptions in project sdk-java by temporalio.

the class TestWorkflowEnvironmentSleepTest method timeskippingCanBeDisabled.

@Test
public void timeskippingCanBeDisabled() throws TimeoutException {
    // Use a differently configured TestWorkflowEnvironment
    tearDown();
    setUp(TestEnvironmentOptions.newBuilder().setUseTimeskipping(false).build());
    WorkflowOptions workflowAOptions = WorkflowOptions.newBuilder().setTaskQueue(WORKFLOW_TASK_QUEUE).setWorkflowExecutionTimeout(Duration.ofMinutes(30)).build();
    WorkflowStub stubA = client.newUntypedWorkflowStub("ConfigurableSleepWorkflow", workflowAOptions);
    // The workflow sleeps for 10 minutes - we'll wait for significantly less than that to verify
    // that timeskipping can be disabled. Unfortunately there's tension between this assertion's
    // false positive rate and how long it takes.
    long durationToSleep = Duration.ofMinutes(10).toMillis();
    Duration durationToWait = Duration.ofSeconds(1);
    stubA.start(durationToSleep);
    WorkflowServiceException e = Assert.assertThrows(WorkflowServiceException.class, () -> stubA.getResult(durationToWait.toMillis(), TimeUnit.MILLISECONDS, Void.class));
    Assert.assertNotNull(e.getCause());
    Assert.assertEquals(io.grpc.StatusRuntimeException.class, e.getCause().getClass());
    Assert.assertEquals(Status.Code.DEADLINE_EXCEEDED, ((io.grpc.StatusRuntimeException) e.getCause()).getStatus().getCode());
    // With timeskipping off, a workflow that doesn't sleep for very long should still finish
    WorkflowStub stubB = client.newUntypedWorkflowStub("ConfigurableSleepWorkflow", workflowAOptions);
    WorkflowExecution executionB = stubB.start(Duration.ZERO);
    stubB.getResult(durationToWait.toMillis(), TimeUnit.MILLISECONDS, Void.class);
}
Also used : WorkflowStub(io.temporal.client.WorkflowStub) StatusRuntimeException(io.grpc.StatusRuntimeException) WorkflowOptions(io.temporal.client.WorkflowOptions) WorkflowExecution(io.temporal.api.common.v1.WorkflowExecution) Duration(java.time.Duration) WorkflowServiceException(io.temporal.client.WorkflowServiceException) Test(org.junit.Test)

Example 2 with WorkflowOptions

use of io.temporal.client.WorkflowOptions in project sdk-java by temporalio.

the class TestWorkflowEnvironmentSleepTest method timeskippingWorksForBothTypesOfUntypedStubs.

@Test
public void timeskippingWorksForBothTypesOfUntypedStubs() {
    WorkflowOptions workflowAOptions = WorkflowOptions.newBuilder().setTaskQueue(WORKFLOW_TASK_QUEUE).setWorkflowExecutionTimeout(Duration.ofMinutes(30)).build();
    WorkflowStub stubA = client.newUntypedWorkflowStub("ConfigurableSleepWorkflow", workflowAOptions);
    // The workflow sleeps for 10 minutes, which will take less than 10 seconds if timeskipping
    // works
    long durationToSleep = Duration.ofMinutes(10).toMillis();
    Duration durationToWait = Duration.ofSeconds(10);
    stubA.start(durationToSleep);
    waitForWorkflow(stubA, "newUntypedStubWithOptions", durationToWait);
    // Now use one stub to start the workflow and create another stub using its WorkflowExecution.
    // This simulates the scenario where someone stored a WorkflowExecution in their database,
    // looked it up, and wants to check status.
    WorkflowStub stubB = client.newUntypedWorkflowStub("ConfigurableSleepWorkflow", workflowAOptions);
    WorkflowExecution executionB = stubB.start(durationToSleep);
    WorkflowStub stubBPrime = client.newUntypedWorkflowStub(executionB, Optional.empty());
    waitForWorkflow(stubBPrime, "newUntypedStubForWorkflowExecution", durationToWait);
}
Also used : WorkflowStub(io.temporal.client.WorkflowStub) WorkflowOptions(io.temporal.client.WorkflowOptions) WorkflowExecution(io.temporal.api.common.v1.WorkflowExecution) Duration(java.time.Duration) Test(org.junit.Test)

Example 3 with WorkflowOptions

use of io.temporal.client.WorkflowOptions in project sdk-java by temporalio.

the class TestWorkflowEnvironmentSleepTest method timeoutDoesNotBlockTimer.

/**
 * The test service skips ahead for timers, but (correctly) does not skip ahead for timeouts. We
 * used to have a bug that's best explained by example.
 *
 * <p>Start workflow A with an execution timeout of T. Start workflow B that sleeps for X, which
 * is after T. This will leave SelfAdvancingTimerImpl's internal task queue as follows:
 *
 * <pre>
 *   [@ now+T] workflow execution timeout, canceled = true
 *   [@ now+X] fire timer, canceled = false
 * </pre>
 *
 * <p>The test service will let real-time pass until T, then skip time to T+X. This blocks all
 * forward progress for however long X is.
 *
 * <p>If you're thinking "That's silly - the first task is canceled, it should obviously be
 * skipped!" then congratulations, you identified the bug and the fix!
 */
@Test
public void timeoutDoesNotBlockTimer() {
    // This is T from the example
    Duration workflowExecutionTimeout = Duration.ofMinutes(5);
    // This is X from the example.
    Duration sleepDuration = workflowExecutionTimeout.multipliedBy(2);
    // This test verifies time-skipping by waiting a small amount of real time for the workflows to
    // complete. In bug-land, they wouldn't complete on time.
    Duration howLongWeWaitForFutures = Duration.ofSeconds(5);
    WorkflowOptions workflowAOptions = WorkflowOptions.newBuilder().setTaskQueue(WORKFLOW_TASK_QUEUE).setWorkflowExecutionTimeout(workflowExecutionTimeout).build();
    WorkflowStub workflowAStub = client.newUntypedWorkflowStub("ConfigurableSleepWorkflow", workflowAOptions);
    // workflowA completes immediately, even in bug-land
    workflowAStub.start(0);
    waitForWorkflow(workflowAStub, "A", howLongWeWaitForFutures);
    // Workflow B's execution timeout needs to be longer than its sleep.
    WorkflowOptions workflowBOptions = WorkflowOptions.newBuilder().setTaskQueue(WORKFLOW_TASK_QUEUE).setWorkflowExecutionTimeout(sleepDuration.multipliedBy(2)).build();
    WorkflowStub workflowBStub = client.newUntypedWorkflowStub("ConfigurableSleepWorkflow", workflowBOptions);
    // In bug land, workflow B wouldn't complete until workflowExecutionTimeout real seconds from
    // now (minus epsilon). Without the bug, it should complete immediately.
    workflowBStub.start(sleepDuration.toMillis());
    waitForWorkflow(workflowBStub, "B", howLongWeWaitForFutures);
}
Also used : WorkflowStub(io.temporal.client.WorkflowStub) WorkflowOptions(io.temporal.client.WorkflowOptions) Duration(java.time.Duration) Test(org.junit.Test)

Example 4 with WorkflowOptions

use of io.temporal.client.WorkflowOptions in project sdk-java by temporalio.

the class SignalAndQueryListenerTest method testSignalAndQueryListener.

@Test
public void testSignalAndQueryListener() {
    WorkflowOptions options = SDKTestOptions.newWorkflowOptionsWithTimeouts(testWorkflowRule.getTaskQueue());
    TestSignalAndQueryListenerWorkflow stub = testWorkflowRule.newWorkflowStubTimeoutOptions(TestSignalAndQueryListenerWorkflow.class);
    WorkflowExecution execution = WorkflowClient.start(stub::execute);
    SignalQueryBase signalStub = testWorkflowRule.getWorkflowClient().newWorkflowStub(SignalQueryBase.class, execution.getWorkflowId());
    // Send signals before listener is registered to test signal buffering
    signalStub.signal("a");
    signalStub.signal("b");
    try {
        signalStub.getSignal();
        // as not listener is not registered yet
        Assert.fail("unreachable");
    } catch (WorkflowQueryException e) {
        Assert.assertTrue(e.getCause().getMessage().contains("Unknown query type: getSignal"));
    }
    stub.register();
    while (true) {
        try {
            Assert.assertEquals("a, b", signalStub.getSignal());
            break;
        } catch (WorkflowQueryException e) {
            Assert.assertTrue(e.getMessage().contains("Unknown query type: getSignal"));
        }
    }
    testWorkflowRule.getInterceptor(TracingWorkerInterceptor.class).setExpected("interceptExecuteWorkflow " + SDKTestWorkflowRule.UUID_REGEXP, "registerSignalHandlers register", "newThread workflow-method", "await await", "handleSignal register", "registerQuery getSignal", "registerSignalHandlers signal", "handleSignal signal", "handleSignal signal", "handleQuery getSignal", "query getSignal");
}
Also used : SignalQueryBase(io.temporal.workflow.shared.TestWorkflows.SignalQueryBase) WorkflowOptions(io.temporal.client.WorkflowOptions) WorkflowExecution(io.temporal.api.common.v1.WorkflowExecution) TracingWorkerInterceptor(io.temporal.testing.internal.TracingWorkerInterceptor) WorkflowQueryException(io.temporal.client.WorkflowQueryException) Test(org.junit.Test)

Example 5 with WorkflowOptions

use of io.temporal.client.WorkflowOptions in project sdk-java by temporalio.

the class SignalExternalWorkflowFailureTest method testTerminateWorkflowSignalError.

@Test
public void testTerminateWorkflowSignalError() throws InterruptedException {
    WorkflowOptions options = WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.getTaskQueue()).setWorkflowId(WORKFLOW_ID).build();
    TestSignaledWorkflow terminatedWorkflow = testWorkflowRule.getWorkflowClient().newWorkflowStub(TestSignaledWorkflow.class, options);
    WorkflowClient.start(terminatedWorkflow::execute);
    TestWorkflowReturnString signalingWorkflow = testWorkflowRule.newWorkflowStub(TestWorkflowReturnString.class);
    WorkflowClient.start(signalingWorkflow::execute);
    // Wait for terminatedWorkflow to start
    latch.await();
    WorkflowStub stub = WorkflowStub.fromTyped(terminatedWorkflow);
    stub.terminate("Mock terminating workflow");
    // Wait for signalingWorkflow to start and terminatedWorkflow to terminate
    latch2.countDown();
    WorkflowStub workflowStub2 = WorkflowStub.fromTyped(signalingWorkflow);
    assertEquals(workflowStub2.getResult(String.class), "Success!");
}
Also used : WorkflowStub(io.temporal.client.WorkflowStub) TestSignaledWorkflow(io.temporal.workflow.shared.TestWorkflows.TestSignaledWorkflow) WorkflowOptions(io.temporal.client.WorkflowOptions) TestWorkflowReturnString(io.temporal.workflow.shared.TestWorkflows.TestWorkflowReturnString) TestWorkflowReturnString(io.temporal.workflow.shared.TestWorkflows.TestWorkflowReturnString) Test(org.junit.Test)

Aggregations

WorkflowOptions (io.temporal.client.WorkflowOptions)130 Test (org.junit.Test)106 WorkflowClient (io.temporal.client.WorkflowClient)51 Worker (io.temporal.worker.Worker)44 WorkflowStub (io.temporal.client.WorkflowStub)23 TestWorkflow1 (io.temporal.workflow.shared.TestWorkflows.TestWorkflow1)21 WorkflowExecution (io.temporal.api.common.v1.WorkflowExecution)19 WorkflowException (io.temporal.client.WorkflowException)15 WorkflowServiceStubs (io.temporal.serviceclient.WorkflowServiceStubs)15 WorkerFactory (io.temporal.worker.WorkerFactory)12 TestWorkflows (io.temporal.workflow.shared.TestWorkflows)12 TimeoutFailure (io.temporal.failure.TimeoutFailure)11 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)11 WorkflowTestingTest (io.temporal.internal.testing.WorkflowTestingTest)9 WorkflowFailedException (io.temporal.client.WorkflowFailedException)7 ChildWorkflowFailure (io.temporal.failure.ChildWorkflowFailure)7 RootScopeBuilder (com.uber.m3.tally.RootScopeBuilder)6 ActivityFailure (io.temporal.failure.ActivityFailure)6 ApplicationFailure (io.temporal.failure.ApplicationFailure)5 NoArgsWorkflow (io.temporal.workflow.shared.TestWorkflows.NoArgsWorkflow)5