@@ -124,13 +124,16 @@ public void shouldAwaitActiveExchange() throws InterruptedException {
124124
125125 // Complete the exchange one second into the future
126126 final Duration exchangeDuration = Duration .ofSeconds (1 );
127+ // taking start time before entering completeExchange to account for possible
128+ // delays in reaching server.stop().
129+ final long startTime = System .nanoTime ();
127130 completeExchange (exchangeDuration );
128131 log ("Complete Exchange triggered" );
129132
130133 // Time the shutdown sequence
131- final Duration delayDuration = Duration .ofSeconds (Utils .adjustTimeout (5 ));
134+ final Duration delayDuration = Duration .ofSeconds (Utils .adjustTimeout (20 ));
132135 log ("Shutdown triggered with the delay of " + delayDuration .getSeconds ());
133- final long elapsed = timeShutdown (delayDuration );
136+ final long elapsed = timeShutdown (delayDuration , startTime );
134137 log ("Shutdown complete" );
135138
136139 // The shutdown should take at least as long as the exchange duration
@@ -151,31 +154,20 @@ public void shouldAwaitActiveExchange() throws InterruptedException {
151154 * @throws InterruptedException if an unexpected interruption occurs
152155 */
153156 @ Test
154- public void shouldCompeteAfterDelay () throws InterruptedException {
157+ public void shouldCompleteAfterDelay () throws InterruptedException {
155158 // Initiate an exchange
156159 startExchange ();
157160 // Wait for the server to receive the exchange
158161 start .await ();
159162 log ("Exchange started" );
160163
161- // Complete the exchange 10 second into the future.
162- // Runs in parallel, so won't block the server stop
163- final Duration exchangeDuration = Duration .ofSeconds (Utils .adjustTimeout (10 ));
164- completeExchange (exchangeDuration );
165- log ("Complete Exchange triggered" );
166-
167-
168164 // Time the shutdown sequence
169165 final Duration delayDuration = Duration .ofSeconds (1 );
170166 log ("Shutdown triggered with the delay of " + delayDuration .getSeconds ());
171167 final long elapsed = timeShutdown (delayDuration );
172168 log ("Shutdown complete" );
173-
174-
175- // The shutdown should not await the exchange to complete
176- if (elapsed >= exchangeDuration .toNanos ()) {
177- fail ("HttpServer.stop terminated too late" );
178- }
169+ complete .countDown ();
170+ log ("Exchange completed" );
179171
180172 // The shutdown delay should have expired
181173 if (elapsed < delayDuration .toNanos ()) {
@@ -277,7 +269,14 @@ public void shouldAllowRepeatedStop() {
277269 */
278270 private long timeShutdown (Duration delayDuration ) {
279271 final long startTime = System .nanoTime ();
272+ return timeShutdown (delayDuration , startTime );
273+ }
280274
275+ /**
276+ * This allows passing a custom start time
277+ */
278+ private long timeShutdown (Duration delayDuration ,
279+ long startTime ) {
281280 server .stop ((int ) delayDuration .toSeconds ());
282281 return System .nanoTime () - startTime ;
283282 }
0 commit comments