3636class VakoTest {
3737 @ ParameterizedTest
3838 @ CsvSource ({
39- "false, 20" ,
40- "true, 20"
39+ "10"
4140 })
42- void asyncCallbackLoop (final boolean optimized , final int iterations ) throws Exception {
41+ void asyncCallbackLoop (final int iterations ) throws Exception {
4342 System .err .printf ("baselineStackDepth=%d%n" , Thread .currentThread ().getStackTrace ().length );
4443 CompletableFuture <Void > join = new CompletableFuture <>();
4544 LoopState loopState = new LoopState ();
46- new AsyncCallbackLoop (optimized , loopState , c -> {
45+ new AsyncCallbackLoop (loopState , c -> {
4746 int iteration = loopState .iteration ();
4847 System .err .printf ("iteration=%d, callStackDepth=%d%n" , iteration , Thread .currentThread ().getStackTrace ().length );
4948 if (!loopState .breakAndCompleteIf (() -> iteration == (iterations - 1 ), c )) {
@@ -59,66 +58,68 @@ void asyncCallbackLoop(final boolean optimized, final int iterations) throws Exc
5958 }
6059 });
6160 join .get ();
61+ System .err .printf ("%n%nDONE%n%n" );
6262 }
6363
6464 @ ParameterizedTest ()
6565 @ CsvSource ({
66- "false, false, 0, 20" ,
67- "false, true, 4, 20" ,
68- "true, false, 0, 20" ,
69- "true, true, 4, 20"
66+ "0, false, 0, 10" ,
67+ "0, true, 4, 10" ,
68+ "4, true, 0, 10"
7069 })
7170 void testThenRunDoWhileLoop (
72- final boolean optimized ,
73- final boolean separateThread ,
74- final int sleepSeconds ,
71+ final int blockInAsyncMethodTotalSeconds ,
72+ final boolean asyncExecution ,
73+ final int delayAsyncExecutionTotalSeconds ,
7574 final int counterInitialValue ) throws Exception {
76- Duration totalSleepDuration = Duration .ofSeconds (sleepSeconds );
75+ Duration blockInAsyncMethodTotalDuration = Duration .ofSeconds (blockInAsyncMethodTotalSeconds );
76+ com .mongodb .assertions .Assertions .assertTrue (asyncExecution || delayAsyncExecutionTotalSeconds == 0 );
77+ Duration delayAsyncExecutionTotalDuration = Duration .ofSeconds (delayAsyncExecutionTotalSeconds );
7778 StartTime start = StartTime .now ();
7879 System .err .printf ("baselineStackDepth=%d%n" , Thread .currentThread ().getStackTrace ().length );
7980 CompletableFuture <Void > join = new CompletableFuture <>();
80- asyncMethod1 (optimized , separateThread , totalSleepDuration , new Counter (counterInitialValue ), (r , t ) -> {
81- System .err .printf ("TEST callback completed callStackDepth=%s, r=%s, t=%s%n" ,
82- Thread .currentThread ().getStackTrace ().length , r , exceptionToString (t ));
83- if (t != null ) {
84- join .completeExceptionally (t );
85- } else {
86- join .complete (r );
87- }
81+ asyncMethod1 (blockInAsyncMethodTotalDuration , asyncExecution , delayAsyncExecutionTotalDuration , new Counter (counterInitialValue ),
82+ (r , t ) -> {
83+ System .err .printf ("TEST callback completed callStackDepth=%s, r=%s, t=%s%n" ,
84+ Thread .currentThread ().getStackTrace ().length , r , exceptionToString (t ));
85+ if (t != null ) {
86+ join .completeExceptionally (t );
87+ } else {
88+ join .complete (r );
89+ }
8890 });
8991 System .err .printf ("asyncMethod1 returned in %s%n" , start .elapsed ());
9092 join .get ();
91- sleep (Duration .ofSeconds (1 ));
92- System .err .printf ("%nDONE%n" );
93+ System .err .printf ("%n%nDONE%n%n" );
9394 }
9495
9596 private static void asyncMethod1 (
96- final boolean optimized ,
97- final boolean separateThread ,
98- final Duration totalSleepDuration ,
97+ final Duration blockInAsyncMethodTotalDuration ,
98+ final boolean asyncExecution ,
99+ final Duration delayAsyncExecutionTotalDuration ,
99100 final Counter counter ,
100101 final SingleResultCallback <Void > callback ) {
101- beginAsync ().thenRunDoWhileLoop (optimized , c -> {
102- // sleep(totalSleepDuration .dividedBy(counter.initial()));
102+ beginAsync ().thenRunDoWhileLoop (c -> {
103+ sleep (blockInAsyncMethodTotalDuration .dividedBy (counter .initial ()));
103104 StartTime start = StartTime .now ();
104- asyncMethod2 (separateThread , totalSleepDuration , counter , c );
105+ asyncMethod2 (asyncExecution , delayAsyncExecutionTotalDuration , counter , c );
105106 System .err .printf ("asyncMethod2 returned in %s%n" , start .elapsed ());
106107 }, () -> !counter .done ()).finish (callback );
107108 }
108109
109110 private static void asyncMethod2 (
110- final boolean separateThread ,
111- final Duration totalSleepDuration ,
111+ final boolean asyncExecution ,
112+ final Duration delayAsyncExecutionTotalDuration ,
112113 final Counter counter ,
113114 final SingleResultCallback <Void > callback ) {
114115 Runnable action = () -> {
115- sleep (totalSleepDuration .dividedBy (counter .initial ()));
116+ sleep (delayAsyncExecutionTotalDuration .dividedBy (counter .initial ()));
116117 counter .countDown ();
117118 StartTime start = StartTime .now ();
118119 callback .complete (callback );
119120 System .err .printf ("asyncMethod2 callback.complete returned in %s%n" , start .elapsed ());
120121 };
121- if (separateThread ) {
122+ if (asyncExecution ) {
122123 ForkJoinPool .commonPool ().execute (action );
123124 } else {
124125 action .run ();
@@ -169,7 +170,7 @@ private static String exceptionToString(@Nullable final Throwable t) {
169170 }
170171 }
171172
172- private static void sleep (Duration duration ) {
173+ private static void sleep (final Duration duration ) {
173174 if (duration .isZero ()) {
174175 return ;
175176 }
0 commit comments