@@ -119,22 +119,25 @@ void singleTimer() throws IOException, TimeoutException {
119119 for (int i = 0 ; i < timestamps .length () - 1 ; i ++) {
120120 secondsElapsed [i ] = timestamps .get (i + 1 ).getSecond () - timestamps .get (i ).getSecond ();
121121 }
122- assertEquals (secondsElapsed [0 ], 3 );
122+ assertEquals (3 , secondsElapsed [0 ]);
123123
124124 }
125125 }
126126
127+
127128 @ Test
128129 void loopWithTimer () throws IOException , TimeoutException {
129130 final String orchestratorName = "LoopWithTimer" ;
130131 final Duration delay = Duration .ofSeconds (2 );
131- AtomicReferenceArray <LocalDateTime > timestamps = new AtomicReferenceArray <>(100 );
132+ AtomicReferenceArray <LocalDateTime > timestamps = new AtomicReferenceArray <>(4 );
132133 AtomicInteger counter = new AtomicInteger ();
133134 DurableTaskGrpcWorker worker = this .createWorkerBuilder ()
134135 .addOrchestrator (orchestratorName , ctx -> {
135- for (int i = 0 ; i < 10 ; i ++) {
136- timestamps .set (counter .get (), LocalDateTime .now ());
137- counter .incrementAndGet ();
136+ for (int i = 0 ; i < 3 ; i ++) {
137+ if (!ctx .getIsReplaying ()) {
138+ timestamps .set (counter .get (), LocalDateTime .now ());
139+ counter .incrementAndGet ();
140+ }
138141 ctx .createTimer (delay ).await ();
139142 }
140143 })
@@ -154,8 +157,7 @@ void loopWithTimer() throws IOException, TimeoutException {
154157 assertTrue (expectedCompletionSecond <= actualCompletionSecond );
155158
156159 // Verify that the correct number of timers were created
157- // ??? Not sure why 65, this seems consistent with what we see in Catalyst
158- assertEquals (65 , counter .get ());
160+ assertEquals (3 , counter .get ());
159161
160162 // Verify that each timer is the expected length
161163 int [] secondsElapsed = new int [timestamps .length ()];
@@ -166,7 +168,66 @@ void loopWithTimer() throws IOException, TimeoutException {
166168 secondsElapsed [i ] = -1 ;
167169 }
168170 }
169- assertEquals (secondsElapsed [0 ], 2 );
171+ assertEquals (2 , secondsElapsed [0 ]);
172+ assertEquals (2 , secondsElapsed [1 ]);
173+ assertEquals (-1 , secondsElapsed [2 ]);
174+
175+
176+ }
177+ }
178+
179+ @ Test
180+ void loopWithWaitForEvent () throws IOException , TimeoutException {
181+ final String orchestratorName = "LoopWithTimer" ;
182+ final Duration delay = Duration .ofSeconds (2 );
183+ AtomicReferenceArray <LocalDateTime > timestamps = new AtomicReferenceArray <>(4 );
184+ AtomicInteger counter = new AtomicInteger ();
185+ DurableTaskGrpcWorker worker = this .createWorkerBuilder ()
186+ .addOrchestrator (orchestratorName , ctx -> {
187+ for (int i = 0 ; i < 4 ; i ++) {
188+ try {
189+ ctx .waitForExternalEvent ("HELLO" , delay ).await ();
190+ }catch (TaskCanceledException tce ){
191+ if (!ctx .getIsReplaying ()){
192+ timestamps .set (counter .get (), LocalDateTime .now ());
193+ counter .incrementAndGet ();
194+ }
195+
196+ }
197+ }
198+ })
199+ .buildAndStart ();
200+
201+ DurableTaskClient client = new DurableTaskGrpcClientBuilder ().build ();
202+ try (worker ; client ) {
203+ String instanceId = client .scheduleNewOrchestrationInstance (orchestratorName );
204+ Duration timeout = delay .plus (defaultTimeout );
205+ OrchestrationMetadata instance = client .waitForInstanceCompletion (instanceId , timeout , false );
206+ assertNotNull (instance );
207+ assertEquals (OrchestrationRuntimeStatus .COMPLETED , instance .getRuntimeStatus ());
208+
209+ // Verify that the delay actually happened
210+ long expectedCompletionSecond = instance .getCreatedAt ().plus (delay ).getEpochSecond ();
211+ long actualCompletionSecond = instance .getLastUpdatedAt ().getEpochSecond ();
212+ assertTrue (expectedCompletionSecond <= actualCompletionSecond );
213+
214+ // Verify that the correct number of timers were created
215+ assertEquals (4 , counter .get ());
216+
217+ // Verify that each timer is the expected length
218+ int [] secondsElapsed = new int [timestamps .length ()];
219+ for (int i = 0 ; i < timestamps .length () - 1 ; i ++) {
220+ if (timestamps .get (i + 1 ) != null && timestamps .get (i ) != null ) {
221+ secondsElapsed [i ] = timestamps .get (i + 1 ).getSecond () - timestamps .get (i ).getSecond ();
222+ }else {
223+ secondsElapsed [i ] = -1 ;
224+ }
225+ }
226+ assertEquals (2 , secondsElapsed [0 ]);
227+ assertEquals (2 , secondsElapsed [1 ]);
228+ assertEquals (2 , secondsElapsed [2 ]);
229+ assertEquals (0 , secondsElapsed [3 ]);
230+
170231
171232 }
172233 }
@@ -192,7 +253,7 @@ void longTimer() throws TimeoutException {
192253 Duration timeout = delay .plus (defaultTimeout );
193254 OrchestrationMetadata instance = client .waitForInstanceCompletion (instanceId , timeout , false );
194255 assertNotNull (instance );
195- assertEquals (OrchestrationRuntimeStatus .COMPLETED , instance .getRuntimeStatus (),
256+ assertEquals (OrchestrationRuntimeStatus .COMPLETED , instance .getRuntimeStatus (),
196257 String .format ("Orchestration failed with error: %s" , instance .getFailureDetails ().getErrorMessage ()));
197258
198259 // Verify that the delay actually happened
@@ -973,13 +1034,13 @@ void multiInstanceQuery() throws TimeoutException{
9731034 // Test CreatedTimeTo filter
9741035 query .setCreatedTimeTo (startTime .minus (Duration .ofSeconds (1 )));
9751036 result = client .queryInstances (query );
976- assertTrue (result .getOrchestrationState ().isEmpty (),
977- "Result should be empty but found " + result .getOrchestrationState ().size () + " instances: " +
1037+ assertTrue (result .getOrchestrationState ().isEmpty (),
1038+ "Result should be empty but found " + result .getOrchestrationState ().size () + " instances: " +
9781039 "Start time: " + startTime + ", " +
9791040 result .getOrchestrationState ().stream ()
980- .map (state -> String .format ("\n ID: %s, Status: %s, Created: %s" ,
981- state .getInstanceId (),
982- state .getRuntimeStatus (),
1041+ .map (state -> String .format ("\n ID: %s, Status: %s, Created: %s" ,
1042+ state .getInstanceId (),
1043+ state .getRuntimeStatus (),
9831044 state .getCreatedAt ()))
9841045 .collect (Collectors .joining (", " )));
9851046
@@ -1292,7 +1353,7 @@ void waitForInstanceStartThrowsException() {
12921353 client .scheduleNewOrchestrationInstance (orchestratorName , null , instanceId );
12931354 });
12941355 thread .start ();
1295-
1356+
12961357 assertThrows (TimeoutException .class , () -> client .waitForInstanceStart (instanceId , Duration .ofSeconds (2 )) );
12971358 }
12981359 }
@@ -1680,8 +1741,8 @@ public void taskExecutionIdTest() {
16801741
16811742 DurableTaskGrpcWorker worker = this .createWorkerBuilder ()
16821743 .addOrchestrator (orchestratorName , ctx -> {
1683- ctx .callActivity (retryActivityName ,null ,taskOptions ).await ();
1684- ctx .callActivity (retryActivityName ,null ,taskOptions ).await ();
1744+ ctx .callActivity (retryActivityName ,null ,taskOptions ).await ();
1745+ ctx .callActivity (retryActivityName ,null ,taskOptions ).await ();
16851746 ctx .complete (true );
16861747 })
16871748 .addActivity (retryActivityName , ctx -> {
0 commit comments