You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2021/05/27 09:08:37 UTC

[plc4x] branch develop updated: plc4j: replaced Thread.sleep() with timer().schedule()

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new afb297d  plc4j: replaced Thread.sleep() with timer().schedule()
afb297d is described below

commit afb297dec41437360d35a22a390841f6c8f41428
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu May 27 11:08:23 2021 +0200

    plc4j: replaced Thread.sleep() with timer().schedule()
---
 .../connectionpool2/CachedPlcConnectionTest.java   | 20 ++++++++----------
 .../triggeredscraper/TriggeredScraperImplTest.java | 24 +++++++++-------------
 2 files changed, 18 insertions(+), 26 deletions(-)

diff --git a/plc4j/tools/connection-cache/src/test/java/org/apache/plc4x/java/utils/connectionpool2/CachedPlcConnectionTest.java b/plc4j/tools/connection-cache/src/test/java/org/apache/plc4x/java/utils/connectionpool2/CachedPlcConnectionTest.java
index ce081e0..4dbbd5d 100644
--- a/plc4j/tools/connection-cache/src/test/java/org/apache/plc4x/java/utils/connectionpool2/CachedPlcConnectionTest.java
+++ b/plc4j/tools/connection-cache/src/test/java/org/apache/plc4x/java/utils/connectionpool2/CachedPlcConnectionTest.java
@@ -21,20 +21,14 @@ package org.apache.plc4x.java.utils.connectionpool2;
 
 import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.messages.PlcReadResponse;
-import org.awaitility.Duration;
 import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
+import java.util.Timer;
+import java.util.TimerTask;
 import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
 
-import static org.awaitility.Awaitility.await;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -47,10 +41,12 @@ class CachedPlcConnectionTest {
 
         when(mockConnection.readRequestBuilder().build().execute()).thenAnswer(invocation -> {
             final CompletableFuture<? extends PlcReadResponse> future = new CompletableFuture<>();
-            // TODO: use the await below
-            await().until(() -> true);
-            TimeUnit.SECONDS.sleep(2);
-            future.completeExceptionally(new RuntimeException("abc"));
+            new Timer().schedule(new TimerTask() {
+                @Override
+                public void run() {
+                    future.completeExceptionally(new RuntimeException("abc"));
+                }
+            }, TimeUnit.SECONDS.toMillis(2));
             return future;
         });
 
diff --git a/plc4j/tools/scraper/src/test/java/org/apache/plc4x/java/scraper/triggeredscraper/TriggeredScraperImplTest.java b/plc4j/tools/scraper/src/test/java/org/apache/plc4x/java/scraper/triggeredscraper/TriggeredScraperImplTest.java
index be29881..cc9817b 100644
--- a/plc4j/tools/scraper/src/test/java/org/apache/plc4x/java/scraper/triggeredscraper/TriggeredScraperImplTest.java
+++ b/plc4j/tools/scraper/src/test/java/org/apache/plc4x/java/scraper/triggeredscraper/TriggeredScraperImplTest.java
@@ -21,8 +21,6 @@ package org.apache.plc4x.java.scraper.triggeredscraper;
 
 import org.apache.plc4x.java.PlcDriverManager;
 import org.apache.plc4x.java.api.types.PlcResponseCode;
-import org.apache.plc4x.java.spi.values.PlcBOOL;
-import org.apache.plc4x.java.spi.values.PlcLINT;
 import org.apache.plc4x.java.mock.connection.MockConnection;
 import org.apache.plc4x.java.mock.connection.MockDevice;
 import org.apache.plc4x.java.scraper.config.ScraperConfiguration;
@@ -31,21 +29,18 @@ import org.apache.plc4x.java.scraper.exception.ScraperException;
 import org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollector;
 import org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollectorImpl;
 import org.apache.plc4x.java.spi.messages.utils.ResponseItem;
+import org.apache.plc4x.java.spi.values.PlcBOOL;
+import org.apache.plc4x.java.spi.values.PlcLINT;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
-import java.sql.Time;
 import java.util.Random;
+import java.util.Timer;
+import java.util.TimerTask;
 import java.util.concurrent.TimeUnit;
 
-import static org.awaitility.Awaitility.await;
-import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.when;
 
 class TriggeredScraperImplTest {
@@ -100,10 +95,11 @@ class TriggeredScraperImplTest {
 
         scraper.start();
 
-        // TODO: use the await below
-        await().until(() -> true);
-        TimeUnit.SECONDS.sleep(2);
-
-        scraper.stop();
+        new Timer().schedule(new TimerTask() {
+            @Override
+            public void run() {
+                scraper.stop();
+            }
+        }, TimeUnit.SECONDS.toMillis(2));
     }
 }
\ No newline at end of file