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 2026/09/06 09:31:52 UTC

(plc4x) 02/04: test(plc4j): stop asserting on what the machine happens to provide

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

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

commit 79788338f4f01909768ef8cb0a386d389f2b36c1
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Sun Sep 6 11:30:48 2026 +0200

    test(plc4j): stop asserting on what the machine happens to provide
    
    Three SocketCAN transport tests asserted that creating an instance throws,
    which only held on a machine that is not Linux: off Linux the platform check
    rejects it early, while on the build agents execution reaches javacan and the
    missing native library throws NoClassDefFoundError and LinkageError - errors,
    not exceptions, so assertThrows(Exception.class) failed. They were written and
    verified on macOS, where the assertion happened to hold for the wrong reason.
    
    What those tests are about is the resolved configuration, which is settled
    before any platform-specific work happens. They now attempt creation, discard
    whatever comes back, and assert only that. The raw-socket tests carried the
    same flaw through libpcap and were fixed with the change that introduced them.
    
    The COTP framing test measured 508 ms of CPU against a 500 ms budget on a
    loaded agent. Not spinning costs whatever loading the connect path's classes
    costs - about 170 ms on a fast laptop and about 510 ms there - while a spin
    burns the whole 1200 ms wait. The budget sat next to the fast figure rather
    than between the two outcomes it distinguishes, so it reported a regression
    where there was none. Widened to 800 ms, which still leaves 400 ms of daylight
    before a spin, with both measurements recorded next to the number.
    
    Co-Authored-By: Claude Opus 5 (1M context) <no...@anthropic.com>
    Claude-Session: https://claude.ai/code/session_01VKeuoJnei5dxjP23RRbPSJ
---
 .../org/apache/plc4x/java/s7/CotpFramingTest.java  |  8 +++--
 .../can/socketcan/SocketCanTransportTest.java      | 39 +++++++++++++++++-----
 2 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/CotpFramingTest.java b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/CotpFramingTest.java
index 2dc7d59c09..b310c7a200 100644
--- a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/CotpFramingTest.java
+++ b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/CotpFramingTest.java
@@ -204,8 +204,12 @@ class CotpFramingTest {
         long cpuSpent = threadCpuTimeOfThisProcess() - cpuBefore;
         try {
             accepted.get(5, TimeUnit.SECONDS);
-            // 1.2 seconds of waiting should cost almost nothing. A spin costs about a second of it.
-            assertTrue(cpuSpent < 500_000_000L,
+            // A spin burns the whole 1200 ms wait; not spinning costs whatever loading the
+            // connect path's classes happens to cost on this machine, measured at ~170 ms on a
+            // fast laptop and ~510 ms on a loaded CI agent. The bound sits between the two rather
+            // than close to the fast figure, which is what made this fail for 508 ms of a 500 ms
+            // budget without anything having regressed.
+            assertTrue(cpuSpent < 800_000_000L,
                 "waiting for the confirm spent " + (cpuSpent / 1_000_000) + " ms of CPU on a 1200 ms wait");
         } finally {
             instance.close();
diff --git a/plc4j/transports/can-socketcan/src/test/java/org/apache/plc4x/java/transport/can/socketcan/SocketCanTransportTest.java b/plc4j/transports/can-socketcan/src/test/java/org/apache/plc4x/java/transport/can/socketcan/SocketCanTransportTest.java
index 0c608acc8c..823b219971 100644
--- a/plc4j/transports/can-socketcan/src/test/java/org/apache/plc4x/java/transport/can/socketcan/SocketCanTransportTest.java
+++ b/plc4j/transports/can-socketcan/src/test/java/org/apache/plc4x/java/transport/can/socketcan/SocketCanTransportTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.plc4x.java.transport.can.socketcan;
 
+import org.apache.plc4x.java.spi.transports.api.TransportInstance;
 import org.apache.plc4x.java.spi.transports.api.config.TransportConfiguration;
 import org.apache.plc4x.java.spi.transports.api.exceptions.TransportException;
 import org.apache.plc4x.java.transport.can.socketcan.config.SocketCanTransportConfiguration;
@@ -78,16 +79,40 @@ class SocketCanTransportTest {
                 transport.createTransportInstance("can-socketcan://can0", config, auditLog));
     }
 
+    /**
+     * Attempts to create an instance and discards whatever comes back.
+     * <p>
+     * What happens next depends entirely on the machine: off Linux the platform check rejects it,
+     * on Linux it goes on to open a real CAN socket, which may fail for want of the interface or
+     * of the native library - or succeed, on a host that has the interface configured. None of
+     * that is what these tests are about, so none of it is asserted on; the tests check the
+     * resolved configuration, which is settled before any of it happens.
+     */
+    private static void attemptCreation(SocketCanTransport transport, String addressSegment,
+                                        SocketCanTransportConfiguration config, AuditLog auditLog) {
+        TransportInstance<SocketCanTransportConfiguration> instance = null;
+        try {
+            instance = transport.createTransportInstance(addressSegment, config, auditLog);
+        } catch (Throwable ignored) {
+            // Deliberately ignored - see the javadoc above.
+        } finally {
+            if (instance != null) {
+                try {
+                    instance.close();
+                } catch (Exception ignored) {
+                    // Nothing useful to do in a test teardown path.
+                }
+            }
+        }
+    }
+
     @Test
     void addressSegmentNamesTheInterface() {
         SocketCanTransport transport = new SocketCanTransport();
         AuditLog auditLog = Mockito.mock(AuditLog.class);
         SocketCanTransportConfiguration config = new SocketCanTransportConfiguration();
 
-        // Opening the socket needs Linux, so the resolved interface is what is asserted here -
-        // it is settled before the platform check runs.
-        assertThrows(Exception.class, () ->
-                transport.createTransportInstance("can0", config, auditLog));
+        attemptCreation(transport, "can0", config, auditLog);
 
         assertEquals("can0", config.interfaceName);
     }
@@ -99,8 +124,7 @@ class SocketCanTransportTest {
         SocketCanTransportConfiguration config = new SocketCanTransportConfiguration();
         config.interfaceName = "vcan0";
 
-        assertThrows(Exception.class, () ->
-                transport.createTransportInstance("can0", config, auditLog));
+        attemptCreation(transport, "can0", config, auditLog);
 
         assertEquals("can0", config.interfaceName);
     }
@@ -112,8 +136,7 @@ class SocketCanTransportTest {
         SocketCanTransportConfiguration config = new SocketCanTransportConfiguration();
         config.interfaceName = "vcan0";
 
-        assertThrows(Exception.class, () ->
-                transport.createTransportInstance("", config, auditLog));
+        attemptCreation(transport, "", config, auditLog);
 
         assertEquals("vcan0", config.interfaceName);
     }