You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/07/30 06:00:37 UTC

[GitHub] [kafka] dplavcic commented on a diff in pull request #12459: KAFKA-13036: Replace EasyMock and PowerMock with Mockito for RocksDBMetricsRecorderTest

dplavcic commented on code in PR #12459:
URL: https://github.com/apache/kafka/pull/12459#discussion_r933757660


##########
streams/src/test/java/org/apache/kafka/streams/state/internals/metrics/RocksDBMetricsRecorderTest.java:
##########
@@ -70,105 +73,136 @@ public class RocksDBMetricsRecorderTest {
     private final Statistics statisticsToAdd2 = mock(Statistics.class);
     private final Statistics statisticsToAdd3 = mock(Statistics.class);
 
-    private final Sensor bytesWrittenToDatabaseSensor = createMock(Sensor.class);
-    private final Sensor bytesReadFromDatabaseSensor = createMock(Sensor.class);
-    private final Sensor memtableBytesFlushedSensor = createMock(Sensor.class);
-    private final Sensor memtableHitRatioSensor = createMock(Sensor.class);
-    private final Sensor memtableAvgFlushTimeSensor = createMock(Sensor.class);
-    private final Sensor memtableMinFlushTimeSensor = createMock(Sensor.class);
-    private final Sensor memtableMaxFlushTimeSensor = createMock(Sensor.class);
-    private final Sensor writeStallDurationSensor = createMock(Sensor.class);
-    private final Sensor blockCacheDataHitRatioSensor = createMock(Sensor.class);
-    private final Sensor blockCacheIndexHitRatioSensor = createMock(Sensor.class);
-    private final Sensor blockCacheFilterHitRatioSensor = createMock(Sensor.class);
-    private final Sensor bytesReadDuringCompactionSensor = createMock(Sensor.class);
-    private final Sensor bytesWrittenDuringCompactionSensor = createMock(Sensor.class);
-    private final Sensor compactionTimeAvgSensor = createMock(Sensor.class);
-    private final Sensor compactionTimeMinSensor = createMock(Sensor.class);
-    private final Sensor compactionTimeMaxSensor = createMock(Sensor.class);
-    private final Sensor numberOfOpenFilesSensor = createMock(Sensor.class);
-    private final Sensor numberOfFileErrorsSensor = createMock(Sensor.class);
-
-    private final StreamsMetricsImpl streamsMetrics = niceMock(StreamsMetricsImpl.class);
+    private final Sensor bytesWrittenToDatabaseSensor = mock(Sensor.class);
+    private final Sensor bytesReadFromDatabaseSensor = mock(Sensor.class);
+    private final Sensor memtableBytesFlushedSensor = mock(Sensor.class);
+    private final Sensor memtableHitRatioSensor = mock(Sensor.class);
+    private final Sensor memtableAvgFlushTimeSensor = mock(Sensor.class);
+    private final Sensor memtableMinFlushTimeSensor = mock(Sensor.class);
+    private final Sensor memtableMaxFlushTimeSensor = mock(Sensor.class);
+    private final Sensor writeStallDurationSensor = mock(Sensor.class);
+    private final Sensor blockCacheDataHitRatioSensor = mock(Sensor.class);
+    private final Sensor blockCacheIndexHitRatioSensor = mock(Sensor.class);
+    private final Sensor blockCacheFilterHitRatioSensor = mock(Sensor.class);
+    private final Sensor bytesReadDuringCompactionSensor = mock(Sensor.class);
+    private final Sensor bytesWrittenDuringCompactionSensor = mock(Sensor.class);
+    private final Sensor compactionTimeAvgSensor = mock(Sensor.class);
+    private final Sensor compactionTimeMinSensor = mock(Sensor.class);
+    private final Sensor compactionTimeMaxSensor = mock(Sensor.class);
+    private final Sensor numberOfOpenFilesSensor = mock(Sensor.class);
+    private final Sensor numberOfFileErrorsSensor = mock(Sensor.class);
+
+    private final StreamsMetricsImpl streamsMetrics = mock(StreamsMetricsImpl.class);
     private final RocksDBMetricsRecordingTrigger recordingTrigger = mock(RocksDBMetricsRecordingTrigger.class);
 
     private final RocksDBMetricsRecorder recorder = new RocksDBMetricsRecorder(METRICS_SCOPE, STORE_NAME);
 
+    private MockedStatic<RocksDBMetrics> dbMetrics;
+
     @Before
     public void setUp() {
-        setUpMetricsStubMock();
-        expect(streamsMetrics.rocksDBMetricsRecordingTrigger()).andStubReturn(recordingTrigger);
-        replay(streamsMetrics);
+        setUpMetricsMock();
+        when(streamsMetrics.rocksDBMetricsRecordingTrigger()).thenReturn(recordingTrigger);
         recorder.init(streamsMetrics, TASK_ID1);
     }
 
+    @After
+    public void cleanUp() {
+        dbMetrics.close();
+    }
+
     @Test
     public void shouldInitMetricsRecorder() {
-        setUpMetricsMock();
-
-        recorder.init(streamsMetrics, TASK_ID1);
-
-        verify(RocksDBMetrics.class);
+        dbMetrics.verify(() -> RocksDBMetrics.bytesWrittenToDatabaseSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.bytesReadFromDatabaseSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.memtableBytesFlushedSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.memtableHitRatioSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.memtableAvgFlushTimeSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.memtableMinFlushTimeSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.memtableMaxFlushTimeSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.writeStallDurationSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.blockCacheDataHitRatioSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.blockCacheIndexHitRatioSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.blockCacheFilterHitRatioSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.bytesWrittenDuringCompactionSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.bytesReadDuringCompactionSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.compactionTimeAvgSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.compactionTimeMinSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.compactionTimeMaxSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.numberOfOpenFilesSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.numberOfFileErrorsSensor(any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addNumImmutableMemTableMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addCurSizeActiveMemTable(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addCurSizeAllMemTables(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addSizeAllMemTables(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addNumEntriesActiveMemTableMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addNumEntriesImmMemTablesMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addNumDeletesActiveMemTableMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addNumDeletesImmMemTablesMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addMemTableFlushPending(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addNumRunningFlushesMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addCompactionPendingMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addNumRunningCompactionsMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addEstimatePendingCompactionBytesMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addTotalSstFilesSizeMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addLiveSstFilesSizeMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addNumLiveVersionMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addBlockCacheCapacityMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addBlockCacheUsageMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addBlockCachePinnedUsageMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addEstimateNumKeysMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addEstimateTableReadersMemMetric(any(), any(), any()));
+        dbMetrics.verify(() -> RocksDBMetrics.addBackgroundErrorsMetric(any(), any(), any()));
         assertThat(recorder.taskId(), is(TASK_ID1));
     }
 
     @Test
     public void shouldThrowIfMetricRecorderIsReInitialisedWithDifferentTask() {
-        setUpMetricsStubMock();
-        recorder.init(streamsMetrics, TASK_ID1);
-
         assertThrows(
-            IllegalStateException.class,
-            () -> recorder.init(streamsMetrics, TASK_ID2)
+                IllegalStateException.class,
+                () -> recorder.init(streamsMetrics, TASK_ID2)
         );
     }
 
     @Test
     public void shouldThrowIfMetricRecorderIsReInitialisedWithDifferentStreamsMetrics() {
-        setUpMetricsStubMock();
-        recorder.init(streamsMetrics, TASK_ID1);
-
         assertThrows(
-            IllegalStateException.class,
-            () -> recorder.init(
-                new StreamsMetricsImpl(new Metrics(), "test-client", StreamsConfig.METRICS_LATEST, new MockTime()),
-                TASK_ID1
-            )
+                IllegalStateException.class,
+                () -> recorder.init(
+                        new StreamsMetricsImpl(new Metrics(), "test-client", StreamsConfig.METRICS_LATEST, new MockTime()),
+                        TASK_ID1
+                )
         );
     }
 
     @Test
     public void shouldSetStatsLevelToExceptDetailedTimersWhenValueProvidersWithStatisticsAreAdded() {
-        statisticsToAdd1.setStatsLevel(StatsLevel.EXCEPT_DETAILED_TIMERS);
-        replay(statisticsToAdd1);
+        doNothing().when(statisticsToAdd1).setStatsLevel(StatsLevel.EXCEPT_DETAILED_TIMERS);

Review Comment:
   I'm not sure if there is a convention on when to stub things, but according to the docs this is redundant (especially in combination with the `verify` at the end):
   > Use doNothing() for setting void methods to do nothing. **Beware that void methods on mocks do nothing by default!** However, there are rare situations when doNothing() comes handy: 
   > - Stubbing consecutive calls on a void method..
   > - When you spy real objects and you want the void method to do nothing ...
   
   - https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#when(T)
   
   Personally, I would just leave the `verify(statisticsToAdd1).setStatsLevel(eq(StatsLevel.EXCEPT_DETAILED_TIMERS));` line we have



##########
streams/src/test/java/org/apache/kafka/streams/state/internals/metrics/RocksDBMetricsRecorderTest.java:
##########
@@ -36,22 +38,23 @@
 import org.rocksdb.StatsLevel;
 import org.rocksdb.TickerType;
 
-import static org.easymock.EasyMock.anyObject;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.mock;
-import static org.easymock.EasyMock.niceMock;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertThrows;
-import static org.powermock.api.easymock.PowerMock.reset;
-import static org.powermock.api.easymock.PowerMock.createMock;
-import static org.powermock.api.easymock.PowerMock.mockStatic;
-import static org.powermock.api.easymock.PowerMock.replay;
-import static org.powermock.api.easymock.PowerMock.verify;
-
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({RocksDBMetrics.class, Sensor.class})
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isA;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)

Review Comment:
   What do you think we use `@RunWith(MockitoJUnitRunner.StrictStubs.class)` which automatically detects stubbing argument mismatches instead of the `@RunWith(MockitoJUnitRunner.class)` 🤔 
   - https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/junit/MockitoJUnitRunner.html
   - https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/junit/MockitoJUnitRunner.StrictStubs.html
   - https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/quality/Strictness.html#STRICT_STUBS
   
   With `StrictStubs` we get (c/p from the [docs](https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/quality/Strictness.html#STRICT_STUBS)):
   > - Improved productivity: the test fails early when code under test invokes stubbed method with different arguments (see [PotentialStubbingProblem](https://javadoc.io/static/org.mockito/mockito-core/4.6.1/org/mockito/exceptions/misusing/PotentialStubbingProblem.html)).
   > - Cleaner tests without unnecessary stubbings: the test fails when unused stubs are present (see [UnnecessaryStubbingException](https://javadoc.io/static/org.mockito/mockito-core/4.6.1/org/mockito/exceptions/misusing/UnnecessaryStubbingException.html)).
   > - Cleaner, more DRY tests ("Don't Repeat Yourself"): If you use [Mockito.verifyNoMoreInteractions(Object...)](https://javadoc.io/static/org.mockito/mockito-core/4.6.1/org/mockito/Mockito.html#verifyNoMoreInteractions(java.lang.Object...)) you no longer need to explicitly verify stubbed invocations. They are automatically verified for you.
   
   Considering this test for example:
   ```java
       @Test
       public void shouldSetStatsLevelToExceptDetailedTimersWhenValueProvidersWithStatisticsAreAdded() {
           doNothing().when(statisticsToAdd1).setStatsLevel(StatsLevel.EXCEPT_DETAILED_TIMERS);
   
           recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, statisticsToAdd1);
   
           verify(statisticsToAdd1).setStatsLevel(eq(StatsLevel.EXCEPT_DETAILED_TIMERS));
       }
   ```
   
   - with `StrictStubs`, if someone changes what is passed to the `setStatsLevel()` method in production code, and hard-code it to, for example `statistics.setStatsLevel(StatsLevel.ALL);`, this test would fail **even without explicit verify** with following message:
   
   ```
   Unnecessary stubbings detected in test class: RocksDBMetricsRecorderTest
   Clean & maintainable test code requires zero unnecessary code.
   Following stubbings are unnecessary (click to navigate to relevant line of code):
     1. -> at org.apache.kafka.streams.state.internals.metrics.RocksDBMetricsRecorderTest.shouldSetStatsLevelToExceptDetailedTimersWhenValueProvidersWithStatisticsAreAdded(RocksDBMetricsRecorderTest.java:180)
   Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.
   ...
   ```
   
   Mockito documentation provides a good explanation of what is happening (and why verify step could be redundant):
   - https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#when(T)
   - https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#verify(T)
   > Although it is possible to verify a stubbed invocation, usually it's just redundant. Let's say you've stubbed foo.bar(). If your code cares what foo.bar() returns then something else breaks(often before even verify() gets executed). If your code doesn't care what get(0) returns then it should not be stubbed. 
   
   
   Note:
   - I'm not sure if Mockito docs links with `fragment/anchor` don't work just for me or for all 🤔 
   
   
   



##########
streams/src/test/java/org/apache/kafka/streams/state/internals/metrics/RocksDBMetricsRecorderTest.java:
##########
@@ -279,449 +313,349 @@ public void shouldThrowIfDbToAddWasAlreadyAddedForOtherSegment() {
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, statisticsToAdd1);
 
         final Throwable exception = assertThrows(
-            IllegalStateException.class,
-            () -> recorder.addValueProviders(SEGMENT_STORE_NAME_2, dbToAdd1, cacheToAdd2, statisticsToAdd2)
+                IllegalStateException.class,
+                () -> recorder.addValueProviders(SEGMENT_STORE_NAME_2, dbToAdd1, cacheToAdd2, statisticsToAdd2)
         );
         assertThat(
-            exception.getMessage(),
-            is("DB instance for store " + SEGMENT_STORE_NAME_2 + " of task " + TASK_ID1 +
-                " was already added for another segment as a value provider. This is a bug in Kafka Streams. " +
-                "Please open a bug report under https://issues.apache.org/jira/projects/KAFKA/issues")
+                exception.getMessage(),
+                is("DB instance for store " + SEGMENT_STORE_NAME_2 + " of task " + TASK_ID1 +
+                        " was already added for another segment as a value provider. This is a bug in Kafka Streams. " +
+                        "Please open a bug report under https://issues.apache.org/jira/projects/KAFKA/issues")
         );
     }
 
     @Test
     public void shouldAddItselfToRecordingTriggerWhenFirstValueProvidersAreAddedToNewlyCreatedRecorder() {
-        recordingTrigger.addMetricsRecorder(recorder);
-        replay(recordingTrigger);
+        doNothing().when(recordingTrigger).addMetricsRecorder(recorder);
 
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, statisticsToAdd1);
 
-        verify(recordingTrigger);
+        verify(recordingTrigger).addMetricsRecorder(eq(recorder));
     }
 
     @Test
     public void shouldAddItselfToRecordingTriggerWhenFirstValueProvidersAreAddedAfterLastValueProvidersWereRemoved() {
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, statisticsToAdd1);
         recorder.removeValueProviders(SEGMENT_STORE_NAME_1);
-        reset(recordingTrigger);
-        recordingTrigger.addMetricsRecorder(recorder);
-        replay(recordingTrigger);
+
+        Mockito.reset(recordingTrigger);
+
+        doNothing().when(recordingTrigger).addMetricsRecorder(recorder);
 
         recorder.addValueProviders(SEGMENT_STORE_NAME_2, dbToAdd2, cacheToAdd2, statisticsToAdd2);
 
-        verify(recordingTrigger);
+        verify(recordingTrigger).addMetricsRecorder(recorder);
     }
 
     @Test
     public void shouldNotAddItselfToRecordingTriggerWhenNotEmpty2() {
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, statisticsToAdd1);
-        reset(recordingTrigger);
-        replay(recordingTrigger);
+
+        verify(recordingTrigger).addMetricsRecorder(recorder);
 
         recorder.addValueProviders(SEGMENT_STORE_NAME_2, dbToAdd2, cacheToAdd2, statisticsToAdd2);
 
-        verify(recordingTrigger);
+        verifyNoMoreInteractions(recordingTrigger);
     }
 
     @Test
     public void shouldCloseStatisticsWhenValueProvidersAreRemoved() {
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, statisticsToAdd1);
-        reset(statisticsToAdd1);
-        statisticsToAdd1.close();
-        replay(statisticsToAdd1);
+        doNothing().when(statisticsToAdd1).close();
 
         recorder.removeValueProviders(SEGMENT_STORE_NAME_1);
 
-        verify(statisticsToAdd1);
+        verify(statisticsToAdd1).close();
     }
 
     @Test
     public void shouldNotCloseStatisticsWhenValueProvidersWithoutStatisticsAreRemoved() {
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, null);
-        reset(statisticsToAdd1);
-        replay(statisticsToAdd1);
-
         recorder.removeValueProviders(SEGMENT_STORE_NAME_1);
-
-        verify(statisticsToAdd1);
+        verify(statisticsToAdd1, never()).close();
     }
 
     @Test
     public void shouldRemoveItselfFromRecordingTriggerWhenLastValueProvidersAreRemoved() {
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, statisticsToAdd1);
         recorder.addValueProviders(SEGMENT_STORE_NAME_2, dbToAdd2, cacheToAdd2, statisticsToAdd2);
-        reset(recordingTrigger);
-        replay(recordingTrigger);
+
+        Mockito.reset(recordingTrigger);
 
         recorder.removeValueProviders(SEGMENT_STORE_NAME_1);
 
-        verify(recordingTrigger);
+        verifyNoInteractions(recordingTrigger);
 
-        reset(recordingTrigger);
-        recordingTrigger.removeMetricsRecorder(recorder);
-        replay(recordingTrigger);
+        Mockito.reset(recordingTrigger);
+        doNothing().when(recordingTrigger).removeMetricsRecorder(recorder);
 
         recorder.removeValueProviders(SEGMENT_STORE_NAME_2);
 
-        verify(recordingTrigger);
+        verify(recordingTrigger).removeMetricsRecorder(eq(recorder));
     }
 
     @Test
     public void shouldThrowIfValueProvidersToRemoveNotFound() {
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, statisticsToAdd1);
 
         assertThrows(
-            IllegalStateException.class,
-            () -> recorder.removeValueProviders(SEGMENT_STORE_NAME_2)
+                IllegalStateException.class,
+                () -> recorder.removeValueProviders(SEGMENT_STORE_NAME_2)
         );
     }
 
     @Test
     public void shouldRecordStatisticsBasedMetrics() {
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, statisticsToAdd1);
         recorder.addValueProviders(SEGMENT_STORE_NAME_2, dbToAdd2, cacheToAdd2, statisticsToAdd2);
-        reset(statisticsToAdd1);
-        reset(statisticsToAdd2);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.BYTES_WRITTEN)).andReturn(1L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.BYTES_WRITTEN)).andReturn(2L);
-        bytesWrittenToDatabaseSensor.record(1 + 2, 0L);
-        replay(bytesWrittenToDatabaseSensor);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.BYTES_READ)).andReturn(2L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.BYTES_READ)).andReturn(3L);
-        bytesReadFromDatabaseSensor.record(2 + 3, 0L);
-        replay(bytesReadFromDatabaseSensor);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.FLUSH_WRITE_BYTES)).andReturn(3L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.FLUSH_WRITE_BYTES)).andReturn(4L);
-        memtableBytesFlushedSensor.record(3 + 4, 0L);
-        replay(memtableBytesFlushedSensor);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.MEMTABLE_HIT)).andReturn(1L);
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.MEMTABLE_MISS)).andReturn(2L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.MEMTABLE_HIT)).andReturn(3L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.MEMTABLE_MISS)).andReturn(4L);
-        memtableHitRatioSensor.record((double) 4 / (4 + 6), 0L);
-        replay(memtableHitRatioSensor);
+        final long now = 0L;
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.BYTES_WRITTEN)).thenReturn(1L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.BYTES_WRITTEN)).thenReturn(2L);
+        doNothing().when(bytesWrittenToDatabaseSensor).record(1 + 2, now);
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.BYTES_READ)).thenReturn(2L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.BYTES_READ)).thenReturn(3L);
+        doNothing().when(bytesReadFromDatabaseSensor).record(2 + 3, now);
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.FLUSH_WRITE_BYTES)).thenReturn(3L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.FLUSH_WRITE_BYTES)).thenReturn(4L);
+        doNothing().when(memtableBytesFlushedSensor).record(3 + 4, now);
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.MEMTABLE_HIT)).thenReturn(1L);
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.MEMTABLE_MISS)).thenReturn(2L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.MEMTABLE_HIT)).thenReturn(3L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.MEMTABLE_MISS)).thenReturn(4L);
+        doNothing().when(memtableHitRatioSensor).record((double) 4 / (4 + 6), now);
 
         final HistogramData memtableFlushTimeData1 = new HistogramData(0.0, 0.0, 0.0, 0.0, 0.0, 16.0, 2L, 10L, 3.0);
         final HistogramData memtableFlushTimeData2 = new HistogramData(0.0, 0.0, 0.0, 0.0, 0.0, 20.0, 4L, 8L, 10.0);
