|
34 | 34 | import org.junit.jupiter.api.BeforeAll;
|
35 | 35 | import org.junit.jupiter.api.Test;
|
36 | 36 | import org.junit.jupiter.api.function.Executable;
|
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.ValueSource; |
37 | 39 |
|
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.List; |
38 | 42 | import java.util.concurrent.ExecutionException;
|
39 | 43 | import java.util.concurrent.TimeUnit;
|
40 | 44 | import java.util.concurrent.TimeoutException;
|
@@ -314,6 +318,96 @@ public void channelCreated(Channel ch) {
|
314 | 318 | pool.close();
|
315 | 319 | }
|
316 | 320 |
|
| 321 | + @Test |
| 322 | + public void testChannelReleaseHealthCheck() { |
| 323 | + Tuple t = bootstrap(); |
| 324 | + |
| 325 | + // Start server |
| 326 | + Channel sc = t.sb.bind(t.address).syncUninterruptibly().channel(); |
| 327 | + ChannelPoolHandler handler = new TestChannelPoolHandler(); |
| 328 | + InnerFixedChannelPool pool = new InnerFixedChannelPool(t.cb, handler, ChannelHealthChecker.ACTIVE, |
| 329 | + AcquireTimeoutAction.NEW, 500, 1, Integer.MAX_VALUE, true); |
| 330 | + |
| 331 | + // releaseHealthCheck=true,channel=open,doHealthCheckOnRelease |
| 332 | + Channel channel = pool.acquire().syncUninterruptibly().getNow(); |
| 333 | + pool.release(channel).syncUninterruptibly().getNow(); |
| 334 | + Channel sameChannel = pool.acquire().syncUninterruptibly().getNow(); |
| 335 | + assertSame(sameChannel, channel); |
| 336 | + |
| 337 | + // releaseHealthCheck=true,channel=close,doHealthCheckOnRelease |
| 338 | + sameChannel.close().syncUninterruptibly(); |
| 339 | + pool.release(channel).syncUninterruptibly().getNow(); |
| 340 | + sameChannel = pool.acquire().syncUninterruptibly().getNow(); |
| 341 | + assertNotSame(sameChannel, channel); |
| 342 | + |
| 343 | + // close all and create a new poll |
| 344 | + sameChannel.close().syncUninterruptibly(); |
| 345 | + channel.close().syncUninterruptibly(); |
| 346 | + pool.close(); |
| 347 | + pool = new InnerFixedChannelPool(t.cb, handler, ChannelHealthChecker.ACTIVE, |
| 348 | + AcquireTimeoutAction.NEW, 500, 1, Integer.MAX_VALUE, false); |
| 349 | + |
| 350 | + // releaseHealthCheck=false,channel=open,releaseAndOffer |
| 351 | + channel = pool.acquire().syncUninterruptibly().getNow(); |
| 352 | + pool.release(channel).syncUninterruptibly().getNow(); |
| 353 | + sameChannel = pool.acquire().syncUninterruptibly().getNow(); |
| 354 | + assertSame(sameChannel, channel); |
| 355 | + |
| 356 | + // releaseHealthCheck=false,channel=close,releaseAndOffer |
| 357 | + sameChannel.close().syncUninterruptibly(); |
| 358 | + pool.release(channel).syncUninterruptibly().getNow(); |
| 359 | + // the acquire will do a heath check.. this is why we call the special method |
| 360 | + sameChannel = pool.pollChannel(); |
| 361 | + assertSame(sameChannel, channel); |
| 362 | + |
| 363 | + sc.close().syncUninterruptibly(); |
| 364 | + sameChannel.close().syncUninterruptibly(); |
| 365 | + channel.close().syncUninterruptibly(); |
| 366 | + pool.close(); |
| 367 | + } |
| 368 | + |
| 369 | + @ParameterizedTest |
| 370 | + @ValueSource(booleans = { true, false }) |
| 371 | + public void testChannelProcessingOrder(boolean lastInFirstOutOrdering) { |
| 372 | + Tuple t = bootstrap(); |
| 373 | + |
| 374 | + // Start server |
| 375 | + Channel sc = t.sb.bind(t.address).syncUninterruptibly().channel(); |
| 376 | + |
| 377 | + FixedChannelPool pool = new FixedChannelPool(t.cb, new TestChannelPoolHandler(), |
| 378 | + ChannelHealthChecker.ACTIVE, AcquireTimeoutAction.NEW, 500, 1, |
| 379 | + Integer.MAX_VALUE, false, lastInFirstOutOrdering); |
| 380 | + |
| 381 | + // create |
| 382 | + int totalChannels = 5; |
| 383 | + List<Channel> channels = new ArrayList<>(); |
| 384 | + for (int i = 0; i < totalChannels; i++) { |
| 385 | + Channel channel = pool.acquire().syncUninterruptibly().getNow(); |
| 386 | + channels.add(channel); |
| 387 | + } |
| 388 | + for (int i = 0; i < totalChannels; i++) { |
| 389 | + pool.release(channels.get(i)).syncUninterruptibly().getNow(); |
| 390 | + } |
| 391 | + |
| 392 | + // test logic |
| 393 | + for (int i = 0; i < totalChannels; i++) { |
| 394 | + Channel channel = pool.acquire().syncUninterruptibly().getNow(); |
| 395 | + if (lastInFirstOutOrdering) { |
| 396 | + assertSame(channel, channels.get(totalChannels - 1 - i)); |
| 397 | + } else { |
| 398 | + assertSame(channel, channels.get(i)); |
| 399 | + } |
| 400 | + } |
| 401 | + |
| 402 | + // close all |
| 403 | + for (int i = 0; i < totalChannels; i++) { |
| 404 | + channels.get(i).close().syncUninterruptibly(); |
| 405 | + pool.release(channels.get(i)).syncUninterruptibly(); |
| 406 | + } |
| 407 | + sc.close().syncUninterruptibly(); |
| 408 | + pool.close(); |
| 409 | + } |
| 410 | + |
317 | 411 | private Tuple bootstrap() {
|
318 | 412 | LocalAddress addr = new LocalAddress(getLocalAddrId());
|
319 | 413 | Bootstrap cb = new Bootstrap();
|
@@ -352,4 +446,24 @@ private static final class Tuple {
|
352 | 446 | this.sb = sb;
|
353 | 447 | }
|
354 | 448 | }
|
| 449 | + |
| 450 | + private static final class InnerFixedChannelPool extends FixedChannelPool { |
| 451 | + |
| 452 | + InnerFixedChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, ChannelHealthChecker healthCheck, |
| 453 | + AcquireTimeoutAction action, long acquireTimeoutMillis, int maxConnections, |
| 454 | + int maxPendingAcquires, boolean releaseHealthCheck) { |
| 455 | + super(bootstrap, handler, healthCheck, action, acquireTimeoutMillis, maxConnections, maxPendingAcquires, |
| 456 | + releaseHealthCheck); |
| 457 | + } |
| 458 | + |
| 459 | + /** |
| 460 | + * The acquire always do a health check. |
| 461 | + * Only for testing purpose. |
| 462 | + * |
| 463 | + * @return channel |
| 464 | + */ |
| 465 | + public Channel pollChannel() { |
| 466 | + return super.pollChannel(); |
| 467 | + } |
| 468 | + } |
355 | 469 | }
|
0 commit comments