Skip to content

Commit 385adc4

Browse files
authored
Code samples for testing and mocking Nexus (#771)
* Code samples for testing and mocking Nexus * Formatting changes from Spotless * Added two more classes that mock the Nexus Service itself * Renamed EchoHandler to EchoClient * Updating gradle wrapper validator * Changed NexusService name for clarity, modified two tests * Suppressing two false warnings that turned into build errors
1 parent bf3515d commit 385adc4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+629
-151
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88
name: "Gradle wrapper validation"
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: actions/checkout@v4
12-
- uses: gradle/wrapper-validation-action@v3
11+
- uses: actions/checkout@v6
12+
- uses: gradle/actions/wrapper-validation@v5
1313

1414
unittest:
1515
name: Unit Tests

core/src/main/java/io/temporal/samples/nexus/caller/CallerStarter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import io.temporal.client.WorkflowClient;
55
import io.temporal.client.WorkflowOptions;
66
import io.temporal.samples.nexus.options.ClientOptions;
7-
import io.temporal.samples.nexus.service.NexusService;
7+
import io.temporal.samples.nexus.service.SampleNexusService;
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010

@@ -26,11 +26,12 @@ public static void main(String[] args) {
2626
logger.info("Workflow result: {}", echoWorkflow.echo("Nexus Echo 👋"));
2727
HelloCallerWorkflow helloWorkflow =
2828
client.newWorkflowStub(HelloCallerWorkflow.class, workflowOptions);
29-
execution = WorkflowClient.start(helloWorkflow::hello, "Nexus", NexusService.Language.EN);
29+
execution = WorkflowClient.start(helloWorkflow::hello, "Nexus", SampleNexusService.Language.EN);
3030
logger.info(
3131
"Started HelloCallerWorkflow workflowId: {} runId: {}",
3232
execution.getWorkflowId(),
3333
execution.getRunId());
34-
logger.info("Workflow result: {}", helloWorkflow.hello("Nexus", NexusService.Language.ES));
34+
logger.info(
35+
"Workflow result: {}", helloWorkflow.hello("Nexus", SampleNexusService.Language.ES));
3536
}
3637
}