-        expect(statisticsToAdd1.getHistogramData(HistogramType.FLUSH_TIME)).andReturn(memtableFlushTimeData1);
-        expect(statisticsToAdd2.getHistogramData(HistogramType.FLUSH_TIME)).andReturn(memtableFlushTimeData2);
-        memtableAvgFlushTimeSensor.record((double) (10 + 8) / (2 + 4), 0L);
-        replay(memtableAvgFlushTimeSensor);
-        memtableMinFlushTimeSensor.record(3.0, 0L);
-        replay(memtableMinFlushTimeSensor);
-        memtableMaxFlushTimeSensor.record(20.0, 0L);
-        replay(memtableMaxFlushTimeSensor);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.STALL_MICROS)).andReturn(4L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.STALL_MICROS)).andReturn(5L);
-        writeStallDurationSensor.record(4 + 5, 0L);
-        replay(writeStallDurationSensor);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_DATA_HIT)).andReturn(5L);
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_DATA_MISS)).andReturn(4L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_DATA_HIT)).andReturn(3L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_DATA_MISS)).andReturn(2L);
-        blockCacheDataHitRatioSensor.record((double) 8 / (8 + 6), 0L);
-        replay(blockCacheDataHitRatioSensor);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_INDEX_HIT)).andReturn(4L);
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_INDEX_MISS)).andReturn(2L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_INDEX_HIT)).andReturn(2L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_INDEX_MISS)).andReturn(4L);
-        blockCacheIndexHitRatioSensor.record((double) 6 / (6 + 6), 0L);
-        replay(blockCacheIndexHitRatioSensor);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_FILTER_HIT)).andReturn(2L);
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_FILTER_MISS)).andReturn(4L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_FILTER_HIT)).andReturn(3L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_FILTER_MISS)).andReturn(5L);
-        blockCacheFilterHitRatioSensor.record((double) 5 / (5 + 9), 0L);
-        replay(blockCacheFilterHitRatioSensor);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.COMPACT_WRITE_BYTES)).andReturn(2L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.COMPACT_WRITE_BYTES)).andReturn(4L);
-        bytesWrittenDuringCompactionSensor.record(2 + 4, 0L);
-        replay(bytesWrittenDuringCompactionSensor);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.COMPACT_READ_BYTES)).andReturn(5L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.COMPACT_READ_BYTES)).andReturn(6L);
-        bytesReadDuringCompactionSensor.record(5 + 6, 0L);
-        replay(bytesReadDuringCompactionSensor);
+        when(statisticsToAdd1.getHistogramData(HistogramType.FLUSH_TIME)).thenReturn(memtableFlushTimeData1);
+        when(statisticsToAdd2.getHistogramData(HistogramType.FLUSH_TIME)).thenReturn(memtableFlushTimeData2);
+        doNothing().when(memtableAvgFlushTimeSensor).record((double) (10 + 8) / (2 + 4), now);
+        doNothing().when(memtableMinFlushTimeSensor).record(3.0, now);
+        doNothing().when(memtableMaxFlushTimeSensor).record(20.0, now);
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.STALL_MICROS)).thenReturn(4L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.STALL_MICROS)).thenReturn(5L);
+        doNothing().when(writeStallDurationSensor).record(4 + 5, now);
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_DATA_HIT)).thenReturn(5L);
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_DATA_MISS)).thenReturn(4L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_DATA_HIT)).thenReturn(3L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_DATA_MISS)).thenReturn(2L);
+        doNothing().when(blockCacheDataHitRatioSensor).record((double) 8 / (8 + 6), now);
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_INDEX_HIT)).thenReturn(4L);
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_INDEX_MISS)).thenReturn(2L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_INDEX_HIT)).thenReturn(2L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_INDEX_MISS)).thenReturn(4L);
+        doNothing().when(blockCacheIndexHitRatioSensor).record((double) 6 / (6 + 6), now);
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_FILTER_HIT)).thenReturn(2L);
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.BLOCK_CACHE_FILTER_MISS)).thenReturn(4L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_FILTER_HIT)).thenReturn(3L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.BLOCK_CACHE_FILTER_MISS)).thenReturn(5L);
+        doNothing().when(blockCacheFilterHitRatioSensor).record((double) 5 / (5 + 9), now);
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.COMPACT_WRITE_BYTES)).thenReturn(2L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.COMPACT_WRITE_BYTES)).thenReturn(4L);
+        doNothing().when(bytesWrittenDuringCompactionSensor).record(2 + 4, now);
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.COMPACT_READ_BYTES)).thenReturn(5L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.COMPACT_READ_BYTES)).thenReturn(6L);
+        doNothing().when(bytesReadDuringCompactionSensor).record(5 + 6, now);
 
         final HistogramData compactionTimeData1 = new HistogramData(0.0, 0.0, 0.0, 0.0, 0.0, 16.0, 2L, 8L, 6.0);
         final HistogramData compactionTimeData2 = new HistogramData(0.0, 0.0, 0.0, 0.0, 0.0, 24.0, 2L, 8L, 4.0);
