1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.pool2.pool407;
19
20 import java.time.Duration;
21 import java.util.concurrent.ExecutorService;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.TimeUnit;
24
25 import org.apache.commons.pool2.PooledObject;
26 import org.junit.jupiter.api.Disabled;
27 import org.junit.jupiter.api.Test;
28
29
30
31
32 public class Pool407Test extends AbstractPool407Test {
33
34
35
36
37 private static class Pool407RoundtripRunnable implements Runnable {
38 private final Pool407 pool;
39
40 public Pool407RoundtripRunnable(final Pool407 pool) {
41 this.pool = pool;
42 }
43
44 @Override
45 public void run() {
46 try {
47 final Pool407Fixture object = pool.borrowObject();
48 if (object != null) {
49 pool.returnObject(object);
50 }
51 } catch (final Exception e) {
52 throw new RuntimeException(e);
53 }
54 }
55 }
56
57 protected void assertShutdown(final ExecutorService executor, final Duration poolConfigMaxWait, final AbstractPool407Factory factory) throws Exception {
58
59
60
61 executor.shutdown();
62 final boolean termination = executor.awaitTermination(Pool407Constants.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
63
64
65 final Pool407Fixture obj = factory.create();
66
67 final PooledObject<Pool407Fixture> pooledObject = obj != null ? factory.makeObject() : null;
68 assertShutdown(termination, poolConfigMaxWait, obj, pooledObject);
69 }
70
71 private void test(final AbstractPool407Factory factory, final int poolSize, final Duration poolConfigMaxWait) throws Exception {
72 final ExecutorService executor = Executors.newFixedThreadPool(poolSize);
73 try (final Pool407 pool = new Pool407(factory, poolConfigMaxWait)) {
74
75 for (int i = 0; i < poolSize; i++) {
76 executor.execute(new Pool407RoundtripRunnable(pool));
77 }
78 assertShutdown(executor, poolConfigMaxWait, factory);
79 }
80 }
81
82 @Test
83 public void testNormalFactoryNonNullFixtureWaitMax() throws Exception {
84 test(new Pool407NormalFactory(new Pool407Fixture()), Pool407Constants.POOL_SIZE, Pool407Constants.WAIT_FOREVER);
85 }
86
87 @Test
88 @Disabled
89 public void testNormalFactoryNullFixtureWaitMax() throws Exception {
90 test(new Pool407NormalFactory(null), Pool407Constants.POOL_SIZE, Pool407Constants.WAIT_FOREVER);
91 }
92
93 @Disabled
94 @Test
95 public void testNullObjectFactoryWaitMax() throws Exception {
96 test(new Pool407NullObjectFactory(), Pool407Constants.POOL_SIZE, Pool407Constants.WAIT_FOREVER);
97 }
98
99 @Test
100 @Disabled
101 public void testNullObjectFactoryWaitShort() throws Exception {
102 test(new Pool407NullObjectFactory(), Pool407Constants.POOL_SIZE, Pool407Constants.WAIT_SHORT);
103 }
104
105 @Test
106 @Disabled
107 public void testNullPoolableFactoryWaitMax() throws Exception {
108 test(new Pool407NullPoolableObjectFactory(), Pool407Constants.POOL_SIZE, Pool407Constants.WAIT_FOREVER);
109 }
110
111 @Test
112 @Disabled
113 public void testNullPoolableFactoryWaitShort() throws Exception {
114 test(new Pool407NullPoolableObjectFactory(), Pool407Constants.POOL_SIZE, Pool407Constants.WAIT_SHORT);
115 }
116 }