core/src/main/java/io/temporal/samples/nexus/caller/CallerWorker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static void main(String[] args) {
2121
WorkflowImplementationOptions.newBuilder()
2222
.setNexusServiceOptions(
2323
Collections.singletonMap(
24-
"NexusService",
24+
"SampleNexusService",
2525
NexusServiceOptions.newBuilder().setEndpoint("my-nexus-endpoint-name").build()))
2626
.build(),
2727
EchoCallerWorkflowImpl.class,
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package io.temporal.samples.nexus.caller;
22

3-
import io.temporal.samples.nexus.service.NexusService;
3+
import io.temporal.samples.nexus.service.SampleNexusService;
44
import io.temporal.workflow.NexusOperationOptions;
55
import io.temporal.workflow.NexusServiceOptions;
66
import io.temporal.workflow.Workflow;
77
import java.time.Duration;
88

99
public class EchoCallerWorkflowImpl implements EchoCallerWorkflow {
10-
NexusService nexusService =
10+
SampleNexusService sampleNexusService =
1111
Workflow.newNexusServiceStub(
12-
NexusService.class,
12+
SampleNexusService.class,
1313
NexusServiceOptions.newBuilder()
1414
.setOperationOptions(
1515
NexusOperationOptions.newBuilder()
@@ -19,6 +19,6 @@ public class EchoCallerWorkflowImpl implements EchoCallerWorkflow {
1919

2020
@Override
2121
public String echo(String message) {
22-
return nexusService.echo(new NexusService.EchoInput(message)).getMessage();
22+
return sampleNexusService.echo(new SampleNexusService.EchoInput(message)).getMessage();
2323
}
2424
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package io.temporal.samples.nexus.caller;
22

3-
import io.temporal.samples.nexus.service.NexusService;
3+
import io.temporal.samples.nexus.service.SampleNexusService;
44
import io.temporal.workflow.WorkflowInterface;
55
import io.temporal.workflow.WorkflowMethod;
66

77
@WorkflowInterface
88
public interface HelloCallerWorkflow {
99
@WorkflowMethod
10-
String hello(String message, NexusService.Language language);
10+
String hello(String message, SampleNexusService.Language language);
1111
}

core/src/main/java/io/temporal/samples/nexus/caller/HelloCallerWorkflowImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package io.temporal.samples.nexus.caller;
22

3-
import io.temporal.samples.nexus.service.NexusService;
3+
import io.temporal.samples.nexus.service.SampleNexusService;
44
import io.temporal.workflow.NexusOperationHandle;
55
import io.temporal.workflow.NexusOperationOptions;
66
import io.temporal.workflow.NexusServiceOptions;
77
import io.temporal.workflow.Workflow;
88
import java.time.Duration;
99

1010
public class HelloCallerWorkflowImpl implements HelloCallerWorkflow {
11-
NexusService nexusService =
11+
SampleNexusService sampleNexusService =
1212
Workflow.newNexusServiceStub(
13-
NexusService.class,
13+
SampleNexusService.class,
1414
NexusServiceOptions.newBuilder()
1515
.setOperationOptions(
1616
NexusOperationOptions.newBuilder()
@@ -19,10 +19,10 @@ public class HelloCallerWorkflowImpl implements HelloCallerWorkflow {
1919
.build());
2020

2121
@Override
22-
public String hello(String message, NexusService.Language language) {
23-
NexusOperationHandle<NexusService.HelloOutput> handle =
22+
public String hello(String message, SampleNexusService.Language language) {
23+
NexusOperationHandle<SampleNexusService.HelloOutput> handle =
2424
Workflow.startNexusOperation(
25-
nexusService::hello, new NexusService.HelloInput(message, language));
25+
sampleNexusService::hello, new SampleNexusService.HelloInput(message, language));
2626
// Optionally wait for the operation to be started. NexusOperationExecution will contain the
2727
// operation token in case this operation is asynchronous.
2828
handle.getExecution().get();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.temporal.samples.nexus.handler;
2+
3+
import io.temporal.samples.nexus.service.SampleNexusService;
4+
5+
public interface EchoClient {
6+
SampleNexusService.EchoOutput echo(SampleNexusService.EchoInput input);
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.temporal.samples.nexus.handler;
2+
3+
import io.temporal.samples.nexus.service.SampleNexusService;
4+
5+
// Note that this is a class, not a Temporal worker. This is to demonstrate that Nexus services can
6+
// simply call a class instead of a worker for fast operations that don't need retry handling.
7+
public class EchoClientImpl implements EchoClient {
8+
@Override
9+
public SampleNexusService.EchoOutput echo(SampleNexusService.EchoInput input) {
10+
return new SampleNexusService.EchoOutput(input.getMessage());
11+
}
12+
}

core/src/main/java/io/temporal/samples/nexus/handler/HandlerWorker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void main(String[] args) {
1515

1616
Worker worker = factory.newWorker(DEFAULT_TASK_QUEUE_NAME);
1717
worker.registerWorkflowImplementationTypes(HelloHandlerWorkflowImpl.class);
18-
worker.registerNexusServiceImplementation(new NexusServiceImpl());
18+
worker.registerNexusServiceImplementation(new SampleNexusServiceImpl());
1919

2020
factory.start();
2121
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package io.temporal.samples.nexus.handler;
22

3-
import io.temporal.samples.nexus.service.NexusService;
3+
import io.temporal.samples.nexus.service.SampleNexusService;
44
import io.temporal.workflow.WorkflowInterface;
55
import io.temporal.workflow.WorkflowMethod;
66

77
@WorkflowInterface
88
public interface HelloHandlerWorkflow {
99
@WorkflowMethod
10-
NexusService.HelloOutput hello(NexusService.HelloInput input);
10+
SampleNexusService.HelloOutput hello(SampleNexusService.HelloInput input);
1111
}

0 commit comments

Comments
 (0)