Skip to content

Commit

Permalink
WIP: Still hangs
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrueden committed Jul 11, 2024
1 parent b2fb6e7 commit 01d2902
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/test/java/org/apposed/appose/SharedMemoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ public void testShmCreate() {
}

@Test
public void testShmAttach() throws IOException {
public void testShmAttach() throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder();
boolean windows = System.getProperty("os.name").startsWith("Win");
String pythonCommand = windows ? "python.exe" : "python";
System.out.println("1");
Process p = new ProcessBuilder().command(pythonCommand, "-c", //
"from multiprocessing.shared_memory import SharedMemory;" + //
"from sys import stdin, stdout;" + //
Expand All @@ -94,13 +95,40 @@ public void testShmAttach() throws IOException {
"shm.unlink()" //
).start();

// Read the name of the shared memory that Python created.
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String[] shmInfo = in.readLine().split("|");
assertEquals(2, shmInfo.length);
System.out.println("2");

// Read output of Python program in a separate thread.
// Because pipes need threads to avoid clogging up.
String[] shmInfo = new String[2];
synchronized (shmInfo) {
Thread t = new Thread(() -> {
System.out.println("4");
// Read the name of the shared memory that Python created.
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
try {
String[] tokens = in.readLine().split("|");
shmInfo[0] = tokens[0];
shmInfo[1] = tokens[1];
} catch (IOException exc) {
exc.printStackTrace();
}
System.out.println("5");
synchronized (shmInfo) {
shmInfo.notify();
}
});
System.out.println("3");
t.start();
shmInfo.wait();
System.out.println("6");
}
System.out.println("7");
//t.join();
String shmName = shmInfo[0];
assertNotNull(shmName);
int shmSize = Integer.parseInt(shmInfo[1]);
assertEquals(345, shmSize);
System.out.println("8");

// Attach to the shared memory.
try (SharedMemory shm = SharedMemory.attach(shmName)) {
Expand All @@ -113,10 +141,12 @@ public void testShmAttach() throws IOException {
assertEquals(45, buf.get(100));
assertEquals(67, buf.get(344));
}
System.out.println("9");

// Close out the Python process.
OutputStream os = p.getOutputStream();
os.write('\n');
os.flush();
System.out.println("10");
}
}

0 comments on commit 01d2902

Please sign in to comment.