-        expect(statisticsToAdd1.getHistogramData(HistogramType.COMPACTION_TIME)).andReturn(compactionTimeData1);
-        expect(statisticsToAdd2.getHistogramData(HistogramType.COMPACTION_TIME)).andReturn(compactionTimeData2);
-        compactionTimeAvgSensor.record((double) (8 + 8) / (2 + 2), 0L);
-        replay(compactionTimeAvgSensor);
-        compactionTimeMinSensor.record(4.0, 0L);
-        replay(compactionTimeMinSensor);
-        compactionTimeMaxSensor.record(24.0, 0L);
-        replay(compactionTimeMaxSensor);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.NO_FILE_OPENS)).andReturn(5L);
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.NO_FILE_CLOSES)).andReturn(3L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.NO_FILE_OPENS)).andReturn(7L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.NO_FILE_CLOSES)).andReturn(4L);
-        numberOfOpenFilesSensor.record((5 + 7) - (3 + 4), 0L);
-        replay(numberOfOpenFilesSensor);
-
-        expect(statisticsToAdd1.getAndResetTickerCount(TickerType.NO_FILE_ERRORS)).andReturn(34L);
-        expect(statisticsToAdd2.getAndResetTickerCount(TickerType.NO_FILE_ERRORS)).andReturn(11L);
-        numberOfFileErrorsSensor.record(11 + 34, 0L);
-        replay(numberOfFileErrorsSensor);
-
-        replay(statisticsToAdd1);
-        replay(statisticsToAdd2);
-
-        recorder.record(0L);
-
-        verify(statisticsToAdd1);
-        verify(statisticsToAdd2);
-        verify(
-            bytesWrittenToDatabaseSensor,
-            bytesReadFromDatabaseSensor,
-            memtableBytesFlushedSensor,
-            memtableHitRatioSensor,
-            memtableAvgFlushTimeSensor,
-            memtableMinFlushTimeSensor,
-            memtableMaxFlushTimeSensor,
-            writeStallDurationSensor,
-            blockCacheDataHitRatioSensor,
-            blockCacheIndexHitRatioSensor,
-            blockCacheFilterHitRatioSensor,
-            bytesWrittenDuringCompactionSensor,
-            bytesReadDuringCompactionSensor,
-            compactionTimeAvgSensor,
-            compactionTimeMinSensor,
-            compactionTimeMaxSensor,
-            numberOfOpenFilesSensor,
-            numberOfFileErrorsSensor
-        );
+        when(statisticsToAdd1.getHistogramData(HistogramType.COMPACTION_TIME)).thenReturn(compactionTimeData1);
+        when(statisticsToAdd2.getHistogramData(HistogramType.COMPACTION_TIME)).thenReturn(compactionTimeData2);
+        doNothing().when(compactionTimeAvgSensor).record((double) (8 + 8) / (2 + 2), now);
+        doNothing().when(compactionTimeMinSensor).record(4.0, now);
+        doNothing().when(compactionTimeMaxSensor).record(24.0, now);
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.NO_FILE_OPENS)).thenReturn(5L);
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.NO_FILE_CLOSES)).thenReturn(3L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.NO_FILE_OPENS)).thenReturn(7L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.NO_FILE_CLOSES)).thenReturn(4L);
+        doNothing().when(numberOfOpenFilesSensor).record((5 + 7) - (3 + 4), now);
+
+        when(statisticsToAdd1.getAndResetTickerCount(TickerType.NO_FILE_ERRORS)).thenReturn(34L);
+        when(statisticsToAdd2.getAndResetTickerCount(TickerType.NO_FILE_ERRORS)).thenReturn(11L);
+        doNothing().when(numberOfFileErrorsSensor).record(11 + 34, now);
+
+        recorder.record(now);
+
+        verify(statisticsToAdd1, times(17)).getAndResetTickerCount(isA(TickerType.class));
+        verify(statisticsToAdd2, times(17)).getAndResetTickerCount(isA(TickerType.class));
+        verify(statisticsToAdd2, times(2)).getHistogramData(isA(HistogramType.class));
+        verify(statisticsToAdd2, times(2)).getHistogramData(isA(HistogramType.class));
+        verify(bytesWrittenToDatabaseSensor).record(isA(Double.class), eq(now));
+        verify(bytesReadFromDatabaseSensor).record(isA(Double.class), eq(now));
+        verify(memtableBytesFlushedSensor).record(isA(Double.class), eq(now));
+        verify(memtableHitRatioSensor).record(isA(Double.class), eq(now));
+        verify(memtableAvgFlushTimeSensor).record(isA(Double.class), eq(now));
+        verify(memtableMinFlushTimeSensor).record(isA(Double.class), eq(now));
+        verify(memtableMaxFlushTimeSensor).record(isA(Double.class), eq(now));
+        verify(writeStallDurationSensor).record(isA(Double.class), eq(now));
+        verify(blockCacheDataHitRatioSensor).record(isA(Double.class), eq(now));
+        verify(blockCacheIndexHitRatioSensor).record(isA(Double.class), eq(now));
+        verify(blockCacheFilterHitRatioSensor).record(isA(Double.class), eq(now));
+        verify(bytesWrittenDuringCompactionSensor).record(isA(Double.class), eq(now));
+        verify(bytesReadDuringCompactionSensor).record(isA(Double.class), eq(now));
+        verify(compactionTimeAvgSensor).record(isA(Double.class), eq(now));
+        verify(compactionTimeMinSensor).record(isA(Double.class), eq(now));
+        verify(compactionTimeMaxSensor).record(isA(Double.class), eq(now));
+        verify(numberOfOpenFilesSensor).record(isA(Double.class), eq(now));
+        verify(numberOfFileErrorsSensor).record(isA(Double.class), eq(now));
     }
 
     @Test
     public void shouldNotRecordStatisticsBasedMetricsIfStatisticsIsNull() {
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, null);
-        replay(
-            bytesWrittenToDatabaseSensor,
-            bytesReadFromDatabaseSensor,
-            memtableBytesFlushedSensor,
-            memtableHitRatioSensor,
-            memtableAvgFlushTimeSensor,
-            memtableMinFlushTimeSensor,
-            memtableMaxFlushTimeSensor,
-            writeStallDurationSensor,
-            blockCacheDataHitRatioSensor,
-            blockCacheIndexHitRatioSensor,
-            blockCacheFilterHitRatioSensor,
-            bytesWrittenDuringCompactionSensor,
-            bytesReadDuringCompactionSensor,
-            compactionTimeAvgSensor,
-            compactionTimeMinSensor,
-            compactionTimeMaxSensor,
-            numberOfOpenFilesSensor,
-            numberOfFileErrorsSensor
-        );
 
         recorder.record(0L);
 
