1515import java .util .stream .Stream ;
1616
1717import org .junit .jupiter .api .AfterEach ;
18+ import org .junit .jupiter .api .BeforeEach ;
1819import org .junit .jupiter .api .Tag ;
1920import org .junit .jupiter .api .Test ;
2021import org .junit .jupiter .params .ParameterizedTest ;
@@ -36,6 +37,13 @@ public class IntegrationTests extends IntegrationTestBase {
3637 // All tests that create a server should save it to this variable for proper shutdown
3738 private DurableTaskGrpcWorker server ;
3839
40+ // Before whole test suite, delete the task hub
41+ @ BeforeEach
42+ private void startUp () {
43+ DurableTaskClient client = new DurableTaskGrpcClientBuilder ().build ();
44+ client .deleteTaskHub ();
45+ }
46+
3947 @ AfterEach
4048 private void shutdown () throws InterruptedException {
4149 if (this .server != null ) {
@@ -793,6 +801,7 @@ void clearCustomStatus() throws TimeoutException {
793801 }
794802 }
795803
804+ // due to clock drift, client/worker and sidecar time are not exactly synchronized, this test needs to accommodate for client vs backend timestamps difference
796805 @ Test
797806 void multiInstanceQuery () throws TimeoutException {
798807 final String plusOne = "plusOne" ;
@@ -830,6 +839,11 @@ void multiInstanceQuery() throws TimeoutException{
830839 }
831840 });
832841
842+ try {
843+ Thread .sleep (2000 );
844+ } catch (InterruptedException e ) {
845+ }
846+
833847 Instant sequencesFinishedTime = Instant .now ();
834848
835849 IntStream .range (0 , 5 ).mapToObj (i -> {
@@ -853,28 +867,43 @@ void multiInstanceQuery() throws TimeoutException{
853867 assertEquals (10 , result .getOrchestrationState ().size ());
854868
855869 // Test CreatedTimeTo filter
856- query .setCreatedTimeTo (startTime );
870+ query .setCreatedTimeTo (startTime . minus ( Duration . ofSeconds ( 1 )) );
857871 result = client .queryInstances (query );
858- assertTrue (result .getOrchestrationState ().isEmpty ());
872+ assertTrue (result .getOrchestrationState ().isEmpty (),
873+ "Result should be empty but found " + result .getOrchestrationState ().size () + " instances: " +
874+ "Start time: " + startTime + ", " +
875+ result .getOrchestrationState ().stream ()
876+ .map (state -> String .format ("\n ID: %s, Status: %s, Created: %s" ,
877+ state .getInstanceId (),
878+ state .getRuntimeStatus (),
879+ state .getCreatedAt ()))
880+ .collect (Collectors .joining (", " )));
859881
860882 query .setCreatedTimeTo (sequencesFinishedTime );
861883 result = client .queryInstances (query );
862- assertEquals (5 , result .getOrchestrationState ().size ());
884+ // Verify all returned instances contain "sequence" in their IDs
885+ assertEquals (5 , result .getOrchestrationState ().stream ()
886+ .filter (state -> state .getInstanceId ().contains ("sequence" ))
887+ .count (),
888+ "Expected exactly 5 instances with 'sequence' in their IDs" );
863889
864- query .setCreatedTimeTo (Instant .now ());
890+ query .setCreatedTimeTo (Instant .now (). plus ( Duration . ofSeconds ( 1 )) );
865891 result = client .queryInstances (query );
866892 assertEquals (10 , result .getOrchestrationState ().size ());
867893
868894 // Test CreatedTimeFrom filter
869- query .setCreatedTimeFrom (Instant .now ());
895+ query .setCreatedTimeFrom (Instant .now (). plus ( Duration . ofSeconds ( 1 )) );
870896 result = client .queryInstances (query );
871897 assertTrue (result .getOrchestrationState ().isEmpty ());
872898
873- query .setCreatedTimeFrom (sequencesFinishedTime );
899+ query .setCreatedTimeFrom (sequencesFinishedTime . minus ( Duration . ofSeconds ( 3 )) );
874900 result = client .queryInstances (query );
875- assertEquals (5 , result .getOrchestrationState ().size ());
901+ assertEquals (5 , result .getOrchestrationState ().stream ()
902+ .filter (state -> state .getInstanceId ().contains ("sequence" ))
903+ .count (),
904+ "Expected exactly 5 instances with 'sequence' in their IDs" );
876905
877- query .setCreatedTimeFrom (startTime );
906+ query .setCreatedTimeFrom (startTime . minus ( Duration . ofSeconds ( 1 )) );
878907 result = client .queryInstances (query );
879908 assertEquals (10 , result .getOrchestrationState ().size ());
880909
@@ -1028,7 +1057,7 @@ void purgeInstanceFilter() throws TimeoutException {
10281057
10291058 // Test CreatedTimeFrom
10301059 PurgeInstanceCriteria criteria = new PurgeInstanceCriteria ();
1031- criteria .setCreatedTimeFrom (startTime );
1060+ criteria .setCreatedTimeFrom (startTime . minus ( Duration . ofSeconds ( 1 )) );
10321061
10331062 PurgeResult result = client .purgeInstances (criteria );
10341063 assertEquals (1 , result .getDeletedInstanceCount ());
0 commit comments