Skip to content

Commit

Permalink
Fix shm test failures on macOS
Browse files Browse the repository at this point in the history
On macOS, the size of the allocated shared memory block might not
precisely match the request. But it needs to be at least as large as
the requested size. E.g.: sizes less than 16384 round up to 16384.

You might think it would be a good idea to make the size() method
of SharedMemory return the actually requested size, rather than the
weird rounded up size, but that's how it works on macOS on Python,
and we're trying to make the Java implementation match the Python
behavior as closely as possible, so... ¯\_(ツ)_/¯
  • Loading branch information
ctrueden committed Sep 24, 2024
1 parent 59461a8 commit 4ea317b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/test/java/org/apposed/appose/SharedMemoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests {@link SharedMemory}.
Expand All @@ -54,7 +55,7 @@ public void testShmCreate() throws IOException {
int size = 456;
try (SharedMemory shm = SharedMemory.create(null, size)) {
assertNotNull(shm.name());
assertEquals(size, shm.size());
assertTrue(shm.size() >= size);
assertNotNull(shm.pointer());

// Modify the memory contents.
Expand All @@ -74,8 +75,8 @@ public void testShmCreate() throws IOException {
String output = runPython(
"from multiprocessing.shared_memory import SharedMemory\n" +
"from sys import stdout\n" +
"shm = SharedMemory(name='" + shm.name() + "', size=" + shm.size() + ")\n" +
"matches = sum(1 for i in range(shm.size) if shm.buf[i] == (shm.size - i) % 256)\n" +
"shm = SharedMemory(name='" + shm.name() + "', size=" + size + ")\n" +
"matches = sum(1 for i in range(" + size + ") if shm.buf[i] == (" + size + " - i) % 256)\n" +
"stdout.write(f'{matches}\\n')\n" +
"stdout.flush()\n" +
"shm.unlink()\n" // HACK: to satisfy Python's overly aggressive resource tracker
Expand Down Expand Up @@ -112,7 +113,7 @@ public void testShmAttach() throws IOException {
assertNotNull(shmName);
assertFalse(shmName.isEmpty());
int shmSize = Integer.parseInt(shmInfo[1]);
assertEquals(345, shmSize);
assertTrue(shmSize >= 345);

// Attach to the shared memory and verify it matches expectations.
try (SharedMemory shm = SharedMemory.attach(shmName, shmSize)) {
Expand Down
15 changes: 11 additions & 4 deletions src/test/java/org/apposed/appose/TypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class TypesTest {
"\"shm\":{" +
"\"appose_type\":\"shm\"," +
"\"name\":\"SHM_NAME\"," +
"\"size\":4000" +
"\"size\":SHM_SIZE" +
"}" +
"}" +
"}";
Expand Down Expand Up @@ -110,7 +110,9 @@ public void testEncode() {
data.put("ndArray", ndArray);
String json = Types.encode(data);
assertNotNull(json);
String expected = JSON.replaceAll("SHM_NAME", ndArray.shm().name());
String expected = JSON
.replaceAll("SHM_NAME", ndArray.shm().name())
.replaceAll("SHM_SIZE", "" + ndArray.shm().size());
assertEquals(expected, json);
}
}
Expand All @@ -119,11 +121,16 @@ public void testEncode() {
public void testDecode() {
Map<String, Object> data;
String shmName;
int shmSize;

// Create name shared memory segment and decode JSON block.
try (SharedMemory shm = SharedMemory.create(null, 4000)) {
shmName = shm.name();
data = Types.decode(JSON.replaceAll("SHM_NAME", shmName));
shmSize = shm.size();
String json = JSON
.replaceAll("SHM_NAME", shmName)
.replaceAll("SHM_SIZE", "" + shmSize);
data = Types.decode(json);
}

// Validate results.
Expand Down Expand Up @@ -158,7 +165,7 @@ public void testDecode() {
assertEquals(20, ndArray.shape().get(1));
assertEquals(25, ndArray.shape().get(2));
assertEquals(shmName, ndArray.shm().name());
assertEquals(4000, ndArray.shm().size());
assertEquals(shmSize, ndArray.shm().size());
}
}

Expand Down

0 comments on commit 4ea317b

Please sign in to comment.