-        verify(
-            bytesWrittenToDatabaseSensor,
-            bytesReadFromDatabaseSensor,
-            memtableBytesFlushedSensor,
-            memtableHitRatioSensor,
-            memtableAvgFlushTimeSensor,
-            memtableMinFlushTimeSensor,
-            memtableMaxFlushTimeSensor,
-            writeStallDurationSensor,
-            blockCacheDataHitRatioSensor,
-            blockCacheIndexHitRatioSensor,
-            blockCacheFilterHitRatioSensor,
-            bytesWrittenDuringCompactionSensor,
-            bytesReadDuringCompactionSensor,
-            compactionTimeAvgSensor,
-            compactionTimeMinSensor,
-            compactionTimeMaxSensor,
-            numberOfOpenFilesSensor,
-            numberOfFileErrorsSensor
-        );
+        verifyNoInteractions(bytesWrittenToDatabaseSensor,
+                bytesReadFromDatabaseSensor,
+                memtableBytesFlushedSensor,
+                memtableHitRatioSensor,
+                memtableAvgFlushTimeSensor,
+                memtableMinFlushTimeSensor,
+                memtableMaxFlushTimeSensor,
+                writeStallDurationSensor,
+                blockCacheDataHitRatioSensor,
+                blockCacheIndexHitRatioSensor,
+                blockCacheFilterHitRatioSensor,
+                bytesWrittenDuringCompactionSensor,
+                bytesReadDuringCompactionSensor,
+                compactionTimeAvgSensor,
+                compactionTimeMinSensor,
+                compactionTimeMaxSensor,
+                numberOfOpenFilesSensor,
+                numberOfFileErrorsSensor);
     }
 
     @Test
     public void shouldCorrectlyHandleHitRatioRecordingsWithZeroHitsAndMisses() {
-        reset(statisticsToAdd1);
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, statisticsToAdd1);
-        expect(statisticsToAdd1.getHistogramData(anyObject())).andStubReturn(new HistogramData(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0L, 0L, 0.0));
-        expect(statisticsToAdd1.getAndResetTickerCount(anyObject())).andStubReturn(0L);
-        replay(statisticsToAdd1);
-        memtableHitRatioSensor.record(0, 0L);
-        blockCacheDataHitRatioSensor.record(0, 0L);
-        blockCacheIndexHitRatioSensor.record(0, 0L);
-        blockCacheFilterHitRatioSensor.record(0, 0L);
-        replay(memtableHitRatioSensor);
-        replay(blockCacheDataHitRatioSensor);
-        replay(blockCacheIndexHitRatioSensor);
-        replay(blockCacheFilterHitRatioSensor);
+        when(statisticsToAdd1.getHistogramData(any())).thenReturn(new HistogramData(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0L, 0L, 0.0));
+        when(statisticsToAdd1.getAndResetTickerCount(any())).thenReturn(0L);
+        doNothing().when(memtableHitRatioSensor).record(0d, 0L);
+        doNothing().when(blockCacheDataHitRatioSensor).record(0d, 0L);
+        doNothing().when(blockCacheIndexHitRatioSensor).record(0d, 0L);
+        doNothing().when(blockCacheFilterHitRatioSensor).record(0d, 0L);
 
         recorder.record(0L);
 
