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    *      https://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;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  
23  import java.time.Duration;
24  
25  import org.apache.commons.pool2.impl.DefaultPooledObject;
26  import org.apache.commons.pool2.impl.GenericObjectPool;
27  import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
28  import org.junit.jupiter.api.Test;
29  
30  class PoolTest {
31  
32      private static final class Foo {
33      }
34  
35      private static final class PooledFooFactory implements PooledObjectFactory<Foo> {
36          private static final long VALIDATION_WAIT_IN_MILLIS = 1000;
37  
38          @Override
39          public void activateObject(final PooledObject<Foo> pooledObject) {
40          }
41  
42          @Override
43          public void destroyObject(final PooledObject<Foo> pooledObject) {
44          }
45  
46          @Override
47          public PooledObject<Foo> makeObject() {
48              return new DefaultPooledObject<>(new Foo());
49          }
50  
51          @Override
52          public void passivateObject(final PooledObject<Foo> pooledObject) {
53          }
54  
55          @Override
56          public boolean validateObject(final PooledObject<Foo> pooledObject) {
57              try {
58                  Thread.sleep(VALIDATION_WAIT_IN_MILLIS);
59              } catch (final InterruptedException e) {
60                  Thread.interrupted();
61              }
62              return false;
63          }
64      }
65  
66      private static final CharSequence COMMONS_POOL_EVICTIONS_TIMER_THREAD_NAME = "commons-pool-EvictionTimer";
67  
68      private static final long EVICTION_PERIOD_IN_MILLIS = 100;
69  
70      @Test
71      void testPool() throws Exception {
72          final GenericObjectPoolConfig<Foo> poolConfig = new GenericObjectPoolConfig<>();
73          poolConfig.setTestWhileIdle(true /* testWhileIdle */);
74          final PooledFooFactory pooledFooFactory = new PooledFooFactory();
75          try (GenericObjectPool<Foo> pool = new GenericObjectPool<>(pooledFooFactory, poolConfig)) {
76              pool.setTimeBetweenEvictionRunsMillis(EVICTION_PERIOD_IN_MILLIS);
77              assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getDurationBetweenEvictionRuns().toMillis());
78              assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getTimeBetweenEvictionRuns().toMillis());
79              pool.setDurationBetweenEvictionRuns(Duration.ofMillis(EVICTION_PERIOD_IN_MILLIS));
80              assertEquals(EVICTION_PERIOD_IN_MILLIS, pool.getTimeBetweenEvictionRuns().toMillis());
81              pool.addObject();
82              try {
83                  Thread.sleep(EVICTION_PERIOD_IN_MILLIS);
84              } catch (final InterruptedException e) {
85                  Thread.interrupted();
86              }
87          }
88          final Thread[] threads = new Thread[Thread.activeCount()];
89          Thread.enumerate(threads);
90          for (final Thread thread : threads) {
91              if (thread == null) {
92                  continue;
93              }
94              final String name = thread.getName();
95              assertFalse(name.contains(COMMONS_POOL_EVICTIONS_TIMER_THREAD_NAME), name);
96          }
97      }
98  }