From 01d29020dc8a994935b1ead9781a339fe02657a6 Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Thu, 11 Jul 2024 15:05:03 -0500 Subject: [PATCH] WIP: Still hangs --- .../org/apposed/appose/SharedMemoryTest.java | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/apposed/appose/SharedMemoryTest.java b/src/test/java/org/apposed/appose/SharedMemoryTest.java index b8e0c01..a94d61a 100644 --- a/src/test/java/org/apposed/appose/SharedMemoryTest.java +++ b/src/test/java/org/apposed/appose/SharedMemoryTest.java @@ -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;" + // @@ -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)) { @@ -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"); } }