Skip to content

Commit 3bdc573

Browse files
authored
Merge pull request eugenp#5226 from dkapil/task/BAEL-8902
BAEL-8902 Metrics Project Test Fails
2 parents 0553cb6 + a05fc4f commit 3bdc573

4 files changed

Lines changed: 35 additions & 19 deletions

File tree

‎metrics/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
<artifactId>spectator-api</artifactId>
8585
<version>${spectator-api.version}</version>
8686
</dependency>
87+
88+
<dependency>
89+
<groupId>org.assertj</groupId>
90+
<artifactId>assertj-core</artifactId>
91+
<scope>test</scope>
92+
</dependency>
93+
8794
</dependencies>
8895

8996
<dependencyManagement>

‎metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasIntegrationTest.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.baeldung.metrics.micrometer;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.within;
5+
import static org.assertj.core.api.Assertions.withinPercentage;
36
import static org.hamcrest.CoreMatchers.allOf;
47
import static org.hamcrest.CoreMatchers.equalTo;
58
import static org.hamcrest.collection.IsMapContaining.hasEntry;
9+
import static org.junit.Assert.assertEquals;
610
import static org.junit.Assert.assertThat;
711
import static org.junit.Assert.assertTrue;
812
import io.micrometer.atlas.AtlasMeterRegistry;
@@ -31,8 +35,10 @@
3135
import java.util.concurrent.TimeUnit;
3236
import java.util.stream.Collectors;
3337

38+
import org.assertj.core.data.Percentage;
3439
import org.junit.Before;
3540
import org.junit.Test;
41+
import org.junit.jupiter.api.Assertions;
3642

3743
import com.netflix.spectator.atlas.AtlasConfig;
3844

@@ -156,7 +162,8 @@ public void givenTimer_whenWrapTasks_thenTimeRecorded() {
156162
timer.record(30, TimeUnit.MILLISECONDS);
157163

158164
assertTrue(2 == timer.count());
159-
assertTrue(50 > timer.totalTime(TimeUnit.MILLISECONDS) && 45 <= timer.totalTime(TimeUnit.MILLISECONDS));
165+
166+
assertThat(timer.totalTime(TimeUnit.MILLISECONDS)).isBetween(40.0, 55.0);
160167
}
161168

162169
@Test
@@ -173,7 +180,7 @@ public void givenLongTimer_whenRunTasks_thenTimerRecorded() {
173180
}
174181
long timeElapsed = longTaskTimer.stop(currentTaskId);
175182

176-
assertTrue(timeElapsed / (int) 1e6 == 2);
183+
assertEquals(2L, timeElapsed/((int) 1e6),1L);
177184
}
178185

179186
@Test

‎metrics/src/test/java/com/baeldung/metrics/servo/AtlasObserverLiveTest.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import static org.hamcrest.CoreMatchers.not;
2828
import static org.junit.Assert.assertThat;
2929

30+
/**
31+
* Atlas server needs to be up and running for this live test to work.
32+
*/
3033
public class AtlasObserverLiveTest {
3134

3235
private final String atlasUri = "http://localhost:7101/api/v1";

‎metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeManualTest.java‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,20 @@ public void givenTimer_whenExecuteTask_thenTimerUpdated() throws Exception {
109109
.build(), MILLISECONDS);
110110

111111
Stopwatch stopwatch = timer.start();
112-
MILLISECONDS.sleep(1);
113-
timer.record(2, MILLISECONDS);
112+
SECONDS.sleep(1);
113+
timer.record(2, SECONDS);
114114
stopwatch.stop();
115115

116-
assertEquals("timer should count 1 millisecond", 1, timer
116+
assertEquals("timer should count 1 second", 1000, timer
117117
.getValue()
118-
.intValue());
119-
assertEquals("timer should count 3 millisecond in total", 3, timer.getTotalTime()
120-
.intValue());
118+
.intValue(),1000);
119+
assertEquals("timer should count 3 second in total", 3000, timer.getTotalTime()
120+
.intValue(),1000);
121121
assertEquals("timer should record 2 updates", 2, timer
122122
.getCount()
123123
.intValue());
124124

125-
assertEquals("timer should have max 2", 2, timer.getMax(), 0.01);
125+
assertEquals("timer should have max 2", 2000, timer.getMax(), 0.01);
126126
}
127127

128128
@Test
@@ -161,7 +161,6 @@ private static String getMonitorTagValue(Monitor monitor, String tagKey) {
161161
}
162162

163163
@Test
164-
//==
165164
public void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Exception {
166165
System.setProperty("netflix.servo", "1000");
167166
StatsTimer timer = new StatsTimer(MonitorConfig
@@ -178,21 +177,21 @@ public void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Excepti
178177
.build(), MILLISECONDS);
179178

180179
Stopwatch stopwatch = timer.start();
181-
MILLISECONDS.sleep(1);
182-
timer.record(3, MILLISECONDS);
180+
SECONDS.sleep(1);
181+
timer.record(3, SECONDS);
183182
stopwatch.stop();
184183

185184
stopwatch = timer.start();
186-
timer.record(6, MILLISECONDS);
187-
MILLISECONDS.sleep(2);
185+
timer.record(6, SECONDS);
186+
SECONDS.sleep(2);
188187
stopwatch.stop();
189188

190-
assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalTime());
191-
assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalMeasurement());
189+
assertEquals("timer should count 12 seconds in total", 12000, timer.getTotalTime(),500);
190+
assertEquals("timer should count 12 seconds in total", 12000, timer.getTotalMeasurement(),500);
192191
assertEquals("timer should record 4 updates", 4, timer.getCount());
193-
assertEquals("stats timer value time-cost/update should be 2", 3, timer
192+
assertEquals("stats timer value time-cost/update should be 2", 3000, timer
194193
.getValue()
195-
.intValue());
194+
.intValue(),500);
196195

197196
final Map<String, Number> metricMap = timer
198197
.getMonitors()
@@ -235,4 +234,4 @@ public void givenInformationalMonitor_whenRecord_thenInformationCollected() thro
235234
assertEquals("information collected", informational.getValue());
236235
}
237236

238-
}
237+
}

0 commit comments

Comments
 (0)