|
24 | 24 | import io.netty.channel.EventLoopGroup; |
25 | 25 | import io.netty.channel.local.LocalAddress; |
26 | 26 | import io.netty.channel.local.LocalChannel; |
| 27 | +import io.netty.channel.local.LocalEventLoopGroup; |
27 | 28 | import io.netty.channel.local.LocalServerChannel; |
28 | 29 | import io.netty.util.concurrent.Future; |
| 30 | +import io.netty.util.concurrent.Promise; |
29 | 31 | import org.junit.jupiter.api.Test; |
30 | 32 | import org.junit.jupiter.api.function.Executable; |
31 | 33 |
|
32 | 34 | import java.util.Queue; |
| 35 | +import java.util.concurrent.BlockingQueue; |
33 | 36 | import java.util.concurrent.LinkedBlockingQueue; |
34 | 37 | import java.util.concurrent.TimeUnit; |
35 | 38 | import java.util.concurrent.atomic.AtomicBoolean; |
@@ -457,4 +460,86 @@ public void channelCreated(Channel ch) { |
457 | 460 | pool.close(); |
458 | 461 | group.shutdownGracefully(); |
459 | 462 | } |
| 463 | + |
| 464 | + @Test |
| 465 | + public void testAcquireCancelledDuringHealthCheckOffersChannelBack() throws Exception { |
| 466 | + EventLoopGroup group = new LocalEventLoopGroup(1); |
| 467 | + LocalAddress addr = new LocalAddress(getLocalAddrId()); |
| 468 | + Bootstrap cb = new Bootstrap() |
| 469 | + .group(group) |
| 470 | + .channel(LocalChannel.class) |
| 471 | + .remoteAddress(addr); |
| 472 | + ServerBootstrap sb = new ServerBootstrap() |
| 473 | + .group(group) |
| 474 | + .channel(LocalServerChannel.class) |
| 475 | + .childHandler(new ChannelInitializer<LocalChannel>() { |
| 476 | + @Override |
| 477 | + public void initChannel(LocalChannel ch) throws Exception { |
| 478 | + ch.pipeline().addLast(new ChannelInboundHandlerAdapter()); |
| 479 | + } |
| 480 | + }); |
| 481 | + Channel sc = sb.bind(addr).sync().channel(); |
| 482 | + CountingChannelPoolHandler handler = new CountingChannelPoolHandler(); |
| 483 | + |
| 484 | + // Hand out health checks that we complete ourselves, so the acquire Promise can be cancelled while the |
| 485 | + // health check of an already pooled Channel is still pending. |
| 486 | + final BlockingQueue<Promise<Boolean>> healthChecks = new LinkedBlockingQueue<Promise<Boolean>>(); |
| 487 | + ChannelHealthChecker healthChecker = new ChannelHealthChecker() { |
| 488 | + @Override |
| 489 | + public Future<Boolean> isHealthy(Channel channel) { |
| 490 | + Promise<Boolean> healthCheck = channel.eventLoop().newPromise(); |
| 491 | + healthChecks.add(healthCheck); |
| 492 | + return healthCheck; |
| 493 | + } |
| 494 | + }; |
| 495 | + // Health checks on release are turned off, so releasing does not need a hand-completed health check as well. |
| 496 | + SimpleChannelPool pool = new SimpleChannelPool(cb, handler, healthChecker, false); |
| 497 | + |
| 498 | + Channel channel = null; |
| 499 | + try { |
| 500 | + // Nothing is pooled yet, so this connects a new Channel and does not run a health check at all. |
| 501 | + channel = pool.acquire().sync().getNow(); |
| 502 | + pool.release(channel).sync(); |
| 503 | + assertTrue(healthChecks.isEmpty()); |
| 504 | + assertEquals(1, handler.acquiredCount()); |
| 505 | + assertEquals(1, handler.releasedCount()); |
| 506 | + |
| 507 | + // This acquire reuses the pooled Channel, so it has to pass the health check first. |
| 508 | + Promise<Channel> acquirePromise = group.next().newPromise(); |
| 509 | + pool.acquire(acquirePromise); |
| 510 | + Promise<Boolean> healthCheck = healthChecks.poll(5, TimeUnit.SECONDS); |
| 511 | + assertNotNull(healthCheck); |
| 512 | + |
| 513 | + assertTrue(acquirePromise.cancel(false)); |
| 514 | + healthCheck.setSuccess(Boolean.TRUE); |
| 515 | + |
| 516 | + // Let the health check listener run on the EventLoop of the Channel. |
| 517 | + channel.eventLoop().submit(new Runnable() { |
| 518 | + @Override |
| 519 | + public void run() { |
| 520 | + // NOOP. |
| 521 | + } |
| 522 | + }).sync(); |
| 523 | + |
| 524 | + assertTrue(acquirePromise.isCancelled()); |
| 525 | + assertEquals(2, handler.acquiredCount()); |
| 526 | + assertEquals(2, handler.releasedCount()); |
| 527 | + assertTrue(channel.isActive()); |
| 528 | + |
| 529 | + // The Channel went back into the pool, so the next acquire hands out the very same Channel. |
| 530 | + Promise<Channel> secondAcquirePromise = group.next().newPromise(); |
| 531 | + pool.acquire(secondAcquirePromise); |
| 532 | + Promise<Boolean> secondHealthCheck = healthChecks.poll(5, TimeUnit.SECONDS); |
| 533 | + assertNotNull(secondHealthCheck); |
| 534 | + secondHealthCheck.setSuccess(Boolean.TRUE); |
| 535 | + assertSame(channel, secondAcquirePromise.sync().getNow()); |
| 536 | + } finally { |
| 537 | + if (channel != null) { |
| 538 | + channel.close().sync(); |
| 539 | + } |
| 540 | + sc.close().sync(); |
| 541 | + pool.close(); |
| 542 | + group.shutdownGracefully(); |
| 543 | + } |
| 544 | + } |
460 | 545 | } |