You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2018/11/24 20:35:55 UTC

[incubator-plc4x] 05/09: [plc4j-scraper] Added tests.

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

jfeinauer pushed a commit to branch feature/plc4j-scraper
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 79c06aa90f785689d19a656c6b577be4f9311320
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Sat Nov 24 13:14:44 2018 +0100

    [plc4j-scraper] Added tests.
---
 plc4j/utils/scraper/pom.xml                        |  8 +-
 .../org/apache/plc4x/java/scraper/Scraper.java     | 90 ++++++++++++++++++++++
 .../apache/plc4x/java/s7/ManualS7PlcDriverMT.java  | 52 +++++++++++--
 .../org/apache/plc4x/java/scraper/ScraperTest.java | 76 ++++++++++++++++++
 4 files changed, 220 insertions(+), 6 deletions(-)

diff --git a/plc4j/utils/scraper/pom.xml b/plc4j/utils/scraper/pom.xml
index 9b2e372..b88e8a2 100644
--- a/plc4j/utils/scraper/pom.xml
+++ b/plc4j/utils/scraper/pom.xml
@@ -49,7 +49,13 @@
     </dependency>
     <dependency>
       <groupId>org.apache.plc4x</groupId>
-      <artifactId>plc4j-connection-pool</artifactId>
+      <artifactId>plc4j-connectionString-pool</artifactId>
+      <version>0.3.0-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-protocol-test</artifactId>
       <version>0.3.0-SNAPSHOT</version>
       <scope>test</scope>
     </dependency>
diff --git a/plc4j/utils/scraper/src/main/java/org/apache/plc4x/java/scraper/Scraper.java b/plc4j/utils/scraper/src/main/java/org/apache/plc4x/java/scraper/Scraper.java
new file mode 100644
index 0000000..dfd4801
--- /dev/null
+++ b/plc4j/utils/scraper/src/main/java/org/apache/plc4x/java/scraper/Scraper.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.plc4x.java.scraper;
+
+import org.apache.plc4x.java.PlcDriverManager;
+import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
+import org.apache.plc4x.java.api.messages.PlcReadResponse;
+
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+/**
+ * Plc Scraper that scrapes one source.
+ */
+public class Scraper implements Runnable {
+
+    private final PlcDriverManager driverManager;
+    private final String connectionString;
+    private final long requestTimeoutMs;
+    private final ResultHandler handler;
+
+    public Scraper(PlcDriverManager driverManager, String connectionString, long requestTimeoutMs, ResultHandler handler) {
+        this.driverManager = driverManager;
+        this.connectionString = connectionString;
+        this.requestTimeoutMs = requestTimeoutMs;
+        this.handler = handler;
+    }
+
+    @Override
+    public void run() {
+        // Does a single fetch
+        try (PlcConnection connection = driverManager.getConnection(connectionString)) {
+            PlcReadResponse response;
+            try {
+                response = connection.readRequestBuilder()
+                    .addItem("item1", "add1")
+                    .build()
+                    .execute()
+                    .get(requestTimeoutMs, TimeUnit.MILLISECONDS);
+            } catch (ExecutionException e) {
+                // Handle execution exception
+                handler.handleException(e);
+                return;
+            }
+            CompletableFuture.runAsync(() -> handler.handle(transformResponseToMap(response)));
+        } catch (PlcConnectionException e) {
+            throw new PlcRuntimeException("Unable to fetch", e);
+        } catch (Exception e) {
+            throw new PlcRuntimeException("Unexpected exception during fetch", e);
+        }
+    }
+
+    private Map<String, Object> transformResponseToMap(PlcReadResponse response) {
+        return response.getFieldNames().stream()
+            .collect(Collectors.toMap(
+                name -> name,
+                response::getObject
+            ));
+    }
+
+    public interface ResultHandler {
+
+        void handle(Map<String, Object> result);
+
+        void handleException(Exception e);
+
+    }
+}
diff --git a/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/s7/ManualS7PlcDriverMT.java b/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/s7/ManualS7PlcDriverMT.java
index 073c221..8822545 100644
--- a/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/s7/ManualS7PlcDriverMT.java
+++ b/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/s7/ManualS7PlcDriverMT.java
@@ -25,7 +25,8 @@ import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
 import org.apache.plc4x.java.api.messages.PlcReadResponse;
 import org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager;
-import org.junit.Test;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.MethodSource;
@@ -36,14 +37,18 @@ import java.util.concurrent.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Stream;
 
