@@ -1330,6 +1330,93 @@ public void testClientCloseAfterServerClosedWithQueuedConnection() throws Except |
1330 | 1330 | } |
1331 | 1331 | } |
1332 | 1332 |
|
| 1333 | + @Test |
| 1334 | + @Timeout(30) |
| 1335 | + public void testPeerWriteAndCloseWhileWriteInProgressDeliversData() throws Exception { |
| 1336 | + final CountDownLatch writeListenerEntered = new CountDownLatch(1); |
| 1337 | + final CountDownLatch releaseWriteListener = new CountDownLatch(1); |
| 1338 | + final CountDownLatch dataReceived = new CountDownLatch(1); |
| 1339 | + final CountDownLatch clientInactive = new CountDownLatch(1); |
| 1340 | + final AtomicReference<Boolean> receivedBeforeInactive = new AtomicReference<>(); |
| 1341 | + final BlockingQueue<LocalChannel> accepted = new LinkedBlockingQueue<>(); |
| 1342 | + |
| 1343 | + ServerBootstrap sb = new ServerBootstrap() |
| 1344 | + .group(group1, group2) |
| 1345 | + .channel(LocalServerChannel.class) |
| 1346 | + .childHandler(new ChannelInitializer<LocalChannel>() { |
| 1347 | + @Override |
| 1348 | + protected void initChannel(LocalChannel ch) { |
| 1349 | + accepted.add(ch); |
| 1350 | + } |
| 1351 | + }); |
| 1352 | + Bootstrap cb = new Bootstrap() |
| 1353 | + .group(group1) |
| 1354 | + .channel(LocalChannel.class) |
| 1355 | + .handler(new ChannelInitializer<LocalChannel>() { |
| 1356 | + @Override |
| 1357 | + protected void initChannel(LocalChannel ch) { |
| 1358 | + ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { |
| 1359 | + @Override |
| 1360 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 1361 | + ReferenceCountUtil.release(msg); |
| 1362 | + dataReceived.countDown(); |
| 1363 | + } |
| 1364 | + |
| 1365 | + @Override |
| 1366 | + public void channelInactive(ChannelHandlerContext ctx) { |
| 1367 | + receivedBeforeInactive.set(dataReceived.getCount() == 0); |
| 1368 | + clientInactive.countDown(); |
| 1369 | + } |
| 1370 | + }); |
| 1371 | + } |
| 1372 | + }); |
| 1373 | + |
| 1374 | + Channel sc = null; |
| 1375 | + Channel cc = null; |
| 1376 | + try { |
| 1377 | + sc = sb.bind(TEST_ADDRESS).sync().channel(); |
| 1378 | + cc = cb.connect(TEST_ADDRESS).sync().channel(); |
| 1379 | + final LocalChannel child = accepted.poll(5, SECONDS); |
| 1380 | + assertNotNull(child); |
| 1381 | + while (!child.isActive()) { |
| 1382 | + Thread.sleep(10); |
| 1383 | + } |
| 1384 | + |
| 1385 | + // Block the client inside doWrite(...) (the promise listener is run while writeInProgress is true) so |
| 1386 | + // that the peer sees the client as writing. |
| 1387 | + cc.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 'X' })).addListener(new ChannelFutureListener() { |
| 1388 | + @Override |
| 1389 | + public void operationComplete(ChannelFuture future) throws Exception { |
| 1390 | + writeListenerEntered.countDown(); |
| 1391 | + try { |
| 1392 | + releaseWriteListener.await(); |
| 1393 | + } catch (InterruptedException ignore) { |
| 1394 | + Thread.currentThread().interrupt(); |
| 1395 | + } |
| 1396 | + } |
| 1397 | + }); |
| 1398 | + assertTrue(writeListenerEntered.await(5, SECONDS)); |
| 1399 | + |
| 1400 | + // Write and close from the peer while the client is still writing, then unblock the client. |
| 1401 | + child.eventLoop().submit(new Runnable() { |
| 1402 | + @Override |
| 1403 | + public void run() { |
| 1404 | + child.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 'P' })); |
| 1405 | + child.close(); |
| 1406 | + } |
| 1407 | + }).sync(); |
| 1408 | + releaseWriteListener.countDown(); |
| 1409 | + |
| 1410 | + assertTrue(clientInactive.await(5, SECONDS)); |
| 1411 | + assertEquals(Boolean.TRUE, receivedBeforeInactive.get(), |
| 1412 | + "Data written by the peer before it closed must not be dropped"); |
| 1413 | + } finally { |
| 1414 | + releaseWriteListener.countDown(); |
| 1415 | + closeChannel(cc); |
| 1416 | + closeChannel(sc); |
| 1417 | + } |
| 1418 | + } |
| 1419 | + |
1333 | 1420 | @Test |
1334 | 1421 | public void testClientCloseWhileConnectionIsQueued() throws Exception { |
1335 | 1422 | BlockingQueue<LocalChannel> accepted = new LinkedBlockingQueue<>(); |
|