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