Skip to content

Commit a45716f

Browse files
committed
fix blocking tests
1 parent da77f09 commit a45716f

6 files changed

Lines changed: 85 additions & 60 deletions

File tree

‎core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package com.baeldung.concurrent.stopping;
22

3-
import com.jayway.awaitility.Awaitility;
4-
import org.junit.Test;
5-
6-
import java.util.concurrent.TimeUnit;
7-
83
import static com.jayway.awaitility.Awaitility.await;
94
import static org.junit.Assert.assertFalse;
105
import static org.junit.Assert.assertTrue;
116

7+
import java.util.concurrent.TimeUnit;
8+
9+
import org.junit.Test;
10+
11+
import com.jayway.awaitility.Awaitility;
12+
1213
public class StopThreadTest {
1314

1415
@Test
1516
public void whenStoppedThreadIsStopped() throws InterruptedException {
1617

17-
int interval = 100;
18+
int interval = 5;
1819

1920
ControlSubThread controlSubThread = new ControlSubThread(interval);
2021
controlSubThread.start();
@@ -33,13 +34,13 @@ public void whenStoppedThreadIsStopped() throws InterruptedException {
3334
@Test
3435
public void whenInterruptedThreadIsStopped() throws InterruptedException {
3536

36-
int interval = 5000;
37+
int interval = 50;
3738

3839
ControlSubThread controlSubThread = new ControlSubThread(interval);
3940
controlSubThread.start();
4041

4142
// Give things a chance to get set up
42-
Thread.sleep(100);
43+
Thread.sleep(interval);
4344
assertTrue(controlSubThread.isRunning());
4445
assertFalse(controlSubThread.isStopped());
4546

@@ -48,6 +49,7 @@ public void whenInterruptedThreadIsStopped() throws InterruptedException {
4849

4950
// Wait less than the time we would normally sleep, and make sure we exited.
5051
Awaitility.await()
52+
.pollDelay(2, TimeUnit.MILLISECONDS)
5153
.atMost(interval/ 10, TimeUnit.MILLISECONDS)
5254
.until(controlSubThread::isStopped);
5355
}

ethereumj/src/test/java/com/baeldung/ethereumj/controllers/EthControllerTestOne.java renamed to ethereumj/src/test/java/com/baeldung/ethereumj/controllers/EthControllerLiveTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
package com.baeldung.ethereumj.controllers;
22

3-
import com.baeldung.ethereumj.ApplicationMain;
4-
import com.baeldung.ethereumj.Constants;
5-
import com.baeldung.ethereumj.transfer.EthResponse;
3+
import static junit.framework.TestCase.assertTrue;
4+
import static org.junit.Assert.assertNotNull;
5+
66
import org.junit.Before;
77
import org.junit.Test;
88
import org.junit.runner.RunWith;
99
import org.springframework.boot.context.embedded.LocalServerPort;
1010
import org.springframework.boot.test.context.SpringBootTest;
11-
import org.springframework.http.*;
11+
import org.springframework.http.HttpEntity;
12+
import org.springframework.http.HttpHeaders;
13+
import org.springframework.http.HttpMethod;
14+
import org.springframework.http.HttpStatus;
15+
import org.springframework.http.ResponseEntity;
1216
import org.springframework.test.context.TestPropertySource;
1317
import org.springframework.test.context.junit4.SpringRunner;
1418
import org.springframework.web.client.RestTemplate;
1519

16-
import static junit.framework.TestCase.assertTrue;
17-
import static org.junit.Assert.assertNotNull;
20+
import com.baeldung.ethereumj.ApplicationMain;
21+
import com.baeldung.ethereumj.Constants;
22+
import com.baeldung.ethereumj.transfer.EthResponse;
1823

1924
@RunWith(SpringRunner.class)
2025
@SpringBootTest(classes = ApplicationMain.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
2126
@TestPropertySource(properties = "server.port=8080")
22-
public class EthControllerTestOne {
27+
public class EthControllerLiveTest {
2328

2429
@LocalServerPort
2530
int port;

‎guava/src/test/java/org/baeldung/guava/GuavaCacheUnitTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public final String load(final String key) {
8989
cache.getUnchecked("hello");
9090
assertEquals(1, cache.size());
9191
cache.getUnchecked("hello");
92-
Thread.sleep(300);
92+
Thread.sleep(3);
9393
cache.getUnchecked("test");
9494
assertEquals(1, cache.size());
9595
assertNull(cache.getIfPresent("hello"));
@@ -106,7 +106,7 @@ public final String load(final String key) {
106106
final LoadingCache<String, String> cache = CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.MILLISECONDS).build(loader);
107107
cache.getUnchecked("hello");
108108
assertEquals(1, cache.size());
109-
Thread.sleep(300);
109+
Thread.sleep(3);
110110
cache.getUnchecked("test");
111111
assertEquals(1, cache.size());
112112
assertNull(cache.getIfPresent("hello"));
@@ -203,8 +203,9 @@ public void onRemoval(final RemovalNotification<String, String> n) {
203203

204204
private String getSuffix(final String str) {
205205
final int lastIndex = str.lastIndexOf('.');
206-
if (lastIndex == -1)
206+
if (lastIndex == -1) {
207207
return null;
208+
}
208209
return str.substring(lastIndex + 1);
209210
}
210211

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

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
package com.baeldung.metrics.micrometer;
22

3-
import com.netflix.spectator.atlas.AtlasConfig;
3+
import static org.hamcrest.CoreMatchers.allOf;
4+
import static org.hamcrest.CoreMatchers.equalTo;
5+
import static org.hamcrest.collection.IsMapContaining.hasEntry;
6+
import static org.junit.Assert.assertThat;
7+
import static org.junit.Assert.assertTrue;
48
import io.micrometer.atlas.AtlasMeterRegistry;
5-
import io.micrometer.core.instrument.*;
9+
import io.micrometer.core.instrument.Clock;
10+
import io.micrometer.core.instrument.Counter;
11+
import io.micrometer.core.instrument.DistributionSummary;
12+
import io.micrometer.core.instrument.Gauge;
13+
import io.micrometer.core.instrument.LongTaskTimer;
14+
import io.micrometer.core.instrument.Measurement;
15+
import io.micrometer.core.instrument.Meter.Type;
16+
import io.micrometer.core.instrument.MeterRegistry;
17+
import io.micrometer.core.instrument.Metrics;
18+
import io.micrometer.core.instrument.Tag;
619
import io.micrometer.core.instrument.Timer;
720
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
821
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
922
import io.micrometer.core.instrument.stats.hist.Histogram;
1023
import io.micrometer.core.instrument.stats.quantile.WindowSketchQuantiles;
11-
import org.junit.Before;
12-
import org.junit.Test;
1324

1425
import java.time.Duration;
15-
import java.util.*;
26+
import java.util.ArrayList;
27+
import java.util.Iterator;
28+
import java.util.List;
29+
import java.util.Map;
30+
import java.util.Optional;
1631
import java.util.concurrent.TimeUnit;
1732
import java.util.stream.Collectors;
1833

19-
import static io.micrometer.core.instrument.Meter.Type;
20-
import static org.hamcrest.CoreMatchers.*;
21-
import static org.hamcrest.collection.IsMapContaining.hasEntry;
22-
import static org.hamcrest.core.IsCollectionContaining.hasItems;
23-
import static org.junit.Assert.assertThat;
24-
import static org.junit.Assert.assertTrue;
34+
import org.junit.Before;
35+
import org.junit.Test;
36+
37+
import com.netflix.spectator.atlas.AtlasConfig;
2538

2639
/**
2740
* @author aiet
@@ -135,15 +148,15 @@ public void givenTimer_whenWrapTasks_thenTimeRecorded() {
135148
Timer timer = registry.timer("app.event");
136149
timer.record(() -> {
137150
try {
138-
TimeUnit.MILLISECONDS.sleep(1500);
151+
TimeUnit.MILLISECONDS.sleep(15);
139152
} catch (InterruptedException ignored) {
140153
}
141154
});
142155

143-
timer.record(3000, TimeUnit.MILLISECONDS);
156+
timer.record(30, TimeUnit.MILLISECONDS);
144157

145158
assertTrue(2 == timer.count());
146-
assertTrue(4510 > timer.totalTime(TimeUnit.MILLISECONDS) && 4500 <= timer.totalTime(TimeUnit.MILLISECONDS));
159+
assertTrue(50 > timer.totalTime(TimeUnit.MILLISECONDS) && 45 <= timer.totalTime(TimeUnit.MILLISECONDS));
147160
}
148161

149162
@Test
@@ -155,12 +168,12 @@ public void givenLongTimer_whenRunTasks_thenTimerRecorded() {
155168

156169
long currentTaskId = longTaskTimer.start();
157170
try {
158-
TimeUnit.SECONDS.sleep(2);
171+
TimeUnit.MILLISECONDS.sleep(2);
159172
} catch (InterruptedException ignored) {
160173
}
161174
long timeElapsed = longTaskTimer.stop(currentTaskId);
162175

163-
assertTrue(timeElapsed / (int) 1e9 == 2);
176+
assertTrue(timeElapsed / (int) 1e6 == 2);
164177
}
165178

166179
@Test

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package com.baeldung.metrics.servo;
22

3+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4+
import static java.util.concurrent.TimeUnit.SECONDS;
5+
import static java.util.stream.Collectors.toMap;
6+
import static org.hamcrest.Matchers.allOf;
7+
import static org.hamcrest.Matchers.containsInAnyOrder;
8+
import static org.hamcrest.Matchers.hasEntry;
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertThat;
11+
12+
import java.util.Map;
13+
14+
import org.junit.Ignore;
15+
import org.junit.Test;
16+
317
import com.netflix.servo.monitor.BasicCounter;
418
import com.netflix.servo.monitor.BasicGauge;
519
import com.netflix.servo.monitor.BasicInformational;
@@ -17,18 +31,6 @@
1731
import com.netflix.servo.monitor.StepCounter;
1832
import com.netflix.servo.monitor.Stopwatch;
1933
import com.netflix.servo.stats.StatsConfig;
20-
import org.junit.Ignore;
21-
import org.junit.Test;
22-
23-
import java.util.Map;
24-
25-
import static java.util.concurrent.TimeUnit.SECONDS;
26-
import static java.util.stream.Collectors.toMap;
27-
import static org.hamcrest.Matchers.allOf;
28-
import static org.hamcrest.Matchers.containsInAnyOrder;
29-
import static org.hamcrest.Matchers.hasEntry;
30-
import static org.junit.Assert.assertEquals;
31-
import static org.junit.Assert.assertThat;
3234

3335
public class MetricTypeTest {
3436

@@ -104,17 +106,18 @@ public void givenPeakRateCounter_whenManipulate_thenPeakRateReturn() throws Exce
104106
public void givenTimer_whenExecuteTask_thenTimerUpdated() throws Exception {
105107
BasicTimer timer = new BasicTimer(MonitorConfig
106108
.builder("test")
107-
.build(), SECONDS);
109+
.build(), MILLISECONDS);
108110

109111
Stopwatch stopwatch = timer.start();
110-
SECONDS.sleep(1);
111-
timer.record(2, SECONDS);
112+
MILLISECONDS.sleep(1);
113+
timer.record(2, MILLISECONDS);
112114
stopwatch.stop();
113115

114-
assertEquals("timer should count 1 second", 1, timer
116+
assertEquals("timer should count 1 millisecond", 1, timer
115117
.getValue()
116118
.intValue());
117-
assertEquals("timer should count 3 seconds in total", 3.0, timer.getTotalTime(), 0.01);
119+
assertEquals("timer should count 3 millisecond in total", 3, timer.getTotalTime()
120+
.intValue());
118121
assertEquals("timer should record 2 updates", 2, timer
119122
.getCount()
120123
.intValue());
@@ -158,6 +161,7 @@ private static String getMonitorTagValue(Monitor monitor, String tagKey) {
158161
}
159162

160163
@Test
164+
//==
161165
public void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Exception {
162166
System.setProperty("netflix.servo", "1000");
163167
StatsTimer timer = new StatsTimer(MonitorConfig
@@ -171,20 +175,20 @@ public void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Excepti
171175
.withPublishMean(true)
172176
.withPublishStdDev(true)
173177
.withPublishVariance(true)
174-
.build(), SECONDS);
178+
.build(), MILLISECONDS);
175179

176180
Stopwatch stopwatch = timer.start();
177-
SECONDS.sleep(1);
178-
timer.record(3, SECONDS);
181+
MILLISECONDS.sleep(1);
182+
timer.record(3, MILLISECONDS);
179183
stopwatch.stop();
180184

181185
stopwatch = timer.start();
182-
timer.record(6, SECONDS);
183-
SECONDS.sleep(2);
186+
timer.record(6, MILLISECONDS);
187+
MILLISECONDS.sleep(2);
184188
stopwatch.stop();
185189

186-
assertEquals("timer should count 12 seconds in total", 12, timer.getTotalTime());
187-
assertEquals("timer should count 12 seconds in total", 12, timer.getTotalMeasurement());
190+
assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalTime());
191+
assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalMeasurement());
188192
assertEquals("timer should record 4 updates", 4, timer.getCount());
189193
assertEquals("stats timer value time-cost/update should be 2", 3, timer
190194
.getValue()

spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java renamed to spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
@SpringBootTest(classes = {BurlapClient.class, HessianClient.class})
2020
@RunWith(SpringRunner.class)
21-
public class CabBookingServiceTest {
21+
public class CabBookingServiceIntegrationTest {
2222

23-
static Logger log = LoggerFactory.getLogger(CabBookingServiceTest.class);
23+
static Logger log = LoggerFactory.getLogger(CabBookingServiceIntegrationTest.class);
2424
@Autowired @Qualifier("burlapInvoker") CabBookingService burlapClient;
2525
@Autowired @Qualifier("hessianInvoker") CabBookingService hessianClient;
2626
static Thread serverThread;

0 commit comments

Comments
 (0)