build(test): 가용 메모리로 테스트 힙을 산정하고 워커 수명을 제한한다

This commit is contained in:
2026-11-19 14:21:56 +09:00
parent 1c270048a1
commit bc84faf7a8
5 changed files with 331 additions and 1 deletions
@@ -0,0 +1,149 @@
package buildlogic;
import java.util.Arrays;
import java.util.stream.Stream;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
class TestHeapPolicyTest {
private static final long MIB = 1L << 20;
private static final long GIB = 1L << 30;
@TestFactory
Stream<DynamicTest> shouldCalculateHeapWithinTheAvailableWorkerBudget() {
return Stream.of(
dynamicTest("10 GiB reaches the 4096 MiB floor", () ->
assertEquals(4096, TestHeapPolicy.calculateHeapMiB(10 * GIB, 1, 1))),
dynamicTest("20 GiB supports two workers at the floor", () ->
assertEquals(4096, TestHeapPolicy.calculateHeapMiB(20 * GIB, 2, 2))),
dynamicTest("13 GiB leaves half and the per-worker reserve", () ->
assertEquals(5632, TestHeapPolicy.calculateHeapMiB(13 * GIB, 1, 1))),
dynamicTest("max workers caps four requested forks", () ->
assertEquals(7168, TestHeapPolicy.calculateHeapMiB(32 * GIB, 4, 2))),
dynamicTest("max forks caps four available workers", () ->
assertEquals(7168, TestHeapPolicy.calculateHeapMiB(32 * GIB, 2, 4))),
dynamicTest("fractional MiB rounds down", () ->
assertEquals(5632, TestHeapPolicy.calculateHeapMiB(13 * GIB + 2 * MIB - 1, 1, 1))),
dynamicTest("an exact additional MiB is retained", () ->
assertEquals(5633, TestHeapPolicy.calculateHeapMiB(13 * GIB + 2 * MIB, 1, 1))),
dynamicTest("the largest positive long does not overflow", () ->
assertEquals(4_398_046_510_079L, TestHeapPolicy.calculateHeapMiB(Long.MAX_VALUE, 1, 1)))
);
}
@TestFactory
Stream<DynamicTest> shouldRejectInsufficientBudgetsWithActionableDiagnostics() {
return Stream.of(
new long[] {10 * GIB - 1, 1, 1},
new long[] {20 * GIB - 1, 4, 2},
new long[] {1, 1, 1},
new long[] {GIB, 1, 1},
new long[] {2 * GIB, 1, 1},
new long[] {8 * GIB, 1, 1}
).map(input -> dynamicTest(Arrays.toString(input), () -> {
IllegalStateException failure = assertThrows(IllegalStateException.class, () ->
TestHeapPolicy.calculateHeapMiB(input[0], (int) input[1], (int) input[2]));
String message = failure.getMessage();
assertAll(
() -> assertTrue(message.contains("availableBytes=" + input[0]), message),
() -> assertTrue(message.contains("effectiveWorkers=" + Math.min(input[1], input[2])), message),
() -> assertTrue(message.contains("minimumHeapMiB=4096"), message),
() -> assertTrue(message.contains("Close other applications"), message),
() -> assertTrue(message.contains("reduce workers"), message)
);
}));
}
@TestFactory
Stream<DynamicTest> shouldRejectNonpositiveAvailabilityOrWorkerLimits() {
return Stream.of(
new long[] {0, 1, 1},
new long[] {-1, 1, 1},
new long[] {Long.MIN_VALUE, 1, 1},
new long[] {10 * GIB, 0, 1},
new long[] {10 * GIB, -1, 1},
new long[] {10 * GIB, Integer.MIN_VALUE, 1},
new long[] {10 * GIB, 1, 0},
new long[] {10 * GIB, 1, -1},
new long[] {10 * GIB, 1, Integer.MIN_VALUE}
).map(input -> dynamicTest(Arrays.toString(input), () ->
assertThrows(IllegalArgumentException.class, () ->
TestHeapPolicy.calculateHeapMiB(input[0], (int) input[1], (int) input[2]))));
}
@TestFactory
Stream<DynamicTest> shouldParseOnlyRawFreeAndInactivePages() {
return Stream.of(
dynamicTest("4K pages ignore speculative and file-backed counts", () ->
assertEquals(2_371_584, TestHeapPolicy.parseMacVmStat("""
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 123.
Pages speculative: 100.
Pages inactive: 456.
File-backed pages: 999.
"""))),
dynamicTest("16K pages accept reordered counters and whitespace", () ->
assertEquals(81_920, TestHeapPolicy.parseMacVmStat("""
Mach Virtual Memory Statistics: (page size of 16384 bytes)
Pages inactive : 3.
Pages active: 999.
Pages free:\t2.
"""))),
dynamicTest("both raw counters may be zero", () ->
assertEquals(0, TestHeapPolicy.parseMacVmStat(vmStat("4096", "0", "0")))),
dynamicTest("counters need not have trailing periods", () ->
assertEquals(20_480, TestHeapPolicy.parseMacVmStat(
vmStat("4096", "2", "3").replace(".\n", "\n")))),
dynamicTest("the largest representable byte count remains valid", () ->
assertEquals(Long.MAX_VALUE, TestHeapPolicy.parseMacVmStat(vmStat("1", "9223372036854775806", "1"))))
);
}
@TestFactory
Stream<DynamicTest> shouldRejectMissingDuplicateMalformedNegativeOrOverflowingVmStatFields() {
String valid = vmStat("4096", "2", "3");
return Stream.of(
"",
"Pages free: 2.\nPages inactive: 3.\n",
"page size of 4096 bytes\nPages free: 2.\nPages inactive: 3.\n",
valid.replace("Pages free: 2.\n", ""),
valid.replace("Pages inactive: 3.\n", ""),
valid + "Pages free: 2.\n",
valid + "Pages inactive: 3.\n",
valid + "Mach Virtual Memory Statistics: (page size of 4096 bytes)\n",
valid + "Pages free: broken.\n",
vmStat("4096", "-1", "3"),
vmStat("4096", "2", "-1"),
vmStat("-4096", "2", "3"),
vmStat("0", "2", "3"),
vmStat("invalid", "2", "3"),
vmStat("4096", "2x", "3"),
vmStat("4096", "2", "3.5"),
valid.replace("Pages free:", "Pages free"),
valid.replace("bytes)", "bytes) trailing"),
vmStat("9223372036854775808", "2", "3"),
vmStat("4096", "9223372036854775808", "3"),
vmStat("1", "9223372036854775807", "1"),
vmStat("4096", "2251799813685248", "0")
).map(output -> dynamicTest("rejects " + output.replace('\n', ' '), () ->
assertThrows(IllegalArgumentException.class, () -> TestHeapPolicy.parseMacVmStat(output))));
}
@Test
void shouldReadPositiveAvailabilityFromTheCurrentOperatingSystem() {
assertTrue(TestHeapPolicy.readAvailableBytes() > 0);
}
private static String vmStat(String pageSize, String free, String inactive) {
return "Mach Virtual Memory Statistics: (page size of " + pageSize + " bytes)\n"
+ "Pages free: " + free + ".\nPages inactive: " + inactive + ".\n";
}
}