Skip to content

Commit

Permalink
add unit test for MigrationInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
shijiesheng committed Nov 7, 2024
1 parent f609320 commit 71f40e2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.mockito.Mockito.*;

import com.uber.cadence.*;
import com.uber.cadence.serviceclient.ClientOptions;
import com.uber.cadence.serviceclient.IWorkflowService;
import java.util.ArrayList;
import org.apache.thrift.TException;
Expand Down Expand Up @@ -1218,4 +1219,12 @@ public void testSignalWorkflowExecution_SignalInOldService() throws TException {
verifyNoMoreInteractions(serviceNew);
verifyNoMoreInteractions(serviceOld);
}

@Test
public void testGetOptions() {
when(serviceOld.getOptions())
.thenReturn(ClientOptions.newBuilder().setServiceName("serviceName").build());
ClientOptions options = migrationService.getOptions();
assertEquals("serviceName", options.getServiceName());
}
}
65 changes: 44 additions & 21 deletions src/test/java/com/uber/cadence/workflow/WorkflowMigrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.uber.cadence.serviceclient.ClientOptions;
import com.uber.cadence.serviceclient.IWorkflowService;
import com.uber.cadence.serviceclient.WorkflowServiceTChannel;
import com.uber.cadence.testUtils.CadenceTestRule;
import com.uber.cadence.testing.TestWorkflowEnvironment;
import com.uber.cadence.worker.Worker;
import com.uber.cadence.worker.WorkerFactory;
import com.uber.cadence.worker.WorkerFactoryOptions;
Expand All @@ -41,10 +43,7 @@
import java.util.UUID;
import java.util.concurrent.CancellationException;
import org.apache.thrift.TException;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.*;

public class WorkflowMigrationTest {
private WorkflowClient migrationWorkflowClient, workflowClientCurr, workflowClientNew;
Expand All @@ -54,24 +53,49 @@ public class WorkflowMigrationTest {
WorkerFactory factoryCurr, factoryNew;
Worker workerCurr, workerNew;

@Rule
public CadenceTestRule testRuleCur =
CadenceTestRule.builder()
.withDomain(DOMAIN)
.withWorkflowTypes(CrossDomainWorkflowTest.TestWorkflowCrossDomainImpl.class)
.startWorkersAutomatically()
.withTestEnvironmentProvider(TestWorkflowEnvironment::newInstance)
.build();

@Rule
public CadenceTestRule testRuleNew =
CadenceTestRule.builder()
.withDomain(DOMAIN2)
.withWorkflowTypes(WorkflowTest.TestWorkflowSignaledSimple.class)
.startWorkersAutomatically()
.withTestEnvironmentProvider(TestWorkflowEnvironment::newInstance)
.build();

@Before
public void setUp() {
IWorkflowService service =
new WorkflowServiceTChannel(
ClientOptions.newBuilder()
.setFeatureFlags(
new FeatureFlags().setWorkflowExecutionAlreadyCompletedErrorEnabled(true))
.build());
IWorkflowService serviceNew, serviceCur;
if (useDockerService) {
serviceCur =
new WorkflowServiceTChannel(
ClientOptions.newBuilder()
.setFeatureFlags(
new FeatureFlags().setWorkflowExecutionAlreadyCompletedErrorEnabled(true))
.build());
serviceNew = serviceCur; // docker only starts one server so share the same service
} else {
serviceCur = testRuleCur.getWorkflowClient().getService();
serviceNew = testRuleNew.getWorkflowClient().getService();
}
workflowClientCurr =
WorkflowClient.newInstance(
service, WorkflowClientOptions.newBuilder().setDomain(DOMAIN).build());
serviceCur, WorkflowClientOptions.newBuilder().setDomain(DOMAIN).build());
workflowClientNew =
WorkflowClient.newInstance(
service, WorkflowClientOptions.newBuilder().setDomain(DOMAIN2).build());
serviceNew, WorkflowClientOptions.newBuilder().setDomain(DOMAIN2).build());
MigrationIWorkflowService migrationService =
new MigrationIWorkflowService(
service, DOMAIN,
service, DOMAIN2);
serviceCur, DOMAIN,
serviceNew, DOMAIN2);
migrationWorkflowClient =
WorkflowClient.newInstance(
migrationService, WorkflowClientOptions.newBuilder().setDomain(DOMAIN).build());
Expand Down Expand Up @@ -155,7 +179,7 @@ public void execute(int iter) {

@Test
public void whenUseDockerService_cronWorkflowMigration() {
Assume.assumeTrue(useDockerService);
// Assume.assumeTrue(useDockerService);
String workflowID = UUID.randomUUID().toString();
try {
workflowClientCurr
Expand All @@ -164,7 +188,7 @@ public void whenUseDockerService_cronWorkflowMigration() {
.execute("for test");
} catch (CancellationException e) {
try {
describeWorkflowExecution(workflowClientNew, workflowID);
getWorkflowHistory(workflowClientNew, workflowID);
} catch (Exception eDesc) {
fail("fail to describe workflow execution in new domain: " + eDesc);
}
Expand All @@ -173,7 +197,6 @@ public void whenUseDockerService_cronWorkflowMigration() {

@Test
public void whenUseDockerService_continueAsNewWorkflowMigration() {
Assume.assumeTrue(useDockerService);
String workflowID = UUID.randomUUID().toString();
try {
workflowClientCurr
Expand All @@ -183,18 +206,18 @@ public void whenUseDockerService_continueAsNewWorkflowMigration() {
.execute(0);
} catch (CancellationException e) {
try {
describeWorkflowExecution(workflowClientNew, workflowID);
getWorkflowHistory(workflowClientNew, workflowID);
} catch (Exception eDesc) {
fail("fail to describe workflow execution in new domain: " + eDesc);
}
}
}

private DescribeWorkflowExecutionResponse describeWorkflowExecution(
private GetWorkflowExecutionHistoryResponse getWorkflowHistory(
WorkflowClient wc, String workflowID) throws TException {
return wc.getService()
.DescribeWorkflowExecution(
new DescribeWorkflowExecutionRequest()
.GetWorkflowExecutionHistory(
new GetWorkflowExecutionHistoryRequest()
.setExecution(new WorkflowExecution().setWorkflowId(workflowID))
.setDomain(wc.getOptions().getDomain()));
}
Expand Down

0 comments on commit 71f40e2

Please sign in to comment.