Skip to content

Commit

Permalink
WIP: Aaand it....... hangs!
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrueden committed Jul 11, 2024
1 parent e9873fd commit b2fb6e7
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/test/java/org/apposed/appose/SharedMemoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@

import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.ByteBuffer;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -72,13 +76,47 @@ public void testShmCreate() {
}
}

public void testShmAttach() {
// TODO: ProcessBuilder exec a Python that constructs a new
// shared memory using Python's plain SharedMemory API.
// Modify it from Python as well, then attach to it here
// and validate that the modifications there are accessible
// here afterward.
// What about Python's forced unlinking? Problem here?
}
@Test
public void testShmAttach() throws IOException {
ProcessBuilder pb = new ProcessBuilder();
boolean windows = System.getProperty("os.name").startsWith("Win");
String pythonCommand = windows ? "python.exe" : "python";
Process p = new ProcessBuilder().command(pythonCommand, "-c", //
"from multiprocessing.shared_memory import SharedMemory;" + //
"from sys import stdin, stdout;" + //
"shm = SharedMemory(create=True, size=345);" + //
"shm.buf[0] = 123;" + //
"shm.buf[100] = 45;" + //
"shm.buf[344] = 67;" + //
"stdout.write(f'{shm.name}|{shm.size}');" + //
"stdout.flush();" + //
"stdin.readline();" + //
"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);
String shmName = shmInfo[0];
int shmSize = Integer.parseInt(shmInfo[1]);
assertEquals(345, shmSize);

// Attach to the shared memory.
try (SharedMemory shm = SharedMemory.attach(shmName)) {
assertNotNull(shm);
assertEquals(shmName, shm.name());
assertEquals(shmSize, shm.size());
ByteBuffer buf = shm.pointer().getByteBuffer(0, shmSize);
// TODO: byte values > 127
assertEquals(123, buf.get(0));
assertEquals(45, buf.get(100));
assertEquals(67, buf.get(344));
}

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

0 comments on commit b2fb6e7

Please sign in to comment.