22
33import static org .junit .Assume .assumeTrue ;
44
5- import io .nexusrpc .handler .HandlerException ;
65import io .nexusrpc .handler .OperationHandler ;
76import io .nexusrpc .handler .OperationImpl ;
87import io .nexusrpc .handler .ServiceImpl ;
98import io .temporal .activity .ActivityInterface ;
109import io .temporal .activity .ActivityMethod ;
1110import io .temporal .client .StartActivityOptions ;
12- import io .temporal .client .WorkflowFailedException ;
13- import io .temporal .failure .ApplicationFailure ;
14- import io .temporal .failure .NexusOperationFailure ;
1511import io .temporal .internal .nexus .OperationToken ;
1612import io .temporal .internal .nexus .OperationTokenType ;
1713import io .temporal .internal .nexus .OperationTokenUtil ;
2521import org .junit .Rule ;
2622import org .junit .Test ;
2723
28- public class AsyncActivityOperationTest extends BaseNexusTest {
24+ public class AsyncActivityOperationTest {
2925
3026 @ Rule
3127 public SDKTestWorkflowRule testWorkflowRule =
3228 SDKTestWorkflowRule .newBuilder ()
33- .setWorkflowTypes (TestNexus .class , DoubleStartNexus . class )
29+ .setWorkflowTypes (TestNexus .class )
3430 .setActivityImplementations (new TestActivityImpl ())
35- .setNexusServiceImplementation (
36- new TestNexusServiceImpl (), new DoubleStartNexusServiceImpl ())
31+ .setNexusServiceImplementation (new TestNexusServiceImpl ())
3732 .build ();
3833
39- @ Override
40- protected SDKTestWorkflowRule getTestWorkflowRule () {
41- return testWorkflowRule ;
42- }
43-
4434 @ Test
4535 public void testActivityOperationEndToEnd () {
4636 // The in-process test-server does not implement the StartActivityExecution RPC; the
4737 // standalone-activity Nexus path requires a real server. Unit-only token assertions stay
4838 // active in OperationTokenTest.
4939 assumeTrue (SDKTestWorkflowRule .useExternalService );
50- // Combined (a) caller workflow receives "hello " + input
51- // (b) opToken loads as ACTIVITY_EXECUTION with aid="act-" + requestId
40+ // The caller workflow receives the activity result and an ACTIVITY_EXECUTION operation token.
5241 TestWorkflows .TestWorkflow1 workflowStub =
5342 testWorkflowRule .newWorkflowStubTimeoutOptions (TestWorkflows .TestWorkflow1 .class );
5443 String result = workflowStub .execute ("world" );
5544 Assert .assertEquals ("hello world" , result );
5645 }
5746
58- @ Test
59- public void testDoubleStartActivityThrows () {
60- // The first start RPC requires StartActivityExecution which is not implemented by the
61- // in-process test server; gate this on a real server so the guard at the second call can be
62- // observed.
63- assumeTrue (SDKTestWorkflowRule .useExternalService );
64- // (c) Calling startActivity twice in one handler invocation throws
65- // HandlerException(BAD_REQUEST).
66- TestDoubleStartWorkflow workflowStub =
67- testWorkflowRule .newWorkflowStubTimeoutOptions (TestDoubleStartWorkflow .class );
68- WorkflowFailedException e =
69- Assert .assertThrows (WorkflowFailedException .class , () -> workflowStub .execute ("anything" ));
70- Assert .assertTrue (e .getCause () instanceof NexusOperationFailure );
71- NexusOperationFailure nexusFailure = (NexusOperationFailure ) e .getCause ();
72- Assert .assertTrue (nexusFailure .getCause () instanceof HandlerException );
73- HandlerException handlerException = (HandlerException ) nexusFailure .getCause ();
74- Assert .assertTrue (handlerException .getCause () instanceof ApplicationFailure );
75- ApplicationFailure appFailure = (ApplicationFailure ) handlerException .getCause ();
76- Assert .assertEquals ("java.lang.IllegalStateException" , appFailure .getType ());
77- Assert .assertTrue (
78- appFailure
79- .getOriginalMessage ()
80- .startsWith ("Only one async operation can be started per operation handler" ));
81- }
82-
8347 @ ActivityInterface
8448 public interface TestActivity {
8549 @ ActivityMethod
@@ -101,14 +65,10 @@ public String execute(String input) {
10165 .setScheduleToCloseTimeout (Duration .ofSeconds (20 ))
10266 .build ();
10367 NexusServiceOptions serviceOptions =
104- NexusServiceOptions .newBuilder ()
105- .setEndpoint (getEndpointName ())
106- .setOperationOptions (options )
107- .build ();
68+ NexusServiceOptions .newBuilder ().setOperationOptions (options ).build ();
10869 TestNexusServices .TestNexusService1 serviceStub =
10970 Workflow .newNexusServiceStub (TestNexusServices .TestNexusService1 .class , serviceOptions );
11071
111- // Drive a handle so we can inspect the operation token for assertion (b).
11272 NexusOperationHandle <String > handle =
11373 Workflow .startNexusOperation (serviceStub ::operation , input );
11474 NexusOperationExecution exec = handle .getExecution ().get ();
@@ -140,66 +100,4 @@ public OperationHandler<String, String> operation() {
140100 .build ()));
141101 }
142102 }
143-
144- // ---------- Double-start test scaffolding ----------
145-
146- public static class DoubleStartNexus implements TestDoubleStartWorkflow {
147- @ Override
148- public String execute (String input ) {
149- NexusOperationOptions options =
150- NexusOperationOptions .newBuilder ()
151- .setScheduleToCloseTimeout (Duration .ofSeconds (10 ))
152- .build ();
153- NexusServiceOptions serviceOptions =
154- NexusServiceOptions .newBuilder ()
155- .setEndpoint (getEndpointName ())
156- .setOperationOptions (options )
157- .build ();
158- DoubleStartNexusService stub =
159- Workflow .newNexusServiceStub (DoubleStartNexusService .class , serviceOptions );
160- return stub .operation (input );
161- }
162- }
163-
164- @ WorkflowInterface
165- public interface TestDoubleStartWorkflow {
166- @ WorkflowMethod
167- String execute (String input );
168- }
169-
170- @ io .nexusrpc .Service
171- public interface DoubleStartNexusService {
172- @ io .nexusrpc .Operation
173- String operation (String input );
174- }
175-
176- @ ServiceImpl (service = DoubleStartNexusService .class )
177- public class DoubleStartNexusServiceImpl {
178- @ OperationImpl
179- public OperationHandler <String , String > operation () {
180- return TemporalOperationHandler .create (
181- (context , client , input ) -> {
182- // First start should succeed.
183- client .startActivity (
184- TestActivity .class ,
185- TestActivity ::process ,
186- input ,
187- StartActivityOptions .newBuilder ()
188- .setId ("act-first-" + context .getRequestId ())
189- .setTaskQueue (testWorkflowRule .getTaskQueue ())
190- .setStartToCloseTimeout (Duration .ofSeconds (10 ))
191- .build ());
192- // Second start must throw HandlerException(BAD_REQUEST).
193- return client .startActivity (
194- TestActivity .class ,
195- TestActivity ::process ,
196- input ,
197- StartActivityOptions .newBuilder ()
198- .setId ("act-second-" + context .getRequestId ())
199- .setTaskQueue (testWorkflowRule .getTaskQueue ())
200- .setStartToCloseTimeout (Duration .ofSeconds (10 ))
201- .build ());
202- });
203- }
204- }
205103}
0 commit comments