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 2021/01/03 13:39:18 UTC

[plc4x] 01/04: Finished Integration.

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

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

commit 4c24817811ce4ec5d5233223c3ed864e3a77d9e8
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Sun Jan 3 14:35:53 2021 +0100

    Finished Integration.
---
 plc4j/tools/connection-pool/pom.xml                | 11 +------
 .../utils/connectionpool/CachedDriverManager.java  | 20 +++++++------
 .../connectionpool/CachedDriverManagerIT.java      |  5 ++--
 .../connectionpool/CachedDriverManagerMT.java      |  5 +++-
 .../services/org.apache.plc4x.java.api.PlcDriver   | 19 ------------
 .../connection-pool/src/test/resources/log4j2.xml  | 15 ++++++++++
 .../connection-pool/src/test/resources/logback.xml | 34 ----------------------
 7 files changed, 34 insertions(+), 75 deletions(-)

diff --git a/plc4j/tools/connection-pool/pom.xml b/plc4j/tools/connection-pool/pom.xml
index f4e0adc..84051c2 100644
--- a/plc4j/tools/connection-pool/pom.xml
+++ b/plc4j/tools/connection-pool/pom.xml
@@ -42,7 +42,6 @@
     <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
-      <version>3.8.1</version>
       <scope>compile</scope>
     </dependency>
     <dependency>
@@ -70,21 +69,13 @@
       </exclusions>
     </dependency>
     <dependency>
-      <groupId>org.junit.jupiter</groupId>
-      <artifactId>junit-jupiter-api</artifactId>
-      <version>5.2.0</version>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
       <groupId>org.assertj</groupId>
       <artifactId>assertj-core</artifactId>
-      <version>3.11.1</version>
       <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>org.mockito</groupId>
       <artifactId>mockito-core</artifactId>
-      <version>2.23.4</version>
       <scope>test</scope>
     </dependency>
     <dependency>
@@ -101,7 +92,7 @@
     </dependency>
     <dependency>
       <groupId>org.junit.jupiter</groupId>
-      <artifactId>junit-jupiter-engine</artifactId>
+      <artifactId>junit-jupiter</artifactId>
       <scope>test</scope>
     </dependency>
   </dependencies>
diff --git a/plc4j/tools/connection-pool/src/main/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManager.java b/plc4j/tools/connection-pool/src/main/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManager.java
index 8c65e75..d15df86 100644
--- a/plc4j/tools/connection-pool/src/main/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManager.java
+++ b/plc4j/tools/connection-pool/src/main/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManager.java
@@ -139,14 +139,16 @@ public class CachedDriverManager extends PlcDriverManager implements CachedDrive
         if (!this.url.equals(url)) {
             throw new IllegalArgumentException("This Cached Driver Manager only supports the Conection " + url);
         }
