From 7d896b1b836696a2031262978ba8c6ee4b6f042a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kohlschu=CC=88tter?= Date: Mon, 23 Sep 2024 15:40:49 +0200 Subject: [PATCH] test: common: Fix flaky FinalizeTest Adding pressure to the GC just by creating new objects may not enough; test test failed sporadically. Now, let's forcibly call System.gc() every 500ms as well. Lastly, ignore all lines not related to unix sockets, if possible. --- .../newsclub/net/unix/FinalizeTestClient.java | 6 +++++ .../net/unix/domain/FinalizeTest.java | 25 +++++++++++++++---- src/site/markdown/changelog.md | 1 + 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/junixsocket-common/src/test/java/org/newsclub/net/unix/FinalizeTestClient.java b/junixsocket-common/src/test/java/org/newsclub/net/unix/FinalizeTestClient.java index b9302dab4..1088bf5e6 100644 --- a/junixsocket-common/src/test/java/org/newsclub/net/unix/FinalizeTestClient.java +++ b/junixsocket-common/src/test/java/org/newsclub/net/unix/FinalizeTestClient.java @@ -62,8 +62,14 @@ public static void main(String[] args) throws Exception { // create some pressure on GC List list = new ArrayList<>(); + long nextGc = System.currentTimeMillis() + 500; while (true) { list.add(new String("junixsocket".getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8)); + long now = System.currentTimeMillis(); + if (now >= nextGc) { + nextGc = now + 500; + System.gc(); // NOPMD + } } } } diff --git a/junixsocket-common/src/test/java/org/newsclub/net/unix/domain/FinalizeTest.java b/junixsocket-common/src/test/java/org/newsclub/net/unix/domain/FinalizeTest.java index 4f2214660..58bddb294 100644 --- a/junixsocket-common/src/test/java/org/newsclub/net/unix/domain/FinalizeTest.java +++ b/junixsocket-common/src/test/java/org/newsclub/net/unix/domain/FinalizeTest.java @@ -18,7 +18,6 @@ package org.newsclub.net.unix.domain; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assumptions.assumeFalse; import static org.junit.jupiter.api.Assumptions.assumeTrue; import java.io.BufferedReader; @@ -27,8 +26,8 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; -import java.util.Objects; import org.newsclub.net.unix.AFSocketCapability; import org.newsclub.net.unix.AFSocketCapabilityRequirement; @@ -67,12 +66,29 @@ private static List lsofUnixSockets(long pid) throws IOException, TestAb try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset .defaultCharset()))) { String l; + + boolean hasUnix = false; + while ((l = in.readLine()) != null) { lines.add(l); + if (!hasUnix && l.contains("unix")) { + hasUnix = true; + } if (l.contains("busybox")) { assumeTrue(false, "incompatible lsof binary"); } } + + if (hasUnix) { + // if "lsof" returns a "unix" identifier, focus on those lines specifically. + for (Iterator it = lines.iterator(); it.hasNext();) { + String line = it.next(); + if (!line.contains("unix")) { + it.remove(); + } + } + } + p.waitFor(); } finally { p.destroy(); @@ -114,9 +130,8 @@ protected void postRunCheck(Process process, Object linesBeforeObj) throws TestA } } - assumeFalse(Objects.requireNonNull(linesAfter).isEmpty(), "lsof may fail to return anything"); - - if (linesAfter != null && linesBefore != null) { + if (linesAfter != null && linesBefore != null && !linesBefore.isEmpty() && !linesAfter + .isEmpty()) { if (linesAfter.size() >= linesBefore.size()) { System.err.println("lsof: Unexpected output"); System.err.println("lsof: Output before: " + linesBefore); diff --git a/src/site/markdown/changelog.md b/src/site/markdown/changelog.md index 762c53ba0..3f0c78625 100644 --- a/src/site/markdown/changelog.md +++ b/src/site/markdown/changelog.md @@ -18,6 +18,7 @@ artifact (`pom`); see [Add junixsocket to your project](dependency. - Fix duplicate file descriptors being received sporadically for non-blocking sockets and upon error - Fix a flaky selftest when VSOCK is not supported - Fix a flaky selftest with GraalvM 17 +- Fix a flaky selftest with old-style finalizers - Improve interoperability with exotic Linux/Java combinations - Add support for loongarch64 Linux - Add more tests