8000 New tests for channel pool: release with health check and processing … · netty/netty@b4d8ae3 · GitHub
[go: up one dir, main page]

Skip to content

Commit b4d8ae3

Browse files
New tests for channel pool: release with health check and processing order during to acquire (#15298)
New tests for channel pool: release with health check and processing order during to acquire Motivation: While working on #14723 it was requested to create some tests. Because #14723 will introduce deprecation in some constructors and change the way to retrieve the channel it is better have the tests before that change is introduced. This ensure that the current behavior ( now tested ) will continue working. Modification: Introcue two new tests Result: All tests passed --------- Co-authored-by: Chris Vest <christianvest_hansen@apple.com>
1 parent 182f154 commit b4d8ae3

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

transport/src/test/java/io/netty/channel/pool/FixedChannelPoolTest.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
import org.junit.jupiter.api.BeforeAll;
3535
import org.junit.jupiter.api.Test;
3636
import org.junit.jupiter.api.function.Executable;
37+
import org.junit.jupiter.params.ParameterizedTest;
38+
import org.junit.jupiter.params.provider.ValueSource;
3739

40+
import java.util.ArrayList;
41+
import java.util.List;
3842
import java.util.concurrent.ExecutionException;
3943
import java.util.concurrent.TimeUnit;
4044
import java.util.concurrent.TimeoutException;
@@ -314,6 +318,96 @@ public void channelCreated(Channel ch) {
314318
pool.close();
315319
}
316320

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+
317411
private Tuple bootstrap() {
318412
LocalAddress addr = new LocalAddress(getLocalAddrId());
319413
Bootstrap cb = new Bootstrap();
@@ -352,4 +446,24 @@ private static final class Tuple {
352446
this.sb = sb;
353447
}
354448
}
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+
}
355469
}

0 commit comments

Comments
 (0)
0