You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2023/01/15 16:21:07 UTC
[plc4x] branch feature/cdutz/connection-cache-and-scraper-ng updated: feat(cache): Added a new implementation of a connection cache
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch feature/cdutz/connection-cache-and-scraper-ng
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/feature/cdutz/connection-cache-and-scraper-ng by this push:
new 2594ebef26 feat(cache): Added a new implementation of a connection cache
2594ebef26 is described below
commit 2594ebef267b2280aade2815499aa112a1f9cc44
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Sun Jan 15 17:20:58 2023 +0100
feat(cache): Added a new implementation of a connection cache
- Added some more tests
---
.../utils/cache/CachedPlcConnectionManager.java | 1 +
.../cache/CachedPlcConnectionManagerTest.java | 88 +++++++++++++++++++++-
.../apache/plc4x/java/utils/cache/ManualTest.java | 18 +++++
3 files changed, 105 insertions(+), 2 deletions(-)
diff --git a/plc4j/tools/connection-cache-ng/src/main/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManager.java b/plc4j/tools/connection-cache-ng/src/main/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManager.java
index 0ffd2d6999..2be009580b 100644
--- a/plc4j/tools/connection-cache-ng/src/main/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManager.java
+++ b/plc4j/tools/connection-cache-ng/src/main/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManager.java
@@ -45,6 +45,7 @@ public class CachedPlcConnectionManager implements PlcConnectionManager {
public static Builder getBuilder(PlcConnectionManager connectionManager) {
return new Builder(connectionManager);
}
+
public CachedPlcConnectionManager(PlcConnectionManager connectionManager, Duration maxLeaseTime, Duration maxWaitTime) {
this.connectionManager = connectionManager;
this.maxLeaseTime = maxLeaseTime;
diff --git a/plc4j/tools/connection-cache-ng/src/test/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManagerTest.java b/plc4j/tools/connection-cache-ng/src/test/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManagerTest.java
index c656382063..e0a0d32e55 100644
--- a/plc4j/tools/connection-cache-ng/src/test/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManagerTest.java
+++ b/plc4j/tools/connection-cache-ng/src/test/java/org/apache/plc4x/java/utils/cache/CachedPlcConnectionManagerTest.java
@@ -1,23 +1,107 @@
+/*
+ * 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
+ *
+ * https://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.utils.cache;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.PlcConnectionManager;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
public class CachedPlcConnectionManagerTest {
+ /**
+ * This is the simplest possible test. Here the ConnectionManager is used exactly once.
+ * So not really much of the caching we can test, but it tests if we're creating connections the right way.
+ * @throws PlcConnectionException something went wrong-
+ */
@Test
- public void testSingleConnectionRequestTest() {
+ public void testSingleConnectionRequestTest() throws PlcConnectionException {
PlcConnectionManager mockConnectionManager = Mockito.mock(PlcConnectionManager.class);
CachedPlcConnectionManager connectionManager = CachedPlcConnectionManager.getBuilder(mockConnectionManager).build();
+
+ // Get the connection for the first time.
+ try(PlcConnection connection = connectionManager.getConnection("test")) {
+ Assertions.assertInstanceOf(LeasedPlcConnection.class, connection);
+ } catch (Exception e) {
+ Assertions.fail("Not expecting an exception here", e);
+ }
+
+ // Check getConnection was called on the mockConnectionManager instance exactly once.
+ Mockito.verify(mockConnectionManager, Mockito.times(1)).getConnection("test");
+ }
+
+ /**
+ * This test tries to get one connection two times after each other, in this case for the second time the
+ * connection should not be created, but the initial one be reused.
+ * @throws PlcConnectionException something went wrong-
+ */
+ @Test
+ public void testDoubleConnectionRequestTest() throws PlcConnectionException {
+ PlcConnectionManager mockConnectionManager = Mockito.mock(PlcConnectionManager.class);
+ CachedPlcConnectionManager connectionManager = CachedPlcConnectionManager.getBuilder(mockConnectionManager).build();
+
+ // Get the connection for the first time.
+ try(PlcConnection connection = connectionManager.getConnection("test")) {
+ Assertions.assertInstanceOf(LeasedPlcConnection.class, connection);
+ } catch (Exception e) {
+ Assertions.fail("Not expecting an exception here", e);
+ }
+
+ // Get the same connection a second time.
try(PlcConnection connection = connectionManager.getConnection("test")) {
Assertions.assertInstanceOf(LeasedPlcConnection.class, connection);
} catch (Exception e) {
Assertions.fail("Not expecting an exception here", e);
}
- // TODO: Check getConnection was called on the mockConnectionManager instance ...
+
+ // Check getConnection was called on the mockConnectionManager instance exactly once.
+ Mockito.verify(mockConnectionManager, Mockito.times(1)).getConnection("test");
+ }
+
+ /**
+ * In contrast to the previous test, in this case different connection strings are used and the
+ * cache should create two different connections.
+ * @throws PlcConnectionException something went wrong-
+ */
+ @Test
+ public void testDoubleConnectionRequestWithDifferentConnectionsTest() throws PlcConnectionException {
+ PlcConnectionManager mockConnectionManager = Mockito.mock(PlcConnectionManager.class);
+ CachedPlcConnectionManager connectionManager = CachedPlcConnectionManager.getBuilder(mockConnectionManager).build();
+
+ // Get the connection for the first time.
+ try(PlcConnection connection = connectionManager.getConnection("test")) {
+ Assertions.assertInstanceOf(LeasedPlcConnection.class, connection);
+ } catch (Exception e) {
+ Assertions.fail("Not expecting an exception here", e);
+ }
+
+ // Get the same connection a second time.
+ try(PlcConnection connection = connectionManager.getConnection("test-other")) {
+ Assertions.assertInstanceOf(LeasedPlcConnection.class, connection);
+ } catch (Exception e) {
+ Assertions.fail("Not expecting an exception here", e);
+ }
+
+ // Check getConnection was called on the mockConnectionManager instance twice, as they are different connections.
+ Mockito.verify(mockConnectionManager, Mockito.times(2)).getConnection(Mockito.any());
}
}
diff --git a/plc4j/tools/connection-cache-ng/src/test/java/org/apache/plc4x/java/utils/cache/ManualTest.java b/plc4j/tools/connection-cache-ng/src/test/java/org/apache/plc4x/java/utils/cache/ManualTest.java
index 94a9456c3b..0905f70b5c 100644
--- a/plc4j/tools/connection-cache-ng/src/test/java/org/apache/plc4x/java/utils/cache/ManualTest.java
+++ b/plc4j/tools/connection-cache-ng/src/test/java/org/apache/plc4x/java/utils/cache/ManualTest.java
@@ -1,3 +1,21 @@
+/*
+ * 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
+ *
+ * https://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.utils.cache;
import org.apache.plc4x.java.PlcDriverManager;