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.
  • Loading branch information
ctrueden committed Sep 24, 2024
1 parent 59461a8 commit 7fa7c7b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 3 additions & 2 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 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 7fa7c7b

Please sign in to comment.