+/**
+ * Manual Test.
+ */
+@Disabled
 public class ManualS7PlcDriverMT {
 
     public static final String CONN_STRING = "s7://10.10.64.22/0/1";
     public static final String FIELD_STRING = "%DB225:DBW0:INT";
 
-
-//    public static final String CONN_STRING = "s7://10.10.64.20/0/1";
+    //    public static final String CONN_STRING = "s7://10.10.64.20/0/1";
 //    public static final String FIELD_STRING = "%DB3:DBD32:DINT";
+
     @Test
     public void simpleLoop() {
         PlcDriverManager plcDriverManager = new PooledPlcDriverManager();
@@ -79,13 +84,48 @@ public class ManualS7PlcDriverMT {
         executorService.awaitTermination(100, TimeUnit.SECONDS);
     }
 
+    @Test
+    public void parallelScheduledLoop() throws InterruptedException {
+        int period = 5;
+        PlcDriverManager plcDriverManager = new PooledPlcDriverManager();
+        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
+        DescriptiveStatistics statistics1 = new DescriptiveStatistics();
+        DescriptiveStatistics statistics2 = new DescriptiveStatistics();
+
+        int numberOfRuns = 1000;
+        AtomicInteger counter1 = new AtomicInteger(0);
+        AtomicInteger counter2 = new AtomicInteger(0);
+        executorService.scheduleAtFixedRate(() -> {
+            // System.out.println("Run: " + counter.get());
+            double timeNs = runSingleRequest(plcDriverManager);
+            statistics1.addValue(timeNs);
+            if (counter1.getAndIncrement() >= numberOfRuns) {
+                executorService.shutdown();
+            }
+        }, 0, period, TimeUnit.MILLISECONDS);
+        executorService.scheduleAtFixedRate(() -> {
+            // System.out.println("Run: " + counter.get());
+            double timeNs = runSingleRequest(plcDriverManager);
+            statistics2.addValue(timeNs);
+            if (counter2.getAndIncrement() >= numberOfRuns) {
+                executorService.shutdown();
+            }
+        }, 0, period, TimeUnit.MILLISECONDS);
+
+        executorService.awaitTermination(100, TimeUnit.SECONDS);
+        System.out.println("Statistics 1");
+        printStatistics(statistics1);
+        System.out.println("Statistics 2");
+        printStatistics(statistics2);
+    }
+
     private static Stream<Arguments> periodAndRus() {
         return Stream.of(
             Arguments.of(10, 100),
             Arguments.of(10, 1000),
             Arguments.of(100, 100),
             Arguments.of(100, 1000)
-            );
+        );
     }
 
     @ParameterizedTest
@@ -122,7 +162,6 @@ public class ManualS7PlcDriverMT {
                     }
                     if (counter.getAndIncrement() >= numberOfRuns) {
                         executorService.shutdown();
-                        ManualS7PlcDriverMT.this.printStatistics(statistics);
                     }
                 }, period, TimeUnit.MILLISECONDS);
             }
@@ -130,11 +169,14 @@ public class ManualS7PlcDriverMT {
 
         executorService.scheduleAtFixedRate(iteration, 0, period, TimeUnit.MILLISECONDS);
         executorService.awaitTermination(100, TimeUnit.SECONDS);
+        // Print statistics
+        ManualS7PlcDriverMT.this.printStatistics(statistics);
     }
 
     private double runSingleRequest(PlcDriverManager plcDriverManager) {
         long start = System.nanoTime();
         try (PlcConnection connection = plcDriverManager.getConnection(CONN_STRING)) {
+            System.out.println("Connection: " + connection);
             CompletableFuture<? extends PlcReadResponse> future = connection.readRequestBuilder()
                 .addItem("distance", FIELD_STRING)
                 .build()
diff --git a/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/scraper/ScraperTest.java b/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/scraper/ScraperTest.java
new file mode 100644
index 0000000..25b526a
--- /dev/null
+++ b/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/scraper/ScraperTest.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.plc4x.java.scraper;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.plc4x.java.PlcDriverManager;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
+import org.apache.plc4x.java.base.messages.items.DefaultStringFieldItem;
+import org.apache.plc4x.java.mock.MockDevice;
+import org.apache.plc4x.java.mock.PlcMockConnection;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.util.Map;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+public class ScraperTest {
+
+    @Test
+    public void scrape() throws PlcConnectionException {
+        PlcDriverManager driverManager = new PlcDriverManager();
+        PlcMockConnection connection = (PlcMockConnection) driverManager.getConnection("mock:scraper");
+        MockDevice mockDevice = Mockito.mock(MockDevice.class);
+        connection.setDevice(mockDevice);
+        when(mockDevice.read(any())).thenReturn(Pair.of(PlcResponseCode.OK, new DefaultStringFieldItem("hallo")));
+
+        Scraper scraper = new Scraper(driverManager, "mock:scraper", 1_000, new Scraper.ResultHandler() {
+            @Override
+            public void handle(Map<String, Object> result) {
+                System.out.println(result);
+            }
+
+            @Override
+            public void handleException(Exception e) {
+                System.err.println(e);
+            }
+        });
+
+        scraper.run();
+    }
+
+    @Test
+    public void scrape_badResponseCode_shouldHandleException() throws PlcConnectionException {
+        PlcDriverManager driverManager = new PlcDriverManager();
+        PlcMockConnection connection = (PlcMockConnection) driverManager.getConnection("mock:scraper");
+        MockDevice mockDevice = Mockito.mock(MockDevice.class);
+        connection.setDevice(mockDevice);
+        when(mockDevice.read(any())).thenReturn(Pair.of(PlcResponseCode.NOT_FOUND, new DefaultStringFieldItem("hallo")));
+
+        Scraper.ResultHandler handler = Mockito.mock(Scraper.ResultHandler.class);
+
+        Scraper scraper = new Scraper(driverManager, "mock:scraper", 1_000, null);
+
+        scraper.run();
+    }
+}
\ No newline at end of file