Search in sources :

Example 1 with Worker

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

the class WorkflowTest method testSignal.

@Test
public void testSignal() throws Exception {
    AtomicReference<WorkflowExecution> execution = new AtomicReference<>();
    startWorkerFor(TestSignalWorkflowImpl.class);
    WorkflowOptions.Builder optionsBuilder = newWorkflowOptionsBuilder(taskList);
    String workflowId = UUID.randomUUID().toString();
    optionsBuilder.setWorkflowId(workflowId);
    AtomicReference<QueryableWorkflow> client = new AtomicReference<>();
    registerDelayedCallback(Duration.ofSeconds(1), () -> {
        assertEquals(workflowId, execution.get().getWorkflowId());
        assertEquals("initial", client.get().getState());
        client.get().mySignal("Hello ");
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        // Test client.get() created using WorkflowExecution
        client.set(workflowClient.newWorkflowStub(QueryableWorkflow.class, execution.get().getWorkflowId(), Optional.of(execution.get().getRunId())));
        assertEquals("Hello ", client.get().getState());
        // Test getTrace through replay by a local worker.
        Worker queryWorker;
        if (useExternalService) {
            queryWorker = new Worker(domain, taskList);
        } else {
            queryWorker = testEnvironment.newWorker(taskList);
        }
        queryWorker.registerWorkflowImplementationTypes(TestSignalWorkflowImpl.class);
        String queryResult = null;
        try {
            queryResult = queryWorker.queryWorkflowExecution(execution.get(), "QueryableWorkflow::getState", String.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        assertEquals("Hello ", queryResult);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        client.get().mySignal("World!");
        assertEquals("World!", client.get().getState());
        assertEquals("Hello World!", workflowClient.newUntypedWorkflowStub(execution.get(), Optional.empty()).getResult(String.class));
    });
    client.set(workflowClient.newWorkflowStub(QueryableWorkflow.class, optionsBuilder.build()));
    // To execute workflow client.execute() would do. But we want to start workflow and immediately return.
    execution.set(WorkflowClient.start(client.get()::execute));
}
Also used : WorkflowExecution(com.uber.cadence.WorkflowExecution) WorkflowOptions(com.uber.cadence.client.WorkflowOptions) Worker(com.uber.cadence.worker.Worker) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeoutException(java.util.concurrent.TimeoutException) ActivityNotExistsException(com.uber.cadence.client.ActivityNotExistsException) CancellationException(java.util.concurrent.CancellationException) FileNotFoundException(java.io.FileNotFoundException) DuplicateWorkflowException(com.uber.cadence.client.DuplicateWorkflowException) WorkflowFailureException(com.uber.cadence.client.WorkflowFailureException) ActivityCancelledException(com.uber.cadence.client.ActivityCancelledException) WorkflowException(com.uber.cadence.client.WorkflowException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DeterministicRunnerTest(com.uber.cadence.internal.sync.DeterministicRunnerTest) Test(org.junit.Test)

Example 2 with Worker

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

the class WorkflowTest method setUp.

@Before
public void setUp() {
    if (testName.getMethodName().equals("testExecute[TestService]") || testName.getMethodName().equals("testStart[TestService]")) {
        taskList = ANNOTATION_TASK_LIST;
    } else {
        taskList = "WorkflowTest-" + testName.getMethodName();
    }
    if (useExternalService) {
        worker = new Worker(domain, taskList);
        workflowClient = WorkflowClient.newInstance(domain);
        WorkflowClientOptions clientOptions = new WorkflowClientOptions.Builder().setDataConverter(JsonDataConverter.getInstance()).build();
        workflowClientWithOptions = WorkflowClient.newInstance(domain, clientOptions);
        scheduledExecutor = new ScheduledThreadPoolExecutor(1);
    } else {
        TestEnvironmentOptions testOptions = new Builder().setDomain(domain).build();
        testEnvironment = TestWorkflowEnvironment.newInstance(testOptions);
        worker = testEnvironment.newWorker(taskList);
        workflowClient = testEnvironment.newWorkflowClient();
        workflowClientWithOptions = testEnvironment.newWorkflowClient();
    }
    ActivityCompletionClient completionClient = workflowClient.newActivityCompletionClient();
    activitiesImpl = new TestActivitiesImpl(completionClient);
    worker.registerActivitiesImplementations(activitiesImpl);
    newWorkflowOptionsBuilder(taskList);
    newActivityOptions1(taskList);
    activitiesImpl.invocations.clear();
    activitiesImpl.procResult.clear();
}
Also used : TestEnvironmentOptions(com.uber.cadence.testing.TestEnvironmentOptions) ActivityCompletionClient(com.uber.cadence.client.ActivityCompletionClient) WorkflowClientOptions(com.uber.cadence.client.WorkflowClientOptions) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Builder(com.uber.cadence.testing.TestEnvironmentOptions.Builder) Worker(com.uber.cadence.worker.Worker) Before(org.junit.Before)

Example 3 with Worker

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

the class WorkflowTestingTest method testSignalWithDelayedCallback.

@Test
public void testSignalWithDelayedCallback() {
    Worker worker = testEnvironment.newWorker(TASK_LIST);
    worker.registerWorkflowImplementationTypes(SignaledWorkflowImpl.class);
    worker.start();
    WorkflowClient client = testEnvironment.newWorkflowClient();
    SignaledWorkflow workflow = client.newWorkflowStub(SignaledWorkflow.class);
    testEnvironment.registerDelayedCallback(Duration.ofMinutes(65), () -> workflow.ProcessSignal("signalInput"));
    assertEquals("signalInput-input1", workflow.workflow1("input1"));
}
Also used : Worker(com.uber.cadence.worker.Worker) WorkflowClient(com.uber.cadence.client.WorkflowClient) Test(org.junit.Test)

Example 4 with Worker

use of com.uber.cadence.worker.Worker 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 5 with Worker

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

the class WorkflowTestingTest method testActivityScheduleToCloseTimeout.

@Test
public void testActivityScheduleToCloseTimeout() {
    Worker worker = testEnvironment.newWorker(TASK_LIST);
    worker.registerWorkflowImplementationTypes(TestActivityTimeoutWorkflowImpl.class);
    worker.registerActivitiesImplementations(new TimingOutActivityImpl());
    worker.start();
    WorkflowClient client = testEnvironment.newWorkflowClient();
    TestActivityTimeoutWorkflow workflow = client.newWorkflowStub(TestActivityTimeoutWorkflow.class);
    try {
        workflow.workflow(1, 10, 10);
        fail("unreacheable");
    } catch (WorkflowException e) {
        assertTrue(e.getCause() instanceof ActivityTimeoutException);
        assertEquals(TimeoutType.SCHEDULE_TO_CLOSE, ((ActivityTimeoutException) e.getCause()).getTimeoutType());
    }
}
Also used : ActivityTimeoutException(com.uber.cadence.workflow.ActivityTimeoutException) WorkflowException(com.uber.cadence.client.WorkflowException) Worker(com.uber.cadence.worker.Worker) WorkflowClient(com.uber.cadence.client.WorkflowClient) Test(org.junit.Test)

Aggregations

Worker (com.uber.cadence.worker.Worker)18 Test (org.junit.Test)16 WorkflowClient (com.uber.cadence.client.WorkflowClient)15 WorkflowException (com.uber.cadence.client.WorkflowException)7 WorkflowExecution (com.uber.cadence.WorkflowExecution)3 WorkflowOptions (com.uber.cadence.client.WorkflowOptions)3 ActivityTimeoutException (com.uber.cadence.workflow.ActivityTimeoutException)3 CancellationException (java.util.concurrent.CancellationException)3 WorkflowStub (com.uber.cadence.client.WorkflowStub)2 GetWorkflowExecutionHistoryRequest (com.uber.cadence.GetWorkflowExecutionHistoryRequest)1 History (com.uber.cadence.History)1 HistoryEvent (com.uber.cadence.HistoryEvent)1 ActivityCancelledException (com.uber.cadence.client.ActivityCancelledException)1 ActivityCompletionClient (com.uber.cadence.client.ActivityCompletionClient)1 ActivityNotExistsException (com.uber.cadence.client.ActivityNotExistsException)1 DuplicateWorkflowException (com.uber.cadence.client.DuplicateWorkflowException)1 WorkflowClientOptions (com.uber.cadence.client.WorkflowClientOptions)1 WorkflowFailureException (com.uber.cadence.client.WorkflowFailureException)1 WorkflowTimedOutException (com.uber.cadence.client.WorkflowTimedOutException)1 DeterministicRunnerTest (com.uber.cadence.internal.sync.DeterministicRunnerTest)1