|
39 | 39 |
|
40 | 40 | import java.util.ArrayList; |
41 | 41 | import java.util.List; |
| 42 | +import java.util.concurrent.CountDownLatch; |
42 | 43 | import java.util.concurrent.ExecutionException; |
43 | 44 | import java.util.concurrent.TimeUnit; |
44 | 45 | import java.util.concurrent.TimeoutException; |
| 46 | +import java.util.concurrent.atomic.AtomicReference; |
45 | 47 |
|
46 | 48 | import static io.netty.channel.pool.ChannelPoolTestUtils.getLocalAddrId; |
47 | 49 |
|
@@ -96,6 +98,130 @@ public void testAcquire() throws Exception { |
96 | 98 | pool.close(); |
97 | 99 | } |
98 | 100 |
|
| 101 | + @Test |
| 102 | + public void testCancelledAcquireReleasesChannel() throws Exception { |
| 103 | + EventLoopGroup localGroup = new MultiThreadIoEventLoopGroup(1, LocalIoHandler.newFactory()); |
| 104 | + Tuple t = bootstrap(localGroup); |
| 105 | + BlockingChannelPoolHandler handler = new BlockingChannelPoolHandler(); |
| 106 | + FixedChannelPool pool = null; |
| 107 | + Channel sc = null; |
| 108 | + try { |
| 109 | + sc = t.sb.bind(t.address).syncUninterruptibly().channel(); |
| 110 | + pool = new FixedChannelPool(t.cb, handler, 1); |
| 111 | + |
| 112 | + Future<Channel> cancelledAcquire = pool.acquire(); |
| 113 | + assertTrue(handler.awaitChannelAcquired()); |
| 114 | + assertEquals(1, pool.acquiredChannelCount()); |
| 115 | + assertTrue(cancelledAcquire.cancel(false)); |
| 116 | + |
| 117 | + Future<Channel> nextAcquire = pool.acquire(); |
| 118 | + handler.continueAcquire(); |
| 119 | + |
| 120 | + assertTrue(nextAcquire.await(5, TimeUnit.SECONDS)); |
| 121 | + assertTrue(nextAcquire.isSuccess()); |
| 122 | + assertTrue(cancelledAcquire.isCancelled()); |
| 123 | + assertSame(handler.channel(), nextAcquire.getNow()); |
| 124 | + assertEquals(1, pool.acquiredChannelCount()); |
| 125 | + |
| 126 | + pool.release(nextAcquire.getNow()).syncUninterruptibly(); |
| 127 | + assertEquals(0, pool.acquiredChannelCount()); |
| 128 | + pool.close(); |
| 129 | + assertFalse(handler.channel().isOpen()); |
| 130 | + } finally { |
| 131 | + handler.continueAcquire(); |
| 132 | + if (pool != null) { |
| 133 | + pool.close(); |
| 134 | + } |
| 135 | + if (handler.channel() != null) { |
| 136 | + handler.channel().close().syncUninterruptibly(); |
| 137 | + } |
| 138 | + if (sc != null) { |
| 139 | + sc.close().syncUninterruptibly(); |
| 140 | + } |
| 141 | + localGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testCancelledPendingAcquireRunsNextTask() throws Exception { |
| 147 | + EventLoopGroup localGroup = new MultiThreadIoEventLoopGroup(1, LocalIoHandler.newFactory()); |
| 148 | + Tuple t = bootstrap(localGroup); |
| 149 | + FixedChannelPool pool = null; |
| 150 | + Channel sc = null; |
| 151 | + Channel channel = null; |
| 152 | + try { |
| 153 | + sc = t.sb.bind(t.address).syncUninterruptibly().channel(); |
| 154 | + pool = new FixedChannelPool(t.cb, new TestChannelPoolHandler(), 1); |
| 155 | + channel = pool.acquire().syncUninterruptibly().getNow(); |
| 156 | + |
| 157 | + Future<Channel> cancelledAcquire = pool.acquire(); |
| 158 | + localGroup.submit(() -> { }).syncUninterruptibly(); |
| 159 | + assertFalse(cancelledAcquire.isDone()); |
| 160 | + assertTrue(cancelledAcquire.cancel(false)); |
| 161 | + |
| 162 | + Future<Channel> nextAcquire = pool.acquire(); |
| 163 | + localGroup.submit(() -> { }).syncUninterruptibly(); |
| 164 | + assertFalse(nextAcquire.isDone()); |
| 165 | + |
| 166 | + pool.release(channel).syncUninterruptibly(); |
| 167 | + assertTrue(nextAcquire.await(5, TimeUnit.SECONDS)); |
| 168 | + assertTrue(nextAcquire.isSuccess()); |
| 169 | + assertSame(channel, nextAcquire.getNow()); |
| 170 | + assertEquals(1, pool.acquiredChannelCount()); |
| 171 | + |
| 172 | + pool.release(nextAcquire.getNow()).syncUninterruptibly(); |
| 173 | + assertEquals(0, pool.acquiredChannelCount()); |
| 174 | + } finally { |
| 175 | + if (pool != null) { |
| 176 | + pool.close(); |
| 177 | + } |
| 178 | + if (channel != null) { |
| 179 | + channel.close().syncUninterruptibly(); |
| 180 | + } |
| 181 | + if (sc != null) { |
| 182 | + sc.close().syncUninterruptibly(); |
| 183 | + } |
| 184 | + localGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void testCancelledAcquireChannelClosedWhenPoolCloses() throws Exception { |
| 190 | + EventLoopGroup localGroup = new MultiThreadIoEventLoopGroup(1, LocalIoHandler.newFactory()); |
| 191 | + Tuple t = bootstrap(localGroup); |
| 192 | + BlockingChannelPoolHandler handler = new BlockingChannelPoolHandler(); |
| 193 | + FixedChannelPool pool = null; |
| 194 | + Channel sc = null; |
| 195 | + try { |
| 196 | + sc = t.sb.bind(t.address).syncUninterruptibly().channel(); |
| 197 | + pool = new FixedChannelPool(t.cb, handler, 1); |
| 198 | + |
| 199 | + Future<Channel> cancelledAcquire = pool.acquire(); |
| 200 | + assertTrue(handler.awaitChannelAcquired()); |
| 201 | + assertTrue(cancelledAcquire.cancel(false)); |
| 202 | + Future<Void> closeFuture = pool.closeAsync(); |
| 203 | + handler.continueAcquire(); |
| 204 | + |
| 205 | + assertTrue(closeFuture.await(5, TimeUnit.SECONDS)); |
| 206 | + assertTrue(closeFuture.isSuccess()); |
| 207 | + assertTrue(handler.channel().closeFuture().await(5, TimeUnit.SECONDS)); |
| 208 | + assertFalse(handler.channel().isOpen()); |
| 209 | + assertEquals(0, pool.acquiredChannelCount()); |
| 210 | + } finally { |
| 211 | + handler.continueAcquire(); |
| 212 | + if (pool != null) { |
| 213 | + pool.close(); |
| 214 | + } |
| 215 | + if (handler.channel() != null) { |
| 216 | + handler.channel().close().syncUninterruptibly(); |
| 217 | + } |
| 218 | + if (sc != null) { |
| 219 | + sc.close().syncUninterruptibly(); |
| 220 | + } |
| 221 | + localGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 222 | + } |
| 223 | + } |
| 224 | + |
99 | 225 | @Test |
100 | 226 | public void testAcquireTimeout() throws Exception { |
101 | 227 | testAcquireTimeout(500); |
@@ -406,13 +532,17 @@ public void testChannelProcessingOrder(boolean lastInFirstOutOrdering) { |
406 | 532 | } |
407 | 533 |
|
408 | 534 | private Tuple bootstrap() { |
| 535 | + return bootstrap(group); |
| 536 | + } |
| 537 | + |
| 538 | + private Tuple bootstrap(EventLoopGroup eventLoopGroup) { |
409 | 539 | LocalAddress addr = new LocalAddress(getLocalAddrId()); |
410 | 540 | Bootstrap cb = new Bootstrap(); |
411 | 541 | cb.remoteAddress(addr); |
412 | | - cb.group(group).channel(LocalChannel.class); |
| 542 | + cb.group(eventLoopGroup).channel(LocalChannel.class); |
413 | 543 |
|
414 | 544 | ServerBootstrap sb = new ServerBootstrap(); |
415 | | - sb.group(group) |
| 545 | + sb.group(eventLoopGroup) |
416 | 546 | .channel(LocalServerChannel.class) |
417 | 547 | .childHandler(new ChannelInitializer<LocalChannel>() { |
418 | 548 | @Override |
@@ -424,6 +554,39 @@ public void initChannel(LocalChannel ch) { |
424 | 554 | return new Tuple(addr, cb, sb); |
425 | 555 | } |
426 | 556 |
|
| 557 | + private static final class BlockingChannelPoolHandler extends AbstractChannelPoolHandler { |
| 558 | + private final CountDownLatch channelAcquired = new CountDownLatch(1); |
| 559 | + private final CountDownLatch continueAcquire = new CountDownLatch(1); |
| 560 | + private final AtomicReference<Channel> channel = new AtomicReference<>(); |
| 561 | + |
| 562 | + @Override |
| 563 | + public void channelCreated(Channel ch) { |
| 564 | + // NOOP |
| 565 | + } |
| 566 | + |
| 567 | + @Override |
| 568 | + public void channelAcquired(Channel ch) throws Exception { |
| 569 | + if (channel.compareAndSet(null, ch)) { |
| 570 | + channelAcquired.countDown(); |
| 571 | + if (!continueAcquire.await(5, TimeUnit.SECONDS)) { |
| 572 | + throw new TimeoutException("Timed out waiting to continue acquire"); |
| 573 | + } |
| 574 | + } |
| 575 | + } |
| 576 | + |
| 577 | + boolean awaitChannelAcquired() throws InterruptedException { |
| 578 | + return channelAcquired.await(5, TimeUnit.SECONDS); |
| 579 | + } |
| 580 | + |
| 581 | + void continueAcquire() { |
| 582 | + continueAcquire.countDown(); |
| 583 | + } |
| 584 | + |
| 585 | + Channel channel() { |
| 586 | + return channel.get(); |
| 587 | + } |
| 588 | + } |
| 589 | + |
427 | 590 | private static final class TestChannelPoolHandler extends AbstractChannelPoolHandler { |
428 | 591 | @Override |
429 | 592 | public void channelCreated(Channel ch) throws Exception { |
|