|
35 | 35 | import org.junit.jupiter.api.Test; |
36 | 36 | import org.junit.jupiter.api.function.Executable; |
37 | 37 |
|
| 38 | +import java.util.concurrent.CountDownLatch; |
38 | 39 | import java.util.concurrent.ExecutionException; |
39 | 40 | import java.util.concurrent.TimeUnit; |
40 | 41 | import java.util.concurrent.TimeoutException; |
| 42 | +import java.util.concurrent.atomic.AtomicReference; |
41 | 43 |
|
42 | 44 | import static io.netty.channel.pool.ChannelPoolTestUtils.getLocalAddrId; |
43 | 45 |
|
@@ -92,6 +94,140 @@ public void testAcquire() throws Exception { |
92 | 94 | pool.close(); |
93 | 95 | } |
94 | 96 |
|
| 97 | + @Test |
| 98 | + public void testCancelledAcquireReleasesChannel() throws Exception { |
| 99 | + EventLoopGroup localGroup = new DefaultEventLoopGroup(1); |
| 100 | + Tuple t = bootstrap(localGroup); |
| 101 | + BlockingChannelPoolHandler handler = new BlockingChannelPoolHandler(); |
| 102 | + FixedChannelPool pool = null; |
| 103 | + Channel sc = null; |
| 104 | + try { |
| 105 | + sc = t.sb.bind(t.address).syncUninterruptibly().channel(); |
| 106 | + pool = new FixedChannelPool(t.cb, handler, 1); |
| 107 | + |
| 108 | + Future<Channel> cancelledAcquire = pool.acquire(); |
| 109 | + assertTrue(handler.awaitChannelAcquired()); |
| 110 | + assertEquals(1, pool.acquiredChannelCount()); |
| 111 | + assertTrue(cancelledAcquire.cancel(false)); |
| 112 | + |
| 113 | + Future<Channel> nextAcquire = pool.acquire(); |
| 114 | + handler.continueAcquire(); |
| 115 | + |
| 116 | + assertTrue(nextAcquire.await(5, TimeUnit.SECONDS)); |
| 117 | + assertTrue(nextAcquire.isSuccess()); |
| 118 | + assertTrue(cancelledAcquire.isCancelled()); |
| 119 | + assertSame(handler.channel(), nextAcquire.getNow()); |
| 120 | + assertEquals(1, pool.acquiredChannelCount()); |
| 121 | + |
| 122 | + pool.release(nextAcquire.getNow()).syncUninterruptibly(); |
| 123 | + assertEquals(0, pool.acquiredChannelCount()); |
| 124 | + pool.close(); |
| 125 | + assertFalse(handler.channel().isOpen()); |
| 126 | + } finally { |
| 127 | + handler.continueAcquire(); |
| 128 | + if (pool != null) { |
| 129 | + pool.close(); |
| 130 | + } |
| 131 | + if (handler.channel() != null) { |
| 132 | + handler.channel().close().syncUninterruptibly(); |
| 133 | + } |
| 134 | + if (sc != null) { |
| 135 | + sc.close().syncUninterruptibly(); |
| 136 | + } |
| 137 | + localGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testCancelledPendingAcquireRunsNextTask() throws Exception { |
| 143 | + EventLoopGroup localGroup = new DefaultEventLoopGroup(1); |
| 144 | + Tuple t = bootstrap(localGroup); |
| 145 | + FixedChannelPool pool = null; |
| 146 | + Channel sc = null; |
| 147 | + Channel channel = null; |
| 148 | + try { |
| 149 | + sc = t.sb.bind(t.address).syncUninterruptibly().channel(); |
| 150 | + pool = new FixedChannelPool(t.cb, new TestChannelPoolHandler(), 1); |
| 151 | + channel = pool.acquire().syncUninterruptibly().getNow(); |
| 152 | + |
| 153 | + Future<Channel> cancelledAcquire = pool.acquire(); |
| 154 | + localGroup.submit(new Runnable() { |
| 155 | + @Override |
| 156 | + public void run() { |
| 157 | + // NOOP |
| 158 | + } |
| 159 | + }).syncUninterruptibly(); |
| 160 | + assertFalse(cancelledAcquire.isDone()); |
| 161 | + assertTrue(cancelledAcquire.cancel(false)); |
| 162 | + |
| 163 | + Future<Channel> nextAcquire = pool.acquire(); |
| 164 | + localGroup.submit(new Runnable() { |
| 165 | + @Override |
| 166 | + public void run() { |
| 167 | + // NOOP |
| 168 | + } |
| 169 | + }).syncUninterruptibly(); |
| 170 | + assertFalse(nextAcquire.isDone()); |
| 171 | + |
| 172 | + pool.release(channel).syncUninterruptibly(); |
| 173 | + assertTrue(nextAcquire.await(5, TimeUnit.SECONDS)); |
| 174 | + assertTrue(nextAcquire.isSuccess()); |
| 175 | + assertSame(channel, nextAcquire.getNow()); |
| 176 | + assertEquals(1, pool.acquiredChannelCount()); |
| 177 | + |
| 178 | + pool.release(nextAcquire.getNow()).syncUninterruptibly(); |
| 179 | + assertEquals(0, pool.acquiredChannelCount()); |
| 180 | + } finally { |
| 181 | + if (pool != null) { |
| 182 | + pool.close(); |
| 183 | + } |
| 184 | + if (channel != null) { |
| 185 | + channel.close().syncUninterruptibly(); |
| 186 | + } |
| 187 | + if (sc != null) { |
| 188 | + sc.close().syncUninterruptibly(); |
| 189 | + } |
| 190 | + localGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void testCancelledAcquireChannelClosedWhenPoolCloses() throws Exception { |
| 196 | + EventLoopGroup localGroup = new DefaultEventLoopGroup(1); |
| 197 | + Tuple t = bootstrap(localGroup); |
| 198 | + BlockingChannelPoolHandler handler = new BlockingChannelPoolHandler(); |
| 199 | + FixedChannelPool pool = null; |
| 200 | + Channel sc = null; |
| 201 | + try { |
| 202 | + sc = t.sb.bind(t.address).syncUninterruptibly().channel(); |
| 203 | + pool = new FixedChannelPool(t.cb, handler, 1); |
| 204 | + |
| 205 | + Future<Channel> cancelledAcquire = pool.acquire(); |
| 206 | + assertTrue(handler.awaitChannelAcquired()); |
| 207 | + assertTrue(cancelledAcquire.cancel(false)); |
| 208 | + Future<Void> closeFuture = pool.closeAsync(); |
| 209 | + handler.continueAcquire(); |
| 210 | + |
| 211 | + assertTrue(closeFuture.await(5, TimeUnit.SECONDS)); |
| 212 | + assertTrue(closeFuture.isSuccess()); |
| 213 | + assertTrue(handler.channel().closeFuture().await(5, TimeUnit.SECONDS)); |
| 214 | + assertFalse(handler.channel().isOpen()); |
| 215 | + assertEquals(0, pool.acquiredChannelCount()); |
| 216 | + } finally { |
| 217 | + handler.continueAcquire(); |
| 218 | + if (pool != null) { |
| 219 | + pool.close(); |
| 220 | + } |
| 221 | + if (handler.channel() != null) { |
| 222 | + handler.channel().close().syncUninterruptibly(); |
| 223 | + } |
| 224 | + if (sc != null) { |
| 225 | + sc.close().syncUninterruptibly(); |
| 226 | + } |
| 227 | + localGroup.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 228 | + } |
| 229 | + } |
| 230 | + |
95 | 231 | @Test |
96 | 232 | public void testAcquireTimeout() throws Exception { |
97 | 233 | testAcquireTimeout(500); |
@@ -315,13 +451,17 @@ public void channelCreated(Channel ch) { |
315 | 451 | } |
316 | 452 |
|
317 | 453 | private Tuple bootstrap() { |
| 454 | + return bootstrap(group); |
| 455 | + } |
| 456 | + |
| 457 | + private Tuple bootstrap(EventLoopGroup eventLoopGroup) { |
318 | 458 | LocalAddress addr = new LocalAddress(getLocalAddrId()); |
319 | 459 | Bootstrap cb = new Bootstrap(); |
320 | 460 | cb.remoteAddress(addr); |
321 | | - cb.group(group).channel(LocalChannel.class); |
| 461 | + cb.group(eventLoopGroup).channel(LocalChannel.class); |
322 | 462 |
|
323 | 463 | ServerBootstrap sb = new ServerBootstrap(); |
324 | | - sb.group(group) |
| 464 | + sb.group(eventLoopGroup) |
325 | 465 | .channel(LocalServerChannel.class) |
326 | 466 | .childHandler(new ChannelInitializer<LocalChannel>() { |
327 | 467 | @Override |
@@ -333,6 +473,39 @@ public void initChannel(LocalChannel ch) { |
333 | 473 | return new Tuple(addr, cb, sb); |
334 | 474 | } |
335 | 475 |
|
| 476 | + private static final class BlockingChannelPoolHandler extends AbstractChannelPoolHandler { |
| 477 | + private final CountDownLatch channelAcquired = new CountDownLatch(1); |
| 478 | + private final CountDownLatch continueAcquire = new CountDownLatch(1); |
| 479 | + private final AtomicReference<Channel> channel = new AtomicReference<Channel>(); |
| 480 | + |
| 481 | + @Override |
| 482 | + public void channelCreated(Channel ch) { |
| 483 | + // NOOP |
| 484 | + } |
| 485 | + |
| 486 | + @Override |
| 487 | + public void channelAcquired(Channel ch) throws Exception { |
| 488 | + if (channel.compareAndSet(null, ch)) { |
| 489 | + channelAcquired.countDown(); |
| 490 | + if (!continueAcquire.await(5, TimeUnit.SECONDS)) { |
| 491 | + throw new TimeoutException("Timed out waiting to continue acquire"); |
| 492 | + } |
| 493 | + } |
| 494 | + } |
| 495 | + |
| 496 | + boolean awaitChannelAcquired() throws InterruptedException { |
| 497 | + return channelAcquired.await(5, TimeUnit.SECONDS); |
| 498 | + } |
| 499 | + |
| 500 | + void continueAcquire() { |
| 501 | + continueAcquire.countDown(); |
| 502 | + } |
| 503 | + |
| 504 | + Channel channel() { |
| 505 | + return channel.get(); |
| 506 | + } |
| 507 | + } |
| 508 | + |
336 | 509 | private static final class TestChannelPoolHandler extends AbstractChannelPoolHandler { |
337 | 510 | @Override |
338 | 511 | public void channelCreated(Channel ch) throws Exception { |
|