Back to Repositories

Testing Dropwizard 5 Metrics Tracker Cleanup Implementation in HikariCP

This test suite validates the Dropwizard 5 metrics tracking functionality in HikariCP’s connection pool management system. It focuses on verifying proper cleanup and removal of various connection pool metrics when the tracker is closed.

Test Coverage Overview

The test suite covers the cleanup behavior of the Dropwizard5MetricsTracker component by verifying the removal of all registered metrics during shutdown.

Key functionality tested includes:
  • Verification of pool wait time metric removal
  • Connection usage and creation metric cleanup
  • Connection state metrics (total, idle, active, pending)
  • Pool configuration metrics (max and min connections)

Implementation Analysis

The testing approach utilizes Mockito’s mocking framework to verify interactions with the MetricRegistry. The test employs a MockitoJUnitRunner for automatic mock initialization and uses a StubPoolStats implementation for isolated testing.

The implementation follows a clear arrange-act-assert pattern with mock verification to ensure proper cleanup of all metric names.

Technical Details

Testing tools and configuration:
  • JUnit 4 test framework
  • Mockito mocking framework
  • MockitoJUnitRunner for test execution
  • StubPoolStats for test isolation
  • Dropwizard Metrics 5 integration

Best Practices Demonstrated

The test demonstrates several quality testing practices:

  • Proper test isolation using mocks and stubs
  • Clear test method naming and organization
  • Comprehensive verification of all metric cleanup operations
  • Efficient setup using @Before annotation
  • Consistent naming conventions for test components

brettwooldridge/hikaricp

src/test/java/com/zaxxer/hikari/metrics/dropwizard/Dropwizard5MetricsTrackerTest.java

            
package com.zaxxer.hikari.metrics.dropwizard;

import com.zaxxer.hikari.mocks.StubPoolStats;
import io.dropwizard.metrics5.MetricRegistry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class Dropwizard5MetricsTrackerTest
{
   @Mock
   public MetricRegistry mockMetricRegistry;

   private Dropwizard5MetricsTracker testee;

   @Before
   public void setup()
   {
      testee = new Dropwizard5MetricsTracker("mypool", new StubPoolStats(0), mockMetricRegistry);
   }

   @Test
   public void close()
   {
      testee.close();

      verify(mockMetricRegistry).remove(MetricRegistry.name("mypool.pool.Wait"));
      verify(mockMetricRegistry).remove(MetricRegistry.name("mypool.pool.Usage"));
      verify(mockMetricRegistry).remove(MetricRegistry.name("mypool.pool.ConnectionCreation"));
      verify(mockMetricRegistry).remove(MetricRegistry.name("mypool.pool.ConnectionTimeoutRate"));
      verify(mockMetricRegistry).remove(MetricRegistry.name("mypool.pool.TotalConnections"));
      verify(mockMetricRegistry).remove(MetricRegistry.name("mypool.pool.IdleConnections"));
      verify(mockMetricRegistry).remove(MetricRegistry.name("mypool.pool.ActiveConnections"));
      verify(mockMetricRegistry).remove(MetricRegistry.name("mypool.pool.PendingConnections"));
      verify(mockMetricRegistry).remove(MetricRegistry.name("mypool.pool.MaxConnections"));
      verify(mockMetricRegistry).remove(MetricRegistry.name("mypool.pool.MinConnections"));
   }
}