-        if (queue.isEmpty() && isConnectionAvailable()) {
-            return getConnection_(url);
-        } else {
-            // At least trigger a connection
-            try {
-                getConnection_(url).close();
-            } catch (Exception e) {
-                // Ignore here.
+        synchronized (this) {
+            if (queue.isEmpty() && isConnectionAvailable()) {
+                return getConnection_(url);
+            } else {
+                // At least trigger a connection
+                try {
+                    getConnection_(url).close();
+                } catch (Exception e) {
+                    // Ignore here.
+                }
             }
         }
         CompletableFuture<PlcConnection> future = new CompletableFuture<>();
@@ -224,7 +226,7 @@ public class CachedDriverManager extends PlcDriverManager implements CachedDrive
     /**
      * Checks if someone is waiting in the line to get the connection.
      */
-    private void checkQueue() {
+    private synchronized void checkQueue() {
         logger.debug("Connection is available, checking if someone is waiting in the queue...");
         CompletableFuture<PlcConnection> next;
         while ((next = queue.poll()) != null) {
diff --git a/plc4j/tools/connection-pool/src/test/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManagerIT.java b/plc4j/tools/connection-pool/src/test/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManagerIT.java
index 0256b16..4ae3052 100644
--- a/plc4j/tools/connection-pool/src/test/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManagerIT.java
+++ b/plc4j/tools/connection-pool/src/test/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManagerIT.java
@@ -30,7 +30,7 @@ class CachedDriverManagerIT {
         MockConnection plcMockConnection = mock(MockConnection.class);
         when(mock.create()).thenReturn(plcMockConnection);
 
-        CachedDriverManager driverManager = new CachedDriverManager("", mock, 2_000);
+        CachedDriverManager driverManager = new CachedDriverManager("", mock, 100_000);
 
         AtomicInteger errorCounter = new AtomicInteger(0);
         AtomicInteger successCounter = new AtomicInteger(0);
@@ -41,6 +41,7 @@ class CachedDriverManagerIT {
                     driverManager.getConnection("").close();
                     successCounter.incrementAndGet();
                 } catch (Exception e) {
+                    e.printStackTrace();
                     errorCounter.incrementAndGet();
                 }
             });
@@ -48,7 +49,7 @@ class CachedDriverManagerIT {
 
         executorService.shutdown();
 
-        executorService.awaitTermination(10, TimeUnit.SECONDS);
+        executorService.awaitTermination(50, TimeUnit.SECONDS);
 
         assertEquals(100, successCounter.get());
         assertEquals(0, errorCounter.get());
diff --git a/plc4j/tools/connection-pool/src/test/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManagerMT.java b/plc4j/tools/connection-pool/src/test/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManagerMT.java
index 96862a0..ced7f52 100644
--- a/plc4j/tools/connection-pool/src/test/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManagerMT.java
+++ b/plc4j/tools/connection-pool/src/test/java/org/apache/plc4x/java/utils/connectionpool/CachedDriverManagerMT.java
@@ -1,9 +1,11 @@
 package org.apache.plc4x.java.utils.connectionpool;
 
+import jdk.nashorn.internal.ir.annotations.Ignore;
 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.messages.PlcReadResponse;
+import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -16,6 +18,7 @@ import java.util.concurrent.*;
  * @author julian
  * Created by julian on 24.02.20
  */
+@Disabled("These should be run manually")
 class CachedDriverManagerMT {
 
     private static final Logger logger = LoggerFactory.getLogger(CachedDriverManagerMT.class);
@@ -87,7 +90,7 @@ class CachedDriverManagerMT {
 
         PooledDriverManager driverManager = new PooledDriverManager();
 
-        for (int i = 1; i <= 100_000; i++) {
+        for (int i = 1; i <= 10_000; i++) {
             pool.submit(() -> {
                 try {
                     PlcConnection conn = driverManager.getConnection(PLC_IP);
diff --git a/plc4j/tools/connection-pool/src/test/resources/META-INF/services/org.apache.plc4x.java.api.PlcDriver b/plc4j/tools/connection-pool/src/test/resources/META-INF/services/org.apache.plc4x.java.api.PlcDriver
deleted file mode 100644
index a2d97ed..0000000
--- a/plc4j/tools/connection-pool/src/test/resources/META-INF/services/org.apache.plc4x.java.api.PlcDriver
+++ /dev/null
@@ -1,19 +0,0 @@
-#
-# 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.
-#
-org.apache.plc4x.java.utils.connectionpool.PooledDummyDriver
diff --git a/plc4j/tools/connection-pool/src/test/resources/log4j2.xml b/plc4j/tools/connection-pool/src/test/resources/log4j2.xml
new file mode 100644
index 0000000..e8efef9
--- /dev/null
+++ b/plc4j/tools/connection-pool/src/test/resources/log4j2.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="WARN">
+    <Appenders>
+        <Console name="Console" target="SYSTEM_OUT">
+            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
+        </Console>
+    </Appenders>
+    <Loggers>
+        <Root level="WARN">
+            <AppenderRef ref="Console"/>
+        </Root>
+        <Logger name="org.pragmaticindustries.cockpit.plc" level="DEBUG"/>
+        <Logger name="org.pragmaticindustries.cockpit.plc.pool2" level="DEBUG"/>
+    </Loggers>
+</Configuration>
\ No newline at end of file
diff --git a/plc4j/tools/connection-pool/src/test/resources/logback.xml b/plc4j/tools/connection-pool/src/test/resources/logback.xml
deleted file mode 100644
index 31c49f0..0000000
--- a/plc4j/tools/connection-pool/src/test/resources/logback.xml
+++ /dev/null
@@ -1,34 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  ~ 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.
-  -->
-<configuration xmlns="http://ch.qos.logback/xml/ns/logback"
-               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-               xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd">
-
-  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
-    <!-- encoders are assigned the type
-         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
-    <encoder>
-      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
-    </encoder>
-  </appender>
-
-  <root level="DEBUG">
-    <appender-ref ref="STDOUT" />
-  </root>
-
-</configuration>
\ No newline at end of file