diff --git a/src/test/java/org/apposed/appose/SharedMemoryTest.java b/src/test/java/org/apposed/appose/SharedMemoryTest.java index 59b4e6a..05fd7c2 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. @@ -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 @@ -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()); } }