Skip to content

Fixed the endTime not modified bug to pass sonar cloud tests. #1774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void record(long duration, TimeUnit timeUnit) {
throw new IllegalArgumentException("Duration cannot be negative");
}

this.endTime.plusNanos(TimeUnit.NANOSECONDS.convert(duration, timeUnit));
this.endTime = this.endTime.plusNanos(TimeUnit.NANOSECONDS.convert(duration, timeUnit));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
package software.amazon.awssdk.metrics.meter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -44,12 +46,13 @@ public class DefaultTimerTest {

@Before
public void setupCase() {
when(mockClock.instant()).thenReturn(Instant.ofEpochMilli(0), Instant.ofEpochMilli(DURATION_MILLIS),
Instant.ofEpochMilli(0), Instant.ofEpochMilli(DURATION_MILLIS));

timer = DefaultTimer.builder()
.clock(mockClock)
.addCategory(MetricCategory.DEFAULT)
.build();

when(mockClock.instant()).thenReturn(Instant.ofEpochMilli(0), Instant.ofEpochMilli(DURATION_MILLIS));
}

@Test
Expand All @@ -73,6 +76,14 @@ public void testCallable() throws Exception {
assertThat(timer.totalTime()).isEqualTo(Duration.ofMillis(DURATION_MILLIS));
}

@Test
public void testEndTimeModifiedInRecord() {
Duration duration = Duration.ofNanos(100L);
Instant originEndTime = timer.endTime();
timer.record(duration.getNano(), TimeUnit.NANOSECONDS);
assertEquals(timer.endTime(), originEndTime.plusNanos(100L));
}

private static class MyCallable implements Callable<Integer> {
@Override
public Integer call() throws Exception {
Expand Down