Skip to content

Commit

Permalink
test: common: Fix flaky FinalizeTest
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kohlschuetter committed Sep 23, 2024
1 parent a7b513a commit 7d896b1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ public static void main(String[] args) throws Exception {

// create some pressure on GC
List<String> 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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -67,12 +66,29 @@ private static List<String> 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<String> it = lines.iterator(); it.hasNext();) {
String line = it.next();
if (!line.contains("unix")) {
it.remove();
}
}
}

p.waitFor();
} finally {
p.destroy();
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/site/markdown/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ artifact (`<type>pom</type>`); 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
Expand Down

0 comments on commit 7d896b1

Please sign in to comment.