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  package org.apache.commons.pool2.impl;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.lang.reflect.Field;
25  import java.util.concurrent.ScheduledFuture;
26  import java.util.concurrent.ThreadPoolExecutor;
27  
28  import org.apache.commons.pool2.BasePooledObjectFactory;
29  import org.apache.commons.pool2.PooledObject;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests for {@link EvictionTimer}.
34   */
35  class TestEvictionTimer {
36  
37      @Test
38      void testStartStopEvictionTimer() throws Exception {
39  
40          try (GenericObjectPool<String> pool = new GenericObjectPool<>(new BasePooledObjectFactory<String>() {
41  
42              @Override
43              public String create() {
44                  return null;
45              }
46  
47              @Override
48              public PooledObject<String> wrap(final String obj) {
49                  return new DefaultPooledObject<>(obj);
50              }
51          })) {
52  
53              // Start evictor #1
54              final BaseGenericObjectPool<String>.Evictor evictor1 = pool.new Evictor();
55              EvictionTimer.schedule(evictor1, TestConstants.ONE_MINUTE_DURATION, TestConstants.ONE_MINUTE_DURATION);
56  
57              // Assert that eviction objects are correctly allocated
58              // 1 - the evictor timer task is created
59              final Field evictorTaskFutureField =
60                      evictor1.getClass().getDeclaredField("scheduledFuture");
61              evictorTaskFutureField.setAccessible(true);
62              ScheduledFuture<?> sf = (ScheduledFuture<?>) evictorTaskFutureField.get(evictor1);
63              assertFalse(sf.isCancelled());
64              // 2- and, the eviction action is added to executor thread pool
65              final Field evictorExecutorField = EvictionTimer.class.getDeclaredField("executor");
66              evictorExecutorField.setAccessible(true);
67              final ThreadPoolExecutor evictionExecutor = (ThreadPoolExecutor) evictorExecutorField.get(null);
68              assertEquals(2, evictionExecutor.getQueue().size()); // Reaper plus one eviction task
69              assertEquals(1, EvictionTimer.getNumTasks());
70  
71              // Start evictor #2
72              final BaseGenericObjectPool<String>.Evictor evictor2 = pool.new Evictor();
73              EvictionTimer.schedule(evictor2, TestConstants.ONE_MINUTE_DURATION, TestConstants.ONE_MINUTE_DURATION);
74  
75              // Assert that eviction objects are correctly allocated
76              // 1 - the evictor timer task is created
77              sf = (ScheduledFuture<?>) evictorTaskFutureField.get(evictor2);
78              assertFalse(sf.isCancelled());
79              // 2- and, the eviction action is added to executor thread pool
80              assertEquals(3, evictionExecutor.getQueue().size()); // Reaper plus 2 eviction tasks
81              assertEquals(2, EvictionTimer.getNumTasks());
82  
83              // Stop evictor #1
84              EvictionTimer.cancel(evictor1, BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT, false);
85  
86              // Assert that eviction objects are correctly cleaned
87              // 1 - the evictor timer task is canceled
88              sf = (ScheduledFuture<?>) evictorTaskFutureField.get(evictor1);
89              assertTrue(sf.isCancelled());
90              // 2- and, the eviction action is removed from executor thread pool
91              final ThreadPoolExecutor evictionExecutorOnStop = (ThreadPoolExecutor) evictorExecutorField.get(null);
92              assertEquals(2, evictionExecutorOnStop.getQueue().size());
93              assertEquals(1, EvictionTimer.getNumTasks());
94  
95              // Stop evictor #2
96              EvictionTimer.cancel(evictor2, BaseObjectPoolConfig.DEFAULT_EVICTOR_SHUTDOWN_TIMEOUT, false);
97  
98              // Assert that eviction objects are correctly cleaned
99              // 1 - the evictor timer task is canceled
100             sf = (ScheduledFuture<?>) evictorTaskFutureField.get(evictor2);
101             assertTrue(sf.isCancelled());
102             // 2- and, the eviction thread pool executor is freed
103             assertNull(evictorExecutorField.get(null));
104             assertEquals(0, EvictionTimer.getNumTasks());
105         }
106     }
107 }