Skip to content

Commit

Permalink
DBZ-7436 Add empty statistics for SingleDuration to avoid NPE
Browse files Browse the repository at this point in the history
(cherry picked from commit fa338b6)
  • Loading branch information
mfvitale committed Feb 2, 2024
1 parent ef6cbbb commit bcf87ff
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions debezium-core/src/main/java/io/debezium/util/Stopwatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,11 @@ public String toString() {
private static final class SingleDuration extends BaseDurations {
private final AtomicReference<Statistics> stats = new AtomicReference<>();

SingleDuration() {
LongSummaryStatistics stats = new LongSummaryStatistics();
this.stats.set(createStatistics(stats));
}

@Override
public Statistics statistics() {
return stats.get();
Expand Down
40 changes: 40 additions & 0 deletions debezium-core/src/test/java/io/debezium/util/StopwatchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.debezium.util;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

import io.debezium.doc.FixFor;

public class StopwatchTest {

@FixFor("DBZ-7436")
@Test
public void whenCallingDurationsOnAStartedStopwatchItShouldNotFailWithANPE() {

Stopwatch reusable = Stopwatch.reusable();

reusable.start();

String reusableStopWatchResults = reusable.durations().toString();

Stopwatch.StopwatchSet multiple = Stopwatch.multiple();

Stopwatch stopwatch = multiple.create();

String multipleStopWatchResults = stopwatch.durations().toString();

Stopwatch accumulating = Stopwatch.accumulating();

String accumulatingStopWatchResults = accumulating.durations().toString();

assertThat(reusableStopWatchResults).isEqualTo(" 0.00000s total; 0 samples; 0.00000s avg; 0.00000s min; 0.00000s max");
assertThat(multipleStopWatchResults).isEqualTo(" 0.00000s total; 0 samples; 0.00000s avg; 0.00000s min; 0.00000s max");
assertThat(accumulatingStopWatchResults).isEqualTo(" 0.00000s total; 0 samples; 0.00000s avg; 0.00000s min; 0.00000s max");
}
}

0 comments on commit bcf87ff

Please sign in to comment.