Hi! Thank you for your help.
We are using 1.2.0
Ive run the test with an example workflow implementation which has just one blocking activity.
2 tests: one with the TestWorkflowExtension
and one with a BeforeEach
I wrote.
I query the open executions with stub.listOpenWorkflowExecutions(request);
.
Note: When I wait for the workflow to finish, the status this time is WORKFLOW_EXECUTION_STATUS_COMPLETED
Here is the code:
package com.layer.temporal.example;
import static io.temporal.api.enums.v1.EventType.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import io.temporal.api.common.v1.WorkflowExecution;
import io.temporal.api.enums.v1.EventType;
import io.temporal.api.enums.v1.WorkflowExecutionStatus;
import io.temporal.api.history.v1.History;
import io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryRequest;
import io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryResponse;
import io.temporal.api.workflowservice.v1.ListOpenWorkflowExecutionsRequest;
import io.temporal.api.workflowservice.v1.WorkflowServiceGrpc;
import io.temporal.client.WorkflowClient;
import io.temporal.serviceclient.WorkflowServiceStubs;
import io.temporal.testing.TestWorkflowEnvironment;
import io.temporal.testing.TestWorkflowExtension;
import io.temporal.worker.Worker;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
public class ExampleWorkflowImplWithExtensionTest {
@RegisterExtension
public static final TestWorkflowExtension testWorkflowExtension =
TestWorkflowExtension.newBuilder()
.setWorkflowTypes(ExampleWorkflowImpl.class)
.setActivityImplementations(newWaitGroupActivity())
.build();
private static ExampleActivityWithWaitGroup newWaitGroupActivity() {
var waitGroup = new CountDownLatch(1);
return new ExampleActivityWithWaitGroup(waitGroup);
}
@Test
void workflow_list_returns_open_workflows_with_status_unspecified(
TestWorkflowEnvironment testEnv, Worker worker, ExampleWorkflow workflow) {
var workflowService = testEnv.getWorkflowService();
// when
// Execute our workflow asynchronously
WorkflowExecution we = WorkflowClient.start(workflow::start, "lala");
// wait for scheduled
waitForEvent(
testEnv,
workflowService,
we,
EVENT_TYPE_ACTIVITY_TASK_SCHEDULED,
Duration.ofSeconds(3),
Duration.ofSeconds(1));
var stub = workflowService.blockingStub();
var request = ListOpenWorkflowExecutionsRequest.newBuilder().build();
var result = stub.listOpenWorkflowExecutions(request);
// then
assertEquals(result.getExecutionsList().size(), 1);
assertEquals(
result.getExecutions(0).getStatus(),
WorkflowExecutionStatus.WORKFLOW_EXECUTION_STATUS_UNSPECIFIED);
}
private void waitForEvent(
TestWorkflowEnvironment testEnv,
WorkflowServiceStubs workflowService,
WorkflowExecution we,
EventType eventType,
Duration atMost,
Duration pollInterval) {
waitForEvent(testEnv, workflowService, we, eventType, atMost, pollInterval, null);
}
private void waitForEvent(
TestWorkflowEnvironment testEnv,
WorkflowServiceStubs workflowService,
WorkflowExecution we,
EventType eventType,
Duration atMost,
Duration pollInterval,
Consumer<History> historyConsumer) {
await()
.atMost(atMost)
.pollInterval(pollInterval)
.untilAsserted(
() -> {
var wfServiceStub = workflowService.blockingStub();
var workflowExecutionHistory =
getWorkflowExecutionHistory(testEnv, we, wfServiceStub);
var history = workflowExecutionHistory.getHistory();
var match =
history.getEventsList().stream()
.anyMatch(e -> e.getEventType().equals(eventType));
if (historyConsumer != null) {
historyConsumer.accept(history);
}
assertTrue(match);
});
}
private GetWorkflowExecutionHistoryResponse getWorkflowExecutionHistory(
TestWorkflowEnvironment testEnv,
WorkflowExecution we,
WorkflowServiceGrpc.WorkflowServiceBlockingStub wfServiceStub) {
return wfServiceStub.getWorkflowExecutionHistory(
GetWorkflowExecutionHistoryRequest.newBuilder()
.setNamespace(testEnv.getNamespace())
.setExecution(we)
.build());
}
}
(edited: removed unused code)