|
18 | 18 | package org.apache.commons.pool3.impl; |
19 | 19 |
|
20 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
21 | 23 |
|
22 | 24 | import java.lang.management.ManagementFactory; |
23 | 25 | import java.time.Duration; |
24 | 26 | import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.concurrent.CountDownLatch; |
| 29 | +import java.util.concurrent.ExecutorService; |
| 30 | +import java.util.concurrent.Executors; |
| 31 | +import java.util.concurrent.Future; |
25 | 32 | import java.util.concurrent.TimeUnit; |
26 | 33 | import java.util.concurrent.atomic.AtomicInteger; |
27 | 34 |
|
@@ -140,4 +147,167 @@ void testJMXRegistrationLatency() { |
140 | 147 | pools.forEach(GenericObjectPool::close); |
141 | 148 | } |
142 | 149 | } |
| 150 | + |
| 151 | + @Test |
| 152 | + void testCollectDetailedStatisticsDefault() { |
| 153 | + // Test that collectDetailedStatistics defaults to true for backward compatibility |
| 154 | + assertTrue(pool.getCollectDetailedStatistics()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void testCollectDetailedStatisticsConfiguration() { |
| 159 | + // Test configuration through config object |
| 160 | + final GenericObjectPoolConfig<String> config = new GenericObjectPoolConfig<>(); |
| 161 | + config.setCollectDetailedStatistics(false); |
| 162 | + try (GenericObjectPool<String, TestException> testPool = new GenericObjectPool<>(factory, config)) { |
| 163 | + assertFalse(testPool.getCollectDetailedStatistics()); |
| 164 | + } |
| 165 | + // Test runtime configuration |
| 166 | + pool.setCollectDetailedStatistics(false); |
| 167 | + assertFalse(pool.getCollectDetailedStatistics()); |
| 168 | + pool.setCollectDetailedStatistics(true); |
| 169 | + assertTrue(pool.getCollectDetailedStatistics()); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + void testCollectDetailedStatisticsDisabled() throws Exception { |
| 174 | + // Configure pool to disable detailed statistics |
| 175 | + pool.setCollectDetailedStatistics(false); |
| 176 | + final DefaultPooledObject<String> pooledObject = (DefaultPooledObject<String>) factory.makeObject(); |
| 177 | + // Record initial values |
| 178 | + final long initialActiveTime = pool.getMeanActiveTimeMillis(); |
| 179 | + final long initialIdleTime = pool.getMeanIdleDuration().toMillis(); |
| 180 | + final long initialWaitTime = pool.getMeanBorrowWaitTimeMillis(); |
| 181 | + final long initialMaxWaitTime = pool.getMaxBorrowWaitTimeMillis(); |
| 182 | + // Update statistics - should be ignored for detailed stats |
| 183 | + pool.updateStatsBorrow(pooledObject, Duration.ofMillis(100)); |
| 184 | + pool.updateStatsReturn(Duration.ofMillis(200)); |
| 185 | + // Basic counters should still work |
| 186 | + assertEquals(1, pool.getBorrowedCount()); |
| 187 | + assertEquals(1, pool.getReturnedCount()); |
| 188 | + // Detailed statistics should remain unchanged |
| 189 | + assertEquals(initialActiveTime, pool.getMeanActiveTimeMillis()); |
| 190 | + assertEquals(initialIdleTime, pool.getMeanIdleDuration().toMillis()); |
| 191 | + assertEquals(initialWaitTime, pool.getMeanBorrowWaitTimeMillis()); |
| 192 | + assertEquals(initialMaxWaitTime, pool.getMaxBorrowWaitTimeMillis()); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + void testCollectDetailedStatisticsEnabled() throws Exception { |
| 197 | + // Ensure detailed statistics are enabled (default) |
| 198 | + pool.setCollectDetailedStatistics(true); |
| 199 | + final DefaultPooledObject<String> pooledObject = (DefaultPooledObject<String>) factory.makeObject(); |
| 200 | + // Update statistics |
| 201 | + pool.updateStatsBorrow(pooledObject, Duration.ofMillis(100)); |
| 202 | + pool.updateStatsReturn(Duration.ofMillis(200)); |
| 203 | + // All counters should work |
| 204 | + assertEquals(1, pool.getBorrowedCount()); |
| 205 | + assertEquals(1, pool.getReturnedCount()); |
| 206 | + // Detailed statistics should be updated |
| 207 | + assertEquals(200, pool.getMeanActiveTimeMillis()); |
| 208 | + assertEquals(100, pool.getMeanBorrowWaitTimeMillis()); |
| 209 | + assertEquals(100, pool.getMaxBorrowWaitTimeMillis()); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + void testCollectDetailedStatisticsToggling() throws Exception { |
| 214 | + final DefaultPooledObject<String> pooledObject = (DefaultPooledObject<String>) factory.makeObject(); |
| 215 | + // Start with detailed stats enabled |
| 216 | + pool.setCollectDetailedStatistics(true); |
| 217 | + pool.updateStatsBorrow(pooledObject, Duration.ofMillis(50)); |
| 218 | + pool.updateStatsReturn(Duration.ofMillis(100)); |
| 219 | + assertEquals(50, pool.getMeanBorrowWaitTimeMillis()); |
| 220 | + assertEquals(100, pool.getMeanActiveTimeMillis()); |
| 221 | + // Disable detailed stats |
| 222 | + pool.setCollectDetailedStatistics(false); |
| 223 | + pool.updateStatsBorrow(pooledObject, Duration.ofMillis(200)); |
| 224 | + pool.updateStatsReturn(Duration.ofMillis(300)); |
| 225 | + // Detailed stats should remain at previous values |
| 226 | + assertEquals(50, pool.getMeanBorrowWaitTimeMillis()); |
| 227 | + assertEquals(100, pool.getMeanActiveTimeMillis()); |
| 228 | + // Basic counters should continue to increment |
| 229 | + assertEquals(2, pool.getBorrowedCount()); |
| 230 | + assertEquals(2, pool.getReturnedCount()); |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + void testStatsStoreConcurrentAccess() throws Exception { |
| 235 | + // Test the lock-free StatsStore implementation under concurrent load |
| 236 | + final int numThreads = 10; |
| 237 | + final int operationsPerThread = 1000; |
| 238 | + final ExecutorService executor = Executors.newFixedThreadPool(numThreads); |
| 239 | + final CountDownLatch startLatch = new CountDownLatch(1); |
| 240 | + final CountDownLatch completeLatch = new CountDownLatch(numThreads); |
| 241 | + final List<Future<Void>> futures = new ArrayList<>(); |
| 242 | + // Create threads that will concurrently update statistics |
| 243 | + for (int i = 0; i < numThreads; i++) { |
| 244 | + final int threadId = i; |
| 245 | + futures.add(executor.submit(() -> { |
| 246 | + try { |
| 247 | + final DefaultPooledObject<String> pooledObject = (DefaultPooledObject<String>) factory.makeObject(); |
| 248 | + // Wait for all threads to be ready |
| 249 | + startLatch.await(); |
| 250 | + // Perform concurrent operations |
| 251 | + for (int j = 0; j < operationsPerThread; j++) { |
| 252 | + pool.updateStatsBorrow(pooledObject, Duration.ofMillis(threadId * 10 + j)); |
| 253 | + pool.updateStatsReturn(Duration.ofMillis(threadId * 20 + j)); |
| 254 | + } |
| 255 | + } catch (Exception e) { |
| 256 | + throw new RuntimeException(e); |
| 257 | + } finally { |
| 258 | + completeLatch.countDown(); |
| 259 | + } |
| 260 | + return null; |
| 261 | + })); |
| 262 | + } |
| 263 | + // Start all threads simultaneously |
| 264 | + startLatch.countDown(); |
| 265 | + // Wait for completion |
| 266 | + assertTrue(completeLatch.await(30, TimeUnit.SECONDS), "Concurrent test should complete within 30 seconds"); |
| 267 | + // Verify no exceptions occurred |
| 268 | + for (Future<Void> future : futures) { |
| 269 | + future.get(); // Will throw if there was an exception |
| 270 | + } |
| 271 | + // Verify that statistics were collected (exact values may vary due to race conditions) |
| 272 | + assertEquals(numThreads * operationsPerThread, pool.getBorrowedCount()); |
| 273 | + assertEquals(numThreads * operationsPerThread, pool.getReturnedCount()); |
| 274 | + // Mean values should be reasonable (not zero or wildly incorrect) |
| 275 | + assertTrue(pool.getMeanActiveTimeMillis() >= 0); |
| 276 | + assertTrue(pool.getMeanBorrowWaitTimeMillis() >= 0); |
| 277 | + assertTrue(pool.getMaxBorrowWaitTimeMillis() >= 0); |
| 278 | + executor.shutdown(); |
| 279 | + assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS)); |
| 280 | + } |
| 281 | + |
| 282 | + @Test |
| 283 | + void testStatsStoreCircularBuffer() throws Exception { |
| 284 | + // Test that StatsStore properly handles circular buffer behavior |
| 285 | + final DefaultPooledObject<String> pooledObject = (DefaultPooledObject<String>) factory.makeObject(); |
| 286 | + // Fill beyond the cache size (100) to test circular behavior |
| 287 | + final int cacheSize = 100; // BaseGenericObjectPool.MEAN_TIMING_STATS_CACHE_SIZE |
| 288 | + for (int i = 0; i < cacheSize + 50; i++) { |
| 289 | + pool.updateStatsBorrow(pooledObject, Duration.ofMillis(i)); |
| 290 | + pool.updateStatsReturn(Duration.ofMillis(i * 2)); |
| 291 | + } |
| 292 | + // Statistics should still be meaningful after circular buffer wrapping |
| 293 | + assertTrue(pool.getMeanActiveTimeMillis() > 0); |
| 294 | + assertTrue(pool.getMeanBorrowWaitTimeMillis() > 0); |
| 295 | + assertTrue(pool.getMaxBorrowWaitTimeMillis() > 0); |
| 296 | + // The mean should reflect recent values, not all historical values |
| 297 | + // (exact assertion depends on circular buffer implementation) |
| 298 | + assertTrue(pool.getMeanBorrowWaitTimeMillis() >= 50); // Should be influenced by recent higher values |
| 299 | + } |
| 300 | + |
| 301 | + @Test |
| 302 | + void testDetailedStatisticsConfigIntegration() { |
| 303 | + // Test that config property is properly applied during pool construction |
| 304 | + final GenericObjectPoolConfig<String> config = new GenericObjectPoolConfig<>(); |
| 305 | + config.setCollectDetailedStatistics(false); |
| 306 | + try (GenericObjectPool<String, TestException> testPool = new GenericObjectPool<>(factory, config)) { |
| 307 | + assertFalse(testPool.getCollectDetailedStatistics(), "Pool should respect collectDetailedStatistics setting from config"); |
| 308 | + // Test that toString includes the new property |
| 309 | + final String configString = config.toString(); |
| 310 | + assertTrue(configString.contains("collectDetailedStatistics"), "Config toString should include collectDetailedStatistics property"); |
| 311 | + } |
| 312 | + } |
143 | 313 | } |
0 commit comments