44package com .azure .compute .batch ;
55
66import com .azure .compute .batch .implementation .task .TaskManager ;
7+ import com .azure .compute .batch .implementation .task .TaskSubmitter ;
78import com .azure .compute .batch .models .BatchTaskBulkCreateOptions ;
89import com .azure .compute .batch .models .BatchTaskCreateParameters ;
910import com .azure .compute .batch .models .CreateTasksErrorException ;
10- import org .junit .jupiter .api .*;
11+ import org .junit .jupiter .api .RepeatedTest ;
12+ import org .junit .jupiter .api .Test ;
1113import org .junit .jupiter .api .parallel .Execution ;
1214import org .junit .jupiter .api .parallel .ExecutionMode ;
1315import org .junit .jupiter .api .parallel .Isolated ;
14-
15- import java .util .*;
16+ import org .junit .jupiter .params .ParameterizedTest ;
17+ import org .junit .jupiter .params .provider .ValueSource ;
18+
19+ import java .io .IOException ;
20+ import java .util .Arrays ;
21+ import java .util .Collections ;
22+ import java .util .HashMap ;
23+ import java .util .HashSet ;
24+ import java .util .List ;
25+ import java .util .Map ;
26+ import java .util .Set ;
1627import java .util .stream .Collectors ;
1728import java .util .stream .IntStream ;
1829
19- import static org .junit .jupiter .api .Assertions .*;
30+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
31+ import static org .junit .jupiter .api .Assertions .assertEquals ;
32+ import static org .junit .jupiter .api .Assertions .assertFalse ;
33+ import static org .junit .jupiter .api .Assertions .assertSame ;
34+ import static org .junit .jupiter .api .Assertions .assertThrows ;
35+ import static org .junit .jupiter .api .Assertions .assertTrue ;
2036
2137@ Execution (ExecutionMode .SAME_THREAD )
2238@ Isolated
@@ -28,7 +44,7 @@ private static List<BatchTaskCreateParameters> makeTasks(String prefix, int coun
2844 .collect (Collectors .toList ());
2945 }
3046
31- private static void runCreateTasks (FakeTaskSubmitter submitter , List <BatchTaskCreateParameters > tasks ) {
47+ private static void runCreateTasks (TaskSubmitter submitter , List <BatchTaskCreateParameters > tasks ) {
3248 TaskManager .createTasks (submitter , "job-1" , tasks , new BatchTaskBulkCreateOptions ().setMaxConcurrency (4 ));
3349 }
3450
@@ -44,11 +60,11 @@ public void serverErrorsAreRetriedUntilSuccessNoCap() {
4460
4561 assertDoesNotThrow (() -> runCreateTasks (fake , tasks ));
4662
47- assertTrue ( fake .getSubmissionCount ("A" ) > 1 , "A should have been retried at least once " );
63+ assertEquals ( 11 , fake .getSubmissionCount ("A" ), "A should succeed after ten retries " );
4864 assertEquals (1 , fake .getSubmissionCount ("B" ), "B should submit once" );
4965 }
5066
51- @ Test
67+ @ RepeatedTest ( 1000 )
5268 public void successAfterOneServerErrorDidRetry () {
5369 Map <String , Integer > serverErrors = new HashMap <>();
5470 serverErrors .put ("X" , 1 ); // one failure then success
@@ -57,7 +73,45 @@ public void successAfterOneServerErrorDidRetry() {
5773 List <BatchTaskCreateParameters > tasks = Collections .singletonList (new BatchTaskCreateParameters ("X" , "echo X" ));
5874
5975 assertDoesNotThrow (() -> runCreateTasks (fake , tasks ));
60- assertTrue (fake .getSubmissionCount ("X" ) >= 2 , "X should have been retried at least once" );
76+ assertEquals (2 , fake .getSubmissionCount ("X" ), "X should succeed after one retry" );
77+ }
78+
79+ @ ParameterizedTest
80+ @ ValueSource (ints = { 1 , 4 })
81+ public void retriesAcrossMultipleChunks (int maxConcurrency ) {
82+ List <BatchTaskCreateParameters > tasks = makeTasks ("T" , 201 );
83+ Map <String , Integer > serverErrors = new HashMap <>();
84+ tasks .forEach (task -> serverErrors .put (task .getId (), 1 ));
85+ FakeTaskSubmitter fake = new FakeTaskSubmitter (Collections .emptySet (), serverErrors , 0 );
86+
87+ assertDoesNotThrow (() -> TaskManager .createTasks (fake , "job-1" , tasks ,
88+ new BatchTaskBulkCreateOptions ().setMaxConcurrency (maxConcurrency )));
89+
90+ tasks .forEach (task -> assertEquals (2 , fake .getSubmissionCount (task .getId ()),
91+ task .getId () + " should succeed after one retry" ));
92+ }
93+
94+ @ RepeatedTest (100 )
95+ public void submissionExceptionIsPropagated () {
96+ RuntimeException expected = new IllegalStateException ("Submission failed" );
97+ TaskSubmitter submitter = (jobId , taskCollection ) -> {
98+ throw expected ;
99+ };
100+
101+ assertSame (expected , assertThrows (IllegalStateException .class ,
102+ () -> runCreateTasks (submitter , Collections .singletonList (new BatchTaskCreateParameters ("X" , "echo X" )))));
103+ }
104+
105+ @ Test
106+ public void checkedSubmissionExceptionIsPropagated () {
107+ IOException expected = new IOException ("Submission failed" );
108+ TaskSubmitter submitter = (jobId , taskCollection ) -> {
109+ throw expected ;
110+ };
111+
112+ RuntimeException actual = assertThrows (RuntimeException .class ,
113+ () -> runCreateTasks (submitter , Collections .singletonList (new BatchTaskCreateParameters ("X" , "echo X" ))));
114+ assertSame (expected , actual .getCause ());
61115 }
62116
63117 @ Test
0 commit comments