From ee86e6b6adec22c32a5fba4d7ad5aa03d8937583 Mon Sep 17 00:00:00 2001
From: Doha2012 <dohae243@gmail.com>
Date: Mon, 8 Jan 2018 23:39:01 +0200
Subject: [PATCH] fix blocking tests (#3378)

* make sure modules using java8

* move url matching code

* upgrade boot parent

* minor cleanup

* fix blocking tests
---
 .../concurrent/stopping/StopThreadTest.java   | 18 ++++---
 ...estOne.java => EthControllerLiveTest.java} | 19 ++++---
 .../baeldung/guava/GuavaCacheUnitTest.java    |  7 +--
 .../micrometer/MicrometerAtlasTest.java       | 45 ++++++++++------
 .../metrics/servo/MetricTypeTest.java         | 52 ++++++++++---------
 ... => CabBookingServiceIntegrationTest.java} |  4 +-
 6 files changed, 85 insertions(+), 60 deletions(-)
 rename ethereumj/src/test/java/com/baeldung/ethereumj/controllers/{EthControllerTestOne.java => EthControllerLiveTest.java} (90%)
 rename spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/{CabBookingServiceTest.java => CabBookingServiceIntegrationTest.java} (97%)

diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java
index 70854f013f..af54d6932e 100644
--- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java
+++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java
@@ -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);
     }
diff --git a/ethereumj/src/test/java/com/baeldung/ethereumj/controllers/EthControllerTestOne.java b/ethereumj/src/test/java/com/baeldung/ethereumj/controllers/EthControllerLiveTest.java
similarity index 90%
rename from ethereumj/src/test/java/com/baeldung/ethereumj/controllers/EthControllerTestOne.java
rename to ethereumj/src/test/java/com/baeldung/ethereumj/controllers/EthControllerLiveTest.java
index 9298c34ec2..f62d229261 100644
--- a/ethereumj/src/test/java/com/baeldung/ethereumj/controllers/EthControllerTestOne.java
+++ b/ethereumj/src/test/java/com/baeldung/ethereumj/controllers/EthControllerLiveTest.java
@@ -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;
diff --git a/guava/src/test/java/org/baeldung/guava/GuavaCacheUnitTest.java b/guava/src/test/java/org/baeldung/guava/GuavaCacheUnitTest.java
index eb67d8af44..49ce6b1a09 100644
--- a/guava/src/test/java/org/baeldung/guava/GuavaCacheUnitTest.java
+++ b/guava/src/test/java/org/baeldung/guava/GuavaCacheUnitTest.java
@@ -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);
     }
 
diff --git a/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasTest.java b/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasTest.java
index 826e06d598..b76dc40ba0 100644
--- a/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasTest.java
+++ b/metrics/src/test/java/com/baeldung/metrics/micrometer/MicrometerAtlasTest.java
@@ -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
diff --git a/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeTest.java b/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeTest.java
index 99009f8d84..237092b1c3 100644
--- a/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeTest.java
+++ b/metrics/src/test/java/com/baeldung/metrics/servo/MetricTypeTest.java
@@ -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()
diff --git a/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java b/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java
similarity index 97%
rename from spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java
rename to spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java
index 373701f714..a1fed9637f 100644
--- a/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java
+++ b/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceIntegrationTest.java
@@ -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;