Skip to content

Commit 1af6529

Browse files
committed
Manage java.io.tmpdir better to make multi-process test pass reliably, move ProcessRunner into tests where it belongs
1 parent a57cc41 commit 1af6529

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

affinity/src/test/java/net/openhft/affinity/FileLockLockCheckTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
package net.openhft.affinity;
1919

2020
import net.openhft.affinity.testimpl.TestFileLockBasedLockChecker;
21-
import org.junit.Assert;
22-
import org.junit.Assume;
23-
import org.junit.Before;
24-
import org.junit.Test;
21+
import org.junit.*;
2522

2623
import java.io.File;
2724
import java.io.IOException;
@@ -54,6 +51,11 @@ public void before() {
5451
System.setProperty("java.io.tmpdir", TARGET + "/" + System.nanoTime());
5552
}
5653

54+
@After
55+
public void after() {
56+
System.setProperty("java.io.tmpdir", TMP);
57+
}
58+
5759
@Test
5860
public void test() throws IOException {
5961
Assert.assertTrue(LockCheck.isCpuFree(cpu));

affinity/src/test/java/net/openhft/affinity/LockCheckTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
package net.openhft.affinity;
1919

2020
import net.openhft.affinity.testimpl.TestFileBasedLockChecker;
21-
import org.junit.Assert;
22-
import org.junit.Assume;
23-
import org.junit.Before;
24-
import org.junit.Test;
21+
import org.junit.*;
2522

2623
import java.io.File;
2724
import java.io.FileWriter;
@@ -55,6 +52,11 @@ public void before() {
5552
System.setProperty("java.io.tmpdir", TARGET + "/" + System.nanoTime());
5653
}
5754

55+
@After
56+
public void after() {
57+
System.setProperty("java.io.tmpdir", TMP);
58+
}
59+
5860
@Test
5961
public void test() throws IOException {
6062
Assert.assertTrue(LockCheck.isCpuFree(cpu));

affinity/src/test/java/net/openhft/affinity/MultiProcessAffinityTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import net.openhft.affinity.common.ProcessRunner;
44
import net.openhft.affinity.lockchecker.FileLockBasedLockChecker;
5-
import org.junit.Assume;
6-
import org.junit.Test;
5+
import org.junit.*;
6+
import org.junit.rules.TemporaryFolder;
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99

@@ -16,11 +16,28 @@
1616

1717
public class MultiProcessAffinityTest {
1818

19+
@Rule
20+
public TemporaryFolder folder = new TemporaryFolder();
21+
private String originalTmpDir;
22+
23+
@Before
24+
public void setUp() {
25+
originalTmpDir = System.getProperty("java.io.tmpdir");
26+
System.setProperty("java.io.tmpdir", folder.getRoot().getAbsolutePath());
27+
}
28+
29+
@After
30+
public void tearDown() {
31+
System.setProperty("java.io.tmpdir", originalTmpDir);
32+
}
33+
1934
@Test
2035
public void shouldNotAcquireLockOnCoresLockedByOtherProcesses() throws IOException, InterruptedException {
2136
Assume.assumeTrue(IS_LINUX);
2237
// run the separate affinity locker
23-
final Process affinityLockerProcess = ProcessRunner.runClass(AffinityLockerProcess.class, "last");
38+
final Process affinityLockerProcess = ProcessRunner.runClass(AffinityLockerProcess.class,
39+
new String[]{"-Djava.io.tmpdir=" + folder.getRoot().getAbsolutePath()},
40+
new String[]{"last"});
2441
try {
2542
int lastCpuId = AffinityLock.PROCESSORS - 1;
2643

affinity/src/main/java/net/openhft/affinity/common/ProcessRunner.java renamed to affinity/src/test/java/net/openhft/affinity/common/ProcessRunner.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ public class ProcessRunner {
2929
* @throws IOException if there is an error starting the process
3030
*/
3131
public static Process runClass(Class<?> clazz, String... args) throws IOException {
32+
return runClass(clazz, new String[]{}, args);
33+
}
34+
35+
/**
36+
* Spawn a process running the main method of a specified class
37+
*
38+
* @param clazz The class to execute
39+
* @param jvmArgs Any arguments to pass to the process
40+
* @param programArgs Any arguments to pass to the process
41+
* @return the Process spawned
42+
* @throws IOException if there is an error starting the process
43+
*/
44+
public static Process runClass(Class<?> clazz, String[] jvmArgs, String[] programArgs) throws IOException {
3245
// Because Java17 must be run using various module flags, these must be propagated
3346
// to the child processes
3447
// https://stackoverflow.com/questions/1490869/how-to-get-vm-arguments-from-inside-of-java-application
@@ -46,10 +59,11 @@ public static Process runClass(Class<?> clazz, String... args) throws IOExceptio
4659
List<String> allArgs = new ArrayList<>();
4760
allArgs.add(javaBin);
4861
allArgs.addAll(jvmArgsWithoutJavaAgents);
62+
allArgs.addAll(Arrays.asList(jvmArgs));
4963
allArgs.add("-cp");
5064
allArgs.add(classPath);
5165
allArgs.add(className);
52-
allArgs.addAll(Arrays.asList(args));
66+
allArgs.addAll(Arrays.asList(programArgs));
5367
ProcessBuilder processBuilder = new ProcessBuilder(allArgs.toArray(new String[]{}));
5468
// processBuilder.inheritIO(); // this doesn't place nice with surefire
5569
return processBuilder.start();

0 commit comments

Comments
 (0)