-        verify(memtableHitRatioSensor);
-        verify(blockCacheDataHitRatioSensor);
-        verify(blockCacheIndexHitRatioSensor);
-        verify(blockCacheFilterHitRatioSensor);
+        verify(memtableHitRatioSensor).record(eq(0d), eq(0L));
+        verify(blockCacheDataHitRatioSensor).record(eq(0d), eq(0L));
+        verify(blockCacheIndexHitRatioSensor).record(eq(0d), eq(0L));
+        verify(blockCacheFilterHitRatioSensor).record(eq(0d), eq(0L));
+        verify(statisticsToAdd1, times(2)).getHistogramData(any());
+        verify(statisticsToAdd1, times(17)).getAndResetTickerCount(any());
     }
 
     @Test
     public void shouldCorrectlyHandleAvgRecordingsWithZeroSumAndCount() {
-        reset(statisticsToAdd1);
         recorder.addValueProviders(SEGMENT_STORE_NAME_1, dbToAdd1, cacheToAdd1, statisticsToAdd1);
-        expect(statisticsToAdd1.getHistogramData(anyObject())).andStubReturn(new HistogramData(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0L, 0L, 0.0));
-        expect(statisticsToAdd1.getAndResetTickerCount(anyObject())).andStubReturn(0L);
-        replay(statisticsToAdd1);
-        memtableAvgFlushTimeSensor.record(0, 0L);
-        compactionTimeAvgSensor.record(0, 0L);
-        replay(memtableAvgFlushTimeSensor);
-        replay(compactionTimeAvgSensor);
-
-        recorder.record(0L);
-
-        verify(memtableAvgFlushTimeSensor);
-        verify(compactionTimeAvgSensor);
+        final long now = 0L;
+        when(statisticsToAdd1.getHistogramData(any())).thenReturn(new HistogramData(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0L, 0L, 0.0));
+        when(statisticsToAdd1.getAndResetTickerCount(any())).thenReturn(0L);
+        doNothing().when(compactionTimeAvgSensor).record(eq(0d), eq(now));

Review Comment:
   There is no need to use matchers when all provided args are raw values.
   
   This can be updated to `doNothing().when(compactionTimeAvgSensor).record(0d, now);`
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org