From 7fa7c7bc019022fd5a4d07aa298f9c9a41da588e Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Tue, 24 Sep 2024 11:44:34 -0500 Subject: [PATCH] Fix shm test failures on macOS 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. --- .../java/org/apposed/appose/SharedMemoryTest.java | 5 +++-- src/test/java/org/apposed/appose/TypesTest.java | 15 +++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/apposed/appose/SharedMemoryTest.java b/src/test/java/org/apposed/appose/SharedMemoryTest.java index 59b4e6a..f03ca41 100644 --- a/src/test/java/org/apposed/appose/SharedMemoryTest.java +++ b/src/test/java/org/apposed/appose/SharedMemoryTest.java @@ -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}. @@ -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. @@ -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)) { diff --git a/src/test/java/org/apposed/appose/TypesTest.java b/src/test/java/org/apposed/appose/TypesTest.java index 06e7e39..2d218fe 100644 --- a/src/test/java/org/apposed/appose/TypesTest.java +++ b/src/test/java/org/apposed/appose/TypesTest.java @@ -70,7 +70,7 @@ public class TypesTest { "\"shm\":{" + "\"appose_type\":\"shm\"," + "\"name\":\"SHM_NAME\"," + - "\"size\":4000" + + "\"size\":SHM_SIZE" + "}" + "}" + "}"; @@ -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); } } @@ -119,11 +121,16 @@ public void testEncode() { public void testDecode() { Map 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. @@ -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()); } }