Skip to content

Commit ef30f9b

Browse files
authored
Added unit tests for all samples. (#14)
* Fixes due to API changes. Added Hello[Activity|Query]Test * Added googleJavaFormat and updated license check in build.gradle * Reformatted code by google format * Added reference to internal Uber repo to README * reformatted unit tests * Added HelloSignalTest * Added HelloActivityRetryTest * Added HelloAsyncTest * Added HelloAsyncActivityCompletionTest * Added HelloAsyncLambda * Added HelloChildTest * Added HelloPeriodicTest * Added FileProcessingTest * Changed failure to a simulated timeout * testEnv.close in case of unit test failure. Comments * Comments PR fixes
1 parent f89cd5d commit ef30f9b

35 files changed

+2285
-947
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
**If you work for Uber** use the internal _cadence-java-client-samples_ repository.
12
# Java Cadence Samples
23
These samples demonstrate various capabilities of Java Cadence client and server. You can learn more about Cadence at:
34
* [Cadence Service](https://github.com/uber/cadence)

build.gradle

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
plugins {
2-
id "com.github.hierynomus.license" version "0.14.0"
2+
id 'net.minecrell.licenser' version '0.3'
3+
id "com.github.sherter.google-java-format" version "0.6"
4+
id "net.ltgt.errorprone" version "0.0.13"
35
}
46

57
apply plugin: 'java'
68
apply plugin: 'maven'
9+
apply plugin: 'com.github.sherter.google-java-format'
10+
11+
googleJavaFormat {
12+
toolVersion '1.5'
13+
include '**/*.java'
14+
exclude '**/generated-sources/*'
15+
}
716

817
group = 'com.uber'
918
version = '0.0.1'
@@ -29,6 +38,16 @@ dependencies {
2938
compile group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.11.274'
3039
compile group: 'commons-configuration', name: 'commons-configuration', version: '1.9'
3140
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
41+
testCompile group: 'junit', name: 'junit', version: '4.12'
42+
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
43+
testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: '1.7.3'
44+
}
45+
46+
compileJava {
47+
dependsOn 'googleJavaFormat'
48+
options.encoding = 'UTF-8'
49+
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" << "-XepExcludedPaths:" +
50+
".*/generated-sources/.*"
3251
}
3352

3453
task execute(type: JavaExec) {
@@ -38,7 +57,4 @@ task execute(type: JavaExec) {
3857

3958
license {
4059
header rootProject.file('license-header.txt')
41-
mapping {
42-
java = 'SLASHSTAR_STYLE'
43-
}
4460
}

src/main/java/com/uber/cadence/samples/common/QueryWorkflowExecution.java

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,50 @@
1414
* express or implied. See the License for the specific language governing
1515
* permissions and limitations under the License.
1616
*/
17+
1718
package com.uber.cadence.samples.common;
1819

1920
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
2021

2122
import com.uber.cadence.WorkflowExecution;
22-
import com.uber.cadence.client.UntypedWorkflowStub;
2323
import com.uber.cadence.client.WorkflowClient;
24+
import com.uber.cadence.client.WorkflowStub;
2425
import com.uber.cadence.serviceclient.IWorkflowService;
2526
import com.uber.cadence.serviceclient.WorkflowServiceTChannel;
27+
import java.util.Optional;
2628

2729
/**
28-
* Simple example utility to query workflow execution using Cadence query API.
29-
* Cadence redirects query to any currently running workflow worker for the workflow type
30-
* of the requested workflow execution.
30+
* Queries a workflow execution using the Cadence query API. Cadence redirects a query to any
31+
* currently running workflow worker for the workflow type of the requested workflow execution.
3132
*
3233
* @author fateev
3334
*/
3435
public class QueryWorkflowExecution {
35-
36-
public static void main(String[] args) throws Exception {
37-
if (args.length < 2 || args.length > 3) {
38-
System.err.println("Usage: java " + QueryWorkflowExecution.class.getName() +
39-
" <queryType> <workflowId> [<runId>]");
40-
System.exit(1);
41-
}
42-
IWorkflowService cadenceService = new WorkflowServiceTChannel();
43-
44-
String queryType = args[0];
45-
46-
WorkflowExecution workflowExecution = new WorkflowExecution();
47-
String workflowId = args[1];
48-
workflowExecution.setWorkflowId(workflowId);
49-
if (args.length == 3) {
50-
String runId = args[1];
51-
workflowExecution.setRunId(runId);
52-
}
53-
WorkflowClient client = WorkflowClient.newInstance(cadenceService, DOMAIN);
54-
UntypedWorkflowStub workflow = client.newUntypedWorkflowStub(workflowExecution);
55-
String result = workflow.query(queryType, String.class);
56-
57-
System.out.println("Query result for " + workflowExecution + ":");
58-
System.out.println(result);
36+
37+
public static void main(String[] args) throws Exception {
38+
if (args.length < 2 || args.length > 3) {
39+
System.err.println(
40+
"Usage: java "
41+
+ QueryWorkflowExecution.class.getName()
42+
+ " <queryType> <workflowId> [<runId>]");
43+
System.exit(1);
44+
}
45+
IWorkflowService cadenceService = new WorkflowServiceTChannel();
46+
47+
String queryType = args[0];
48+
49+
WorkflowExecution workflowExecution = new WorkflowExecution();
50+
String workflowId = args[1];
51+
workflowExecution.setWorkflowId(workflowId);
52+
if (args.length == 3) {
53+
String runId = args[1];
54+
workflowExecution.setRunId(runId);
5955
}
56+
WorkflowClient client = WorkflowClient.newInstance(cadenceService, DOMAIN);
57+
WorkflowStub workflow = client.newUntypedWorkflowStub(workflowExecution, Optional.empty());
58+
String result = workflow.query(queryType, String.class);
59+
60+
System.out.println("Query result for " + workflowExecution + ":");
61+
System.out.println(result);
62+
}
6063
}

src/main/java/com/uber/cadence/samples/common/QueryWorkflowExecutionLocalReplay.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* express or implied. See the License for the specific language governing
1515
* permissions and limitations under the License.
1616
*/
17+
1718
package com.uber.cadence.samples.common;
1819

1920
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
@@ -24,36 +25,40 @@
2425
import com.uber.cadence.worker.Worker;
2526

2627
/**
27-
* Query workflow execution by getting history from Cadence and executing it on a local worker.
28-
* Use this approach to debug workflow execution in a local environment.
29-
*
28+
* Queries a workflow execution by getting a history from the Cadence service and executing it on a
29+
* local worker. Use this approach to debug a workflow execution in a local environment.
30+
*
3031
* @author fateev
3132
*/
3233
public class QueryWorkflowExecutionLocalReplay {
3334

34-
public static void main(String[] args) throws Exception {
35-
if (args.length < 3) {
36-
System.err.println("Usage: java " + QueryWorkflowExecutionLocalReplay.class.getName()
37-
+ "<workflow implementation class> <workflowId> <runId> <queryType>");
38-
System.exit(1);
39-
}
40-
IWorkflowService cadenceService = new WorkflowServiceTChannel();
35+
public static void main(String[] args) throws Exception {
36+
if (args.length < 3) {
37+
System.err.println(
38+
"Usage: java "
39+
+ QueryWorkflowExecutionLocalReplay.class.getName()
40+
+ "<workflow implementation class> <workflowId> <runId> <queryType>");
41+
System.exit(1);
42+
}
43+
IWorkflowService cadenceService = new WorkflowServiceTChannel();
4144

42-
WorkflowExecution workflowExecution = new WorkflowExecution();
43-
String workflowId = args[1];
44-
workflowExecution.setWorkflowId(workflowId);
45-
String runId = args[2];
46-
workflowExecution.setRunId(runId);
47-
String queryType = args[3];
45+
WorkflowExecution workflowExecution = new WorkflowExecution();
46+
String workflowId = args[1];
47+
workflowExecution.setWorkflowId(workflowId);
48+
String runId = args[2];
49+
workflowExecution.setRunId(runId);
50+
String queryType = args[3];
4851

49-
String implementationTypeName = args[0];
50-
@SuppressWarnings("unchecked")
51-
Class<Object> workflowImplementationType = (Class<Object>) Class.forName(implementationTypeName);
52-
Worker replayer = new Worker(cadenceService, DOMAIN, null, null);
53-
replayer.registerWorkflowImplementationTypes(workflowImplementationType);
54-
System.out.println("Beginning query replay for " + workflowExecution);
55-
String queryResult = replayer.queryWorkflowExecution(workflowExecution, queryType, String.class);
56-
System.out.println("Done query replay for " + workflowExecution);
57-
System.out.println("Query result:\n" + queryResult);
58-
}
52+
String implementationTypeName = args[0];
53+
@SuppressWarnings("unchecked")
54+
Class<Object> workflowImplementationType =
55+
(Class<Object>) Class.forName(implementationTypeName);
56+
Worker replayer = new Worker(cadenceService, DOMAIN, null, null);
57+
replayer.registerWorkflowImplementationTypes(workflowImplementationType);
58+
System.out.println("Beginning query replay for " + workflowExecution);
59+
String queryResult =
60+
replayer.queryWorkflowExecution(workflowExecution, queryType, String.class);
61+
System.out.println("Done query replay for " + workflowExecution);
62+
System.out.println("Query result:\n" + queryResult);
63+
}
5964
}

src/main/java/com/uber/cadence/samples/common/RegisterDomain.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* express or implied. See the License for the specific language governing
1515
* permissions and limitations under the License.
1616
*/
17+
1718
package com.uber.cadence.samples.common;
1819

1920
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
@@ -26,26 +27,30 @@
2627
import org.apache.thrift.TException;
2728

2829
/**
29-
* Simple example utility to pretty print workflow execution history.
30+
* Registers the "sample" domain with the Cadence service.
3031
*
3132
* @author fateev
3233
*/
3334
public class RegisterDomain {
3435

35-
public static void main(String[] args) throws TException, IOException {
36-
IWorkflowService cadenceService = new WorkflowServiceTChannel();
37-
RegisterDomainRequest request = new RegisterDomainRequest();
38-
request.setDescription("Java Samples");
39-
request.setEmitMetric(false);
40-
request.setName(DOMAIN);
41-
int retentionPeriodInDays = 1;
42-
request.setWorkflowExecutionRetentionPeriodInDays(retentionPeriodInDays);
43-
try {
44-
cadenceService.RegisterDomain(request);
45-
System.out.println("Successfully registered domain \"" + DOMAIN + "\" with retentionDays=" + retentionPeriodInDays);
46-
} catch (DomainAlreadyExistsError e) {
47-
System.out.println("Domain \"" + DOMAIN + "\" is already registered");
48-
}
49-
System.exit(0);
36+
public static void main(String[] args) throws TException, IOException {
37+
IWorkflowService cadenceService = new WorkflowServiceTChannel();
38+
RegisterDomainRequest request = new RegisterDomainRequest();
39+
request.setDescription("Java Samples");
40+
request.setEmitMetric(false);
41+
request.setName(DOMAIN);
42+
int retentionPeriodInDays = 1;
43+
request.setWorkflowExecutionRetentionPeriodInDays(retentionPeriodInDays);
44+
try {
45+
cadenceService.RegisterDomain(request);
46+
System.out.println(
47+
"Successfully registered domain \""
48+
+ DOMAIN
49+
+ "\" with retentionDays="
50+
+ retentionPeriodInDays);
51+
} catch (DomainAlreadyExistsError e) {
52+
System.out.println("Domain \"" + DOMAIN + "\" is already registered");
5053
}
54+
System.exit(0);
55+
}
5156
}

src/main/java/com/uber/cadence/samples/common/SampleConstants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* express or implied. See the License for the specific language governing
1515
* permissions and limitations under the License.
1616
*/
17+
1718
package com.uber.cadence.samples.common;
1819

1920
public class SampleConstants {
20-
public static final String DOMAIN = "sample";
21+
public static final String DOMAIN = "sample";
2122
}

src/main/java/com/uber/cadence/samples/common/WorkflowExecutionHistoryPrinter.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* express or implied. See the License for the specific language governing
1515
* permissions and limitations under the License.
1616
*/
17+
1718
package com.uber.cadence.samples.common;
1819

1920
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
@@ -24,23 +25,27 @@
2425
import com.uber.cadence.serviceclient.WorkflowServiceTChannel;
2526

2627
/**
27-
* Simple example utility to pretty print workflow execution history.
28+
* Prints a workflow execution history to the console.
2829
*
2930
* @author fateev
3031
*/
3132
public class WorkflowExecutionHistoryPrinter {
3233

33-
public static void main(String[] args) throws Exception {
34-
if (args.length < 2) {
35-
System.err.println("Usage: java " + WorkflowExecutionHistoryPrinter.class.getName() + " <workflowId> <runId>");
36-
System.exit(1);
37-
}
38-
IWorkflowService cadenceService = new WorkflowServiceTChannel();
39-
WorkflowExecution workflowExecution = new WorkflowExecution();
40-
String workflowId = args[0];
41-
workflowExecution.setWorkflowId(workflowId);
42-
String runId = args[1];
43-
workflowExecution.setRunId(runId);
44-
System.out.println(WorkflowExecutionUtils.prettyPrintHistory(cadenceService, DOMAIN, workflowExecution, true));
34+
public static void main(String[] args) throws Exception {
35+
if (args.length < 2) {
36+
System.err.println(
37+
"Usage: java "
38+
+ WorkflowExecutionHistoryPrinter.class.getName()
39+
+ " <workflowId> <runId>");
40+
System.exit(1);
4541
}
42+
IWorkflowService cadenceService = new WorkflowServiceTChannel();
43+
WorkflowExecution workflowExecution = new WorkflowExecution();
44+
String workflowId = args[0];
45+
workflowExecution.setWorkflowId(workflowId);
46+
String runId = args[1];
47+
workflowExecution.setRunId(runId);
48+
System.out.println(
49+
WorkflowExecutionUtils.prettyPrintHistory(cadenceService, DOMAIN, workflowExecution, true));
50+
}
4651
}

src/main/java/com/uber/cadence/samples/fileprocessing/FileProcessingStarter.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,38 @@
1414
* express or implied. See the License for the specific language governing
1515
* permissions and limitations under the License.
1616
*/
17+
1718
package com.uber.cadence.samples.fileprocessing;
1819

1920
import static com.uber.cadence.samples.common.SampleConstants.DOMAIN;
2021

2122
import com.uber.cadence.client.WorkflowClient;
2223
import java.net.URL;
2324

24-
/**
25-
* This is used for launching a Workflow instance of file processing sample.
26-
*/
25+
/** Starts a file processing sample workflow. */
2726
public class FileProcessingStarter {
2827

29-
public static void main(String[] args) throws Exception {
30-
// Start Workflow instance
31-
WorkflowClient workflowClient = WorkflowClient.newInstance(DOMAIN);
32-
FileProcessingWorkflow workflow = workflowClient.newWorkflowStub(FileProcessingWorkflow.class);
33-
34-
System.out.println("Executing FileProcessingWorkflow");
35-
36-
URL source = new URL("http://www.google.com/");
37-
URL destination = new URL("http://dummy");
38-
39-
// This is going to block until the workflow completion.
40-
// This is rarely used in production. Use the commented code below for async start version.
41-
workflow.processFile(source, destination);
42-
System.out.println("FileProcessingWorkflow completed");
43-
44-
// Use this code instead of the above blocking call to start workflow asynchronously.
45-
// WorkflowExecution workflowExecution = WorkflowClient.start(workflow::processFile, source, destination);
46-
// System.out.println("Started periodic workflow with workflowId=\"" + workflowExecution.getWorkflowId()
47-
// + "\" and runId=\"" + workflowExecution.getRunId() + "\"");
48-
//
49-
System.exit(0);
50-
}
28+
public static void main(String[] args) throws Exception {
29+
WorkflowClient workflowClient = WorkflowClient.newInstance(DOMAIN);
30+
FileProcessingWorkflow workflow = workflowClient.newWorkflowStub(FileProcessingWorkflow.class);
31+
32+
System.out.println("Executing FileProcessingWorkflow");
33+
34+
URL source = new URL("http://www.google.com/");
35+
URL destination = new URL("http://dummy");
36+
37+
// This is going to block until the workflow completes.
38+
// This is rarely used in production. Use the commented code below for async start version.
39+
workflow.processFile(source, destination);
40+
System.out.println("FileProcessingWorkflow completed");
41+
42+
// Use this code instead of the above blocking call to start workflow asynchronously.
43+
// WorkflowExecution workflowExecution = WorkflowClient.start(workflow::processFile,
44+
// source, destination);
45+
// System.out.println("Started periodic workflow with workflowId=\"" +
46+
// workflowExecution.getWorkflowId()
47+
// + "\" and runId=\"" + workflowExecution.getRunId() + "\"");
48+
//
49+
System.exit(0);
50+
}
5151
}

0 commit comments

Comments
 (0)