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  package org.apache.commons.pool2.impl;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.text.SimpleDateFormat;
23  import java.util.Set;
24  
25  import org.apache.commons.pool2.impl.TestGenericObjectPool.SimpleFactory;
26  import org.junit.jupiter.api.Test;
27  
28  public class TestDefaultPooledObjectInfo {
29  
30      @Test
31      public void testGetLastBorrowTrace() throws Exception {
32          final AbandonedConfig abandonedConfig = new AbandonedConfig();
33  
34          abandonedConfig.setRemoveAbandonedOnBorrow(true);
35          abandonedConfig.setRemoveAbandonedTimeout(TestConstants.ONE_SECOND_DURATION);
36          abandonedConfig.setLogAbandoned(true);
37          try (final GenericObjectPool<String> pool = new GenericObjectPool<>(new SimpleFactory(),
38                  new GenericObjectPoolConfig<>(), abandonedConfig)) {
39  
40              pool.borrowObject();
41              // pool.returnObject(s1); // Object not returned, causes abandoned object created exception
42  
43              final Set<DefaultPooledObjectInfo> strings = pool.listAllObjects();
44              final DefaultPooledObjectInfo s1Info = strings.iterator().next();
45              final String lastBorrowTrace = s1Info.getLastBorrowTrace();
46  
47              assertTrue(lastBorrowTrace.startsWith("Pooled object created"));
48          }
49      }
50  
51      @Test
52      public void testGetPooledObjectToString() throws Exception {
53          try (final GenericObjectPool<String> pool = new GenericObjectPool<>(new SimpleFactory())) {
54  
55              final String s1 = pool.borrowObject();
56  
57              final Set<DefaultPooledObjectInfo> strings = pool.listAllObjects();
58  
59              assertEquals(1, strings.size());
60  
61              final DefaultPooledObjectInfo s1Info = strings.iterator().next();
62  
63              assertEquals(s1, s1Info.getPooledObjectToString());
64          }
65      }
66  
67      @Test
68      public void testGetPooledObjectType() throws Exception {
69          try (final GenericObjectPool<String> pool = new GenericObjectPool<>(new SimpleFactory())) {
70  
71              pool.borrowObject();
72  
73              final Set<DefaultPooledObjectInfo> strings = pool.listAllObjects();
74  
75              assertEquals(1, strings.size());
76  
77              final DefaultPooledObjectInfo s1Info = strings.iterator().next();
78  
79              assertEquals(String.class.getName(), s1Info.getPooledObjectType());
80          }
81      }
82  
83      @Test
84      public void testTiming() throws Exception {
85          try (final GenericObjectPool<String> pool = new GenericObjectPool<>(new SimpleFactory())) {
86  
87              final long t1Millis = System.currentTimeMillis();
88  
89              Thread.sleep(50);
90              final String s1 = pool.borrowObject();
91              Thread.sleep(50);
92  
93              final long t2Millis = System.currentTimeMillis();
94  
95              Thread.sleep(50);
96              pool.returnObject(s1);
97              Thread.sleep(50);
98  
99              final long t3Millis = System.currentTimeMillis();
100 
101             Thread.sleep(50);
102             pool.borrowObject();
103             Thread.sleep(50);
104 
105             final long t4Millis = System.currentTimeMillis();
106 
107             final Set<DefaultPooledObjectInfo> strings = pool.listAllObjects();
108 
109             assertEquals(1, strings.size());
110 
111             final DefaultPooledObjectInfo s1Info = strings.iterator().next();
112 
113             final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
114 
115             assertTrue(s1Info.getCreateTime() > t1Millis);
116             assertEquals(sdf.format(Long.valueOf(s1Info.getCreateTime())), s1Info.getCreateTimeFormatted());
117             assertTrue(s1Info.getCreateTime() < t2Millis);
118 
119             assertTrue(s1Info.getLastReturnTime() > t2Millis);
120             assertEquals(sdf.format(Long.valueOf(s1Info.getLastReturnTime())),
121                     s1Info.getLastReturnTimeFormatted());
122             assertTrue(s1Info.getLastReturnTime() < t3Millis);
123 
124             assertTrue(s1Info.getLastBorrowTime() > t3Millis);
125             assertEquals(sdf.format(Long.valueOf(s1Info.getLastBorrowTime())),
126                     s1Info.getLastBorrowTimeFormatted());
127             assertTrue(s1Info.getLastBorrowTime() < t4Millis);
128         }
129     }
130 }