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/22 07:12:16 UTC

[incubator-plc4x] branch develop updated: [OPM] Added Timeout Test for PlcEntityManager.read(...).

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

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


The following commit(s) were added to refs/heads/develop by this push:
     new b87edb1  [OPM] Added Timeout Test for PlcEntityManager.read(...).
b87edb1 is described below

commit b87edb19d45adfa29605a9b6b4c546c16450e570
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Thu Nov 22 08:11:53 2018 +0100

    [OPM] Added Timeout Test for PlcEntityManager.read(...).
---
 .../apache/plc4x/java/mock/PlcMockConnection.java  | 12 ++++++--
 plc4j/utils/opm/pom.xml                            | 11 +++----
 .../plc4x/java/opm/PlcEntityManagerTest.java       | 34 ++++++++++++++++++++++
 3 files changed, 49 insertions(+), 8 deletions(-)

diff --git a/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java
index 277f69f..5bf5941 100644
--- a/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java
+++ b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java
@@ -34,6 +34,7 @@ import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
 import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 import java.util.function.Function;
+import java.util.function.Supplier;
 import java.util.stream.Collectors;
 
 public class PlcMockConnection implements PlcConnection, PlcReader {
@@ -101,9 +102,14 @@ public class PlcMockConnection implements PlcConnection, PlcReader {
 
     @Override
     public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
-        Map<String, Pair<PlcResponseCode, BaseDefaultFieldItem>> response = readRequest.getFieldNames().stream()
-            .collect(Collectors.toMap(Function.identity(), name -> device.read(((MockField) readRequest.getField(name)).getFieldQuery())));
-        return CompletableFuture.completedFuture(new DefaultPlcReadResponse((DefaultPlcReadRequest)readRequest, response));
+        return CompletableFuture.supplyAsync(new Supplier<PlcReadResponse>() {
+            @Override
+            public PlcReadResponse get() {
+                Map<String, Pair<PlcResponseCode, BaseDefaultFieldItem>> response = readRequest.getFieldNames().stream()
+                    .collect(Collectors.toMap(Function.identity(), name -> device.read(((MockField) readRequest.getField(name)).getFieldQuery())));
+                return new DefaultPlcReadResponse((DefaultPlcReadRequest)readRequest, response);
+            }
+        });
     }
 
     @Override
diff --git a/plc4j/utils/opm/pom.xml b/plc4j/utils/opm/pom.xml
index 6e292c7..2912674 100644
--- a/plc4j/utils/opm/pom.xml
+++ b/plc4j/utils/opm/pom.xml
@@ -91,11 +91,12 @@
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-dependency-plugin</artifactId>
-        <configuration>
-          <usedDependencies combine.children="append">
-            <usedDependency>org.apache.plc4x:plc4j-protocol-test</usedDependency>
-          </usedDependencies>
-        </configuration>
+        <!--Currently commented out as the dependency is used "explicitly".-->
+        <!--<configuration>-->
+          <!--<usedDependencies combine.children="append">-->
+            <!--<usedDependency>org.apache.plc4x:plc4j-protocol-test</usedDependency>-->
+          <!--</usedDependencies>-->
+        <!--</configuration>-->
       </plugin>
     </plugins>
   </build>
diff --git a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java
index 98a9b58..429b100 100644
--- a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java
+++ b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java
@@ -19,18 +19,28 @@
 
 package org.apache.plc4x.java.opm;
 
+import org.apache.commons.lang3.tuple.Pair;
 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.PlcInvalidFieldException;
 import org.apache.plc4x.java.api.messages.PlcReadRequest;
 import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata;
+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 org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import java.util.concurrent.CompletableFuture;
 
 import static org.junit.Assert.*;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 public class PlcEntityManagerTest {
@@ -83,6 +93,29 @@ public class PlcEntityManagerTest {
     }
 
     @Test(expected = OPMException.class)
+    public void read_timeoutOnGet_throwsException() throws PlcConnectionException, OPMException {
+        // Prepare the Mock
+        MockDevice mockDevice = Mockito.mock(MockDevice.class);
+        PlcDriverManager driverManager = new PlcDriverManager();
+        PlcMockConnection connection = (PlcMockConnection) driverManager.getConnection("mock:test");
+        when(mockDevice.read(any())).thenAnswer(new Answer<Object>() {
+            @Override
+            public Object answer(InvocationOnMock invocation) throws Throwable {
+                // Sleep for 3s
+                Thread.sleep(3_000);
+                return Pair.of(PlcResponseCode.OK, new DefaultStringFieldItem("Hallo"));
+            }
+        });
+        connection.setDevice(mockDevice);
+
+        // Create Entity Manager
+        PlcEntityManager entityManager = new PlcEntityManager(driverManager);
+
+        // Issue Call which SHOULD timeout
+        BadEntity entity = entityManager.read(BadEntity.class, "mock:test");
+    }
+
+    @Test(expected = OPMException.class)
     public void read_uninstantiableEntity_throws() throws OPMException {
         PlcEntityManager entityManager = new PlcEntityManager();
 
@@ -123,4 +156,5 @@ public class PlcEntityManagerTest {
             return field1;
         }
     }
+
 }
\ No newline at end of file