fix blocking tests (#3378)

* make sure modules using java8

* move url matching code

* upgrade boot parent

* minor cleanup

* fix blocking tests
This commit is contained in:
Doha2012 2018-01-08 23:39:01 +02:00 committed by Grzegorz Piwowarek
parent cbd1a9dfbf
commit ee86e6b6ad
6 changed files with 85 additions and 60 deletions

View File

@ -1,20 +1,21 @@
package com.baeldung.concurrent.stopping; package com.baeldung.concurrent.stopping;
import com.jayway.awaitility.Awaitility;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
import static com.jayway.awaitility.Awaitility.await; import static com.jayway.awaitility.Awaitility.await;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import com.jayway.awaitility.Awaitility;
public class StopThreadTest { public class StopThreadTest {
@Test @Test
public void whenStoppedThreadIsStopped() throws InterruptedException { public void whenStoppedThreadIsStopped() throws InterruptedException {
int interval = 100; int interval = 5;
ControlSubThread controlSubThread = new ControlSubThread(interval); ControlSubThread controlSubThread = new ControlSubThread(interval);
controlSubThread.start(); controlSubThread.start();
@ -33,13 +34,13 @@ public class StopThreadTest {
@Test @Test
public void whenInterruptedThreadIsStopped() throws InterruptedException { public void whenInterruptedThreadIsStopped() throws InterruptedException {
int interval = 5000; int interval = 50;
ControlSubThread controlSubThread = new ControlSubThread(interval); ControlSubThread controlSubThread = new ControlSubThread(interval);
controlSubThread.start(); controlSubThread.start();
// Give things a chance to get set up // Give things a chance to get set up
Thread.sleep(100); Thread.sleep(interval);
assertTrue(controlSubThread.isRunning()); assertTrue(controlSubThread.isRunning());
assertFalse(controlSubThread.isStopped()); assertFalse(controlSubThread.isStopped());
@ -48,6 +49,7 @@ public class StopThreadTest {
// Wait less than the time we would normally sleep, and make sure we exited. // Wait less than the time we would normally sleep, and make sure we exited.
Awaitility.await() Awaitility.await()
.pollDelay(2, TimeUnit.MILLISECONDS)
.atMost(interval/ 10, TimeUnit.MILLISECONDS) .atMost(interval/ 10, TimeUnit.MILLISECONDS)
.until(controlSubThread::isStopped); .until(controlSubThread::isStopped);
} }

View File

@ -1,25 +1,30 @@
package com.baeldung.ethereumj.controllers; package com.baeldung.ethereumj.controllers;
import com.baeldung.ethereumj.ApplicationMain; import static junit.framework.TestCase.assertTrue;
import com.baeldung.ethereumj.Constants; import static org.junit.Assert.assertNotNull;
import com.baeldung.ethereumj.transfer.EthResponse;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import static junit.framework.TestCase.assertTrue; import com.baeldung.ethereumj.ApplicationMain;
import static org.junit.Assert.assertNotNull; import com.baeldung.ethereumj.Constants;
import com.baeldung.ethereumj.transfer.EthResponse;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationMain.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @SpringBootTest(classes = ApplicationMain.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = "server.port=8080") @TestPropertySource(properties = "server.port=8080")
public class EthControllerTestOne { public class EthControllerLiveTest {
@LocalServerPort @LocalServerPort
int port; int port;

View File

@ -89,7 +89,7 @@ public class GuavaCacheUnitTest {
cache.getUnchecked("hello"); cache.getUnchecked("hello");
assertEquals(1, cache.size()); assertEquals(1, cache.size());
cache.getUnchecked("hello"); cache.getUnchecked("hello");
Thread.sleep(300); Thread.sleep(3);
cache.getUnchecked("test"); cache.getUnchecked("test");
assertEquals(1, cache.size()); assertEquals(1, cache.size());
assertNull(cache.getIfPresent("hello")); assertNull(cache.getIfPresent("hello"));
@ -106,7 +106,7 @@ public class GuavaCacheUnitTest {
final LoadingCache<String, String> cache = CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.MILLISECONDS).build(loader); final LoadingCache<String, String> cache = CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.MILLISECONDS).build(loader);
cache.getUnchecked("hello"); cache.getUnchecked("hello");
assertEquals(1, cache.size()); assertEquals(1, cache.size());
Thread.sleep(300); Thread.sleep(3);
cache.getUnchecked("test"); cache.getUnchecked("test");
assertEquals(1, cache.size()); assertEquals(1, cache.size());
assertNull(cache.getIfPresent("hello")); assertNull(cache.getIfPresent("hello"));
@ -203,8 +203,9 @@ public class GuavaCacheUnitTest {
private String getSuffix(final String str) { private String getSuffix(final String str) {
final int lastIndex = str.lastIndexOf('.'); final int lastIndex = str.lastIndexOf('.');
if (lastIndex == -1) if (lastIndex == -1) {
return null; return null;
}
return str.substring(lastIndex + 1); return str.substring(lastIndex + 1);
} }

View File

@ -1,27 +1,40 @@
package com.baeldung.metrics.micrometer; package com.baeldung.metrics.micrometer;
import com.netflix.spectator.atlas.AtlasConfig; import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import io.micrometer.atlas.AtlasMeterRegistry; import io.micrometer.atlas.AtlasMeterRegistry;
import io.micrometer.core.instrument.*; import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.LongTaskTimer;
import io.micrometer.core.instrument.Measurement;
import io.micrometer.core.instrument.Meter.Type;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Timer; import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry; import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.micrometer.core.instrument.stats.hist.Histogram; import io.micrometer.core.instrument.stats.hist.Histogram;
import io.micrometer.core.instrument.stats.quantile.WindowSketchQuantiles; import io.micrometer.core.instrument.stats.quantile.WindowSketchQuantiles;
import org.junit.Before;
import org.junit.Test;
import java.time.Duration; import java.time.Duration;
import java.util.*; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static io.micrometer.core.instrument.Meter.Type; import org.junit.Before;
import static org.hamcrest.CoreMatchers.*; import org.junit.Test;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.hamcrest.core.IsCollectionContaining.hasItems; import com.netflix.spectator.atlas.AtlasConfig;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/** /**
* @author aiet * @author aiet
@ -135,15 +148,15 @@ public class MicrometerAtlasTest {
Timer timer = registry.timer("app.event"); Timer timer = registry.timer("app.event");
timer.record(() -> { timer.record(() -> {
try { try {
TimeUnit.MILLISECONDS.sleep(1500); TimeUnit.MILLISECONDS.sleep(15);
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
}); });
timer.record(3000, TimeUnit.MILLISECONDS); timer.record(30, TimeUnit.MILLISECONDS);
assertTrue(2 == timer.count()); assertTrue(2 == timer.count());
assertTrue(4510 > timer.totalTime(TimeUnit.MILLISECONDS) && 4500 <= timer.totalTime(TimeUnit.MILLISECONDS)); assertTrue(50 > timer.totalTime(TimeUnit.MILLISECONDS) && 45 <= timer.totalTime(TimeUnit.MILLISECONDS));
} }
@Test @Test
@ -155,12 +168,12 @@ public class MicrometerAtlasTest {
long currentTaskId = longTaskTimer.start(); long currentTaskId = longTaskTimer.start();
try { try {
TimeUnit.SECONDS.sleep(2); TimeUnit.MILLISECONDS.sleep(2);
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
long timeElapsed = longTaskTimer.stop(currentTaskId); long timeElapsed = longTaskTimer.stop(currentTaskId);
assertTrue(timeElapsed / (int) 1e9 == 2); assertTrue(timeElapsed / (int) 1e6 == 2);
} }
@Test @Test

View File

@ -1,5 +1,19 @@
package com.baeldung.metrics.servo; package com.baeldung.metrics.servo;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toMap;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import com.netflix.servo.monitor.BasicCounter; import com.netflix.servo.monitor.BasicCounter;
import com.netflix.servo.monitor.BasicGauge; import com.netflix.servo.monitor.BasicGauge;
import com.netflix.servo.monitor.BasicInformational; import com.netflix.servo.monitor.BasicInformational;
@ -17,18 +31,6 @@ import com.netflix.servo.monitor.StatsTimer;
import com.netflix.servo.monitor.StepCounter; import com.netflix.servo.monitor.StepCounter;
import com.netflix.servo.monitor.Stopwatch; import com.netflix.servo.monitor.Stopwatch;
import com.netflix.servo.stats.StatsConfig; import com.netflix.servo.stats.StatsConfig;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Map;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toMap;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class MetricTypeTest { public class MetricTypeTest {
@ -104,17 +106,18 @@ public class MetricTypeTest {
public void givenTimer_whenExecuteTask_thenTimerUpdated() throws Exception { public void givenTimer_whenExecuteTask_thenTimerUpdated() throws Exception {
BasicTimer timer = new BasicTimer(MonitorConfig BasicTimer timer = new BasicTimer(MonitorConfig
.builder("test") .builder("test")
.build(), SECONDS); .build(), MILLISECONDS);
Stopwatch stopwatch = timer.start(); Stopwatch stopwatch = timer.start();
SECONDS.sleep(1); MILLISECONDS.sleep(1);
timer.record(2, SECONDS); timer.record(2, MILLISECONDS);
stopwatch.stop(); stopwatch.stop();
assertEquals("timer should count 1 second", 1, timer assertEquals("timer should count 1 millisecond", 1, timer
.getValue() .getValue()
.intValue()); .intValue());
assertEquals("timer should count 3 seconds in total", 3.0, timer.getTotalTime(), 0.01); assertEquals("timer should count 3 millisecond in total", 3, timer.getTotalTime()
.intValue());
assertEquals("timer should record 2 updates", 2, timer assertEquals("timer should record 2 updates", 2, timer
.getCount() .getCount()
.intValue()); .intValue());
@ -158,6 +161,7 @@ public class MetricTypeTest {
} }
@Test @Test
//==
public void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Exception { public void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Exception {
System.setProperty("netflix.servo", "1000"); System.setProperty("netflix.servo", "1000");
StatsTimer timer = new StatsTimer(MonitorConfig StatsTimer timer = new StatsTimer(MonitorConfig
@ -171,20 +175,20 @@ public class MetricTypeTest {
.withPublishMean(true) .withPublishMean(true)
.withPublishStdDev(true) .withPublishStdDev(true)
.withPublishVariance(true) .withPublishVariance(true)
.build(), SECONDS); .build(), MILLISECONDS);
Stopwatch stopwatch = timer.start(); Stopwatch stopwatch = timer.start();
SECONDS.sleep(1); MILLISECONDS.sleep(1);
timer.record(3, SECONDS); timer.record(3, MILLISECONDS);
stopwatch.stop(); stopwatch.stop();
stopwatch = timer.start(); stopwatch = timer.start();
timer.record(6, SECONDS); timer.record(6, MILLISECONDS);
SECONDS.sleep(2); MILLISECONDS.sleep(2);
stopwatch.stop(); stopwatch.stop();
assertEquals("timer should count 12 seconds in total", 12, timer.getTotalTime()); assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalTime());
assertEquals("timer should count 12 seconds in total", 12, timer.getTotalMeasurement()); assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalMeasurement());
assertEquals("timer should record 4 updates", 4, timer.getCount()); assertEquals("timer should record 4 updates", 4, timer.getCount());
assertEquals("stats timer value time-cost/update should be 2", 3, timer assertEquals("stats timer value time-cost/update should be 2", 3, timer
.getValue() .getValue()

View File

@ -18,9 +18,9 @@ import static java.lang.Thread.sleep;
@SpringBootTest(classes = {BurlapClient.class, HessianClient.class}) @SpringBootTest(classes = {BurlapClient.class, HessianClient.class})
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class CabBookingServiceTest { public class CabBookingServiceIntegrationTest {
static Logger log = LoggerFactory.getLogger(CabBookingServiceTest.class); static Logger log = LoggerFactory.getLogger(CabBookingServiceIntegrationTest.class);
@Autowired @Qualifier("burlapInvoker") CabBookingService burlapClient; @Autowired @Qualifier("burlapInvoker") CabBookingService burlapClient;
@Autowired @Qualifier("hessianInvoker") CabBookingService hessianClient; @Autowired @Qualifier("hessianInvoker") CabBookingService hessianClient;
static Thread serverThread; static Thread serverThread;