fix blocking tests

This commit is contained in:
DOHA 2018-01-08 23:14:58 +02:00
parent da77f09e88
commit a45716fe48
6 changed files with 85 additions and 60 deletions

View File

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

View File

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

View File

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

View File

@ -1,27 +1,40 @@
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.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.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.micrometer.core.instrument.stats.hist.Histogram;
import io.micrometer.core.instrument.stats.quantile.WindowSketchQuantiles;
import org.junit.Before;
import org.junit.Test;
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.stream.Collectors;
import static io.micrometer.core.instrument.Meter.Type;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import com.netflix.spectator.atlas.AtlasConfig;
/**
* @author aiet
@ -135,15 +148,15 @@ public class MicrometerAtlasTest {
Timer timer = registry.timer("app.event");
timer.record(() -> {
try {
TimeUnit.MILLISECONDS.sleep(1500);
TimeUnit.MILLISECONDS.sleep(15);
} catch (InterruptedException ignored) {
}
});
timer.record(3000, TimeUnit.MILLISECONDS);
timer.record(30, TimeUnit.MILLISECONDS);
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
@ -155,12 +168,12 @@ public class MicrometerAtlasTest {
long currentTaskId = longTaskTimer.start();
try {
TimeUnit.SECONDS.sleep(2);
TimeUnit.MILLISECONDS.sleep(2);
} catch (InterruptedException ignored) {
}
long timeElapsed = longTaskTimer.stop(currentTaskId);
assertTrue(timeElapsed / (int) 1e9 == 2);
assertTrue(timeElapsed / (int) 1e6 == 2);
}
@Test

View File

@ -1,5 +1,19 @@
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.BasicGauge;
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.Stopwatch;
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 {
@ -104,17 +106,18 @@ public class MetricTypeTest {
public void givenTimer_whenExecuteTask_thenTimerUpdated() throws Exception {
BasicTimer timer = new BasicTimer(MonitorConfig
.builder("test")
.build(), SECONDS);
.build(), MILLISECONDS);
Stopwatch stopwatch = timer.start();
SECONDS.sleep(1);
timer.record(2, SECONDS);
MILLISECONDS.sleep(1);
timer.record(2, MILLISECONDS);
stopwatch.stop();
assertEquals("timer should count 1 second", 1, timer
assertEquals("timer should count 1 millisecond", 1, timer
.getValue()
.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
.getCount()
.intValue());
@ -158,6 +161,7 @@ public class MetricTypeTest {
}
@Test
//==
public void givenStatsTimer_whenExecuteTask_thenStatsCalculated() throws Exception {
System.setProperty("netflix.servo", "1000");
StatsTimer timer = new StatsTimer(MonitorConfig
@ -171,20 +175,20 @@ public class MetricTypeTest {
.withPublishMean(true)
.withPublishStdDev(true)
.withPublishVariance(true)
.build(), SECONDS);
.build(), MILLISECONDS);
Stopwatch stopwatch = timer.start();
SECONDS.sleep(1);
timer.record(3, SECONDS);
MILLISECONDS.sleep(1);
timer.record(3, MILLISECONDS);
stopwatch.stop();
stopwatch = timer.start();
timer.record(6, SECONDS);
SECONDS.sleep(2);
timer.record(6, MILLISECONDS);
MILLISECONDS.sleep(2);
stopwatch.stop();
assertEquals("timer should count 12 seconds 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.getTotalTime());
assertEquals("timer should count 12 milliseconds in total", 12, timer.getTotalMeasurement());
assertEquals("timer should record 4 updates", 4, timer.getCount());
assertEquals("stats timer value time-cost/update should be 2", 3, timer
.getValue()

View File

@ -18,9 +18,9 @@ import static java.lang.Thread.sleep;
@SpringBootTest(classes = {BurlapClient.class, HessianClient.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("hessianInvoker") CabBookingService hessianClient;
static Thread serverThread;