View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests POOL-407.
31   */
32  public class Pool407Test extends AbstractPool407Test {
33  
34      /**
35       * Borrows from a pool and then immediately returns to that a pool.
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          // Old note: This never finishes when the factory makes nulls because two threads are stuck forever
59          // If a factory always returns a null object or a null poolable object, then we will wait forever.
60          // This would also be true is object validation always fails.
61          executor.shutdown();
62          final boolean termination = executor.awaitTermination(Pool407Constants.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
63          // Order matters: test create() before makeObject()
64          // Calling create() here in this test should not have side-effects
65          final Pool407Fixture obj = factory.create();
66          // Calling makeObject() here in this test should not have side-effects
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              // Start 'poolSize' threads that try to borrow a Pool407Fixture with the same key
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 }