You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by hu...@apache.org on 2022/01/04 19:06:04 UTC

[plc4x] branch bug/PLC4X-329 created (now 8d540f1)

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

hutcheb pushed a change to branch bug/PLC4X-329
in repository https://gitbox.apache.org/repos/asf/plc4x.git.


      at 8d540f1  Add test for syncronized TransactionHandler

This branch includes the following new commits:

     new 8d540f1  Add test for syncronized TransactionHandler

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[plc4x] 01/01: Add test for syncronized TransactionHandler

Posted by hu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

hutcheb pushed a commit to branch bug/PLC4X-329
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 8d540f1284142b51b2b5d87234ff3ee18603a888
Author: hutcheb <hu...@apache.org>
AuthorDate: Wed Jan 5 05:04:06 2022 +1000

    Add test for syncronized TransactionHandler
---
 .../context/SecureChannelTransactionManager.java   |  4 +-
 .../plc4x/java/opcua/OpcuaPlcDriverTest.java       | 80 ++++++++++++++++++++++
 2 files changed, 82 insertions(+), 2 deletions(-)

diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannelTransactionManager.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannelTransactionManager.java
index e22574a..e6310d7 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannelTransactionManager.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannelTransactionManager.java
@@ -36,8 +36,8 @@ public class SecureChannelTransactionManager {
     private AtomicInteger activeTransactionId = new AtomicInteger(0);
     private Map<Integer, Transaction> queue = new HashMap<>();
 
-    public void submit(Consumer<Integer> onSend, Integer transactionId) {
-        LOGGER.info("New Transaction Submitted {}", activeTransactionId.get());
+    public synchronized void submit(Consumer<Integer> onSend, Integer transactionId) {
+        LOGGER.info("Active transaction Number {}", activeTransactionId.get());
         if (activeTransactionId.get() == transactionId) {
             onSend.accept(transactionId);
             int newTransactionId = getActiveTransactionIdentifier();
diff --git a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java
index a915e92..c9ee08c 100644
--- a/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java
+++ b/plc4j/drivers/opcua/src/test/java/org/apache/plc4x/java/opcua/OpcuaPlcDriverTest.java
@@ -41,6 +41,7 @@ import java.math.BigInteger;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.concurrent.ExecutionException;
 
 public class OpcuaPlcDriverTest {
 
@@ -370,4 +371,83 @@ public class OpcuaPlcDriverTest {
 
     }
 
+    /*
+        Test added to test the syncronized Trnasactionhandler.
+        The test originally failed one out of every 5 or so.
+     */
+    @Test
+    public void multipleThreads() {
+        class ReadWorker extends Thread {
+            private PlcConnection connection;
+
+            public ReadWorker(PlcConnection opcuaConnection) {
+                this.connection = opcuaConnection;
+            }
+
+            @Override
+            public void run() {
+                try {
+                    PlcReadRequest.Builder read_builder = connection.readRequestBuilder();
+                    read_builder.addItem("Bool", BOOL_IDENTIFIER_READ_WRITE);
+                    PlcReadRequest read_request = read_builder.build();
+
+                    for (int i = 0; i < 100; i ++) {
+                        PlcReadResponse read_response = read_request.execute().get();
+                        assertThat(read_response.getResponseCode("Bool")).isEqualTo(PlcResponseCode.OK);
+                    }
+                } catch (ExecutionException executionException) {
+                    executionException.printStackTrace();
+                } catch (InterruptedException interruptedException) {
+                    interruptedException.printStackTrace();
+                }
+            }
+        }
+
+        class WriteWorker extends Thread {
+            private PlcConnection connection;
+
+            public WriteWorker(PlcConnection opcuaConnection) {
+                this.connection = opcuaConnection;
+            }
+
+            @Override
+            public void run() {
+                try {
+                    PlcWriteRequest.Builder write_builder = connection.writeRequestBuilder();
+                    write_builder.addItem("Bool", BOOL_IDENTIFIER_READ_WRITE, true);
+                    PlcWriteRequest write_request = write_builder.build();
+
+                    for (int i = 0; i < 100; i ++) {
+                        PlcWriteResponse write_response = write_request.execute().get();
+                        assertThat(write_response.getResponseCode("Bool")).isEqualTo(PlcResponseCode.OK);
+                    }
+                } catch (ExecutionException executionException) {
+                    executionException.printStackTrace();
+                } catch (InterruptedException interruptedException) {
+                    interruptedException.printStackTrace();
+                }
+            }
+        }
+
+
+        try {
+            PlcConnection opcuaConnection = new PlcDriverManager().getConnection(tcpConnectionAddress);
+            Condition<PlcConnection> is_connected = new Condition<>(PlcConnection::isConnected, "is connected");
+            assertThat(opcuaConnection).is(is_connected);
+
+            ReadWorker read_worker = new ReadWorker(opcuaConnection);
+            WriteWorker write_worker = new WriteWorker(opcuaConnection);
+            read_worker.start();
+            write_worker.start();
+
+            read_worker.join();
+            write_worker.join();
+
+            opcuaConnection.close();
+            assert !opcuaConnection.isConnected();
+        } catch (Exception e) {
+            fail("Exception during readVariables Test EXCEPTION: " + e.getMessage());
+        }
+    }
+
 }