@@ -235,9 +235,10 @@ public void testHandshakeFutureIsNotifiedOnChannelClose() throws Exception { |
235 | 235 | @Test |
236 | 236 | @Timeout(value = 10000, unit = TimeUnit.MILLISECONDS) |
237 | 237 | public void testClientHandshakerForceClose() throws Exception { |
| 238 | + final long forceCloseTimeoutMillis = 10000; |
238 | 239 | final WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker( |
239 | 240 | new URI("ws://localhost:1234/test"), WebSocketVersion.V13, null, true, |
240 | | - EmptyHttpHeaders.INSTANCE, Integer.MAX_VALUE, true, false, 20); |
| 241 | + EmptyHttpHeaders.INSTANCE, Integer.MAX_VALUE, true, false, forceCloseTimeoutMillis); |
241 | 242 |
|
242 | 243 | EmbeddedChannel serverChannel = createServerChannel( |
243 | 244 | new CloseNoOpServerProtocolHandler("/test"), |
@@ -260,6 +261,10 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Except |
260 | 261 | } |
261 | 262 | }); |
262 | 263 |
|
| 264 | + // Freeze the clock of the client so the force close timeout can only fire when we advance the time |
| 265 | + // explicitly. Otherwise a slow machine could make the timeout expire before we check that it has not. |
| 266 | + clientChannel.freezeTime(); |
| 267 | + |
263 | 268 | // Transfer the handshake from the client to the server |
264 | 269 | transferAllDataWithMerge(clientChannel, serverChannel); |
265 | 270 | // Transfer the handshake from the server to client |
@@ -271,11 +276,15 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Except |
271 | 276 | // Should not be closed yet as we disabled closing the connection on the server |
272 | 277 | assertFalse(clientForceClosed); |
273 | 278 |
|
274 | | - while (!clientForceClosed) { |
275 | | - Thread.sleep(10); |
276 | | - // We need to run all pending tasks as the force close timeout is scheduled on the EventLoop. |
277 | | - clientChannel.runPendingTasks(); |
278 | | - } |
| 279 | + // Just before the timeout the connection must still be open. |
| 280 | + clientChannel.advanceTimeBy(forceCloseTimeoutMillis - 1, TimeUnit.MILLISECONDS); |
| 281 | + clientChannel.runScheduledPendingTasks(); |
| 282 | + assertFalse(clientForceClosed); |
| 283 | + |
| 284 | + // The force close timeout is scheduled on the EventLoop, so advance the time to trigger it. |
| 285 | + clientChannel.advanceTimeBy(1, TimeUnit.MILLISECONDS); |
| 286 | + clientChannel.runScheduledPendingTasks(); |
| 287 | + assertTrue(clientForceClosed); |
279 | 288 |
|
280 | 289 | // clientForceClosed would be set to TRUE after any close, |
281 | 290 | // so check here that force close timeout was actually fired |
|