Files
sodalive-backend-spring-boot/buildSrc/src/test/kotlin/buildlogic/TestHeapPolicyTest.kt
T

188 lines
8.3 KiB
Kotlin

package buildlogic
import org.junit.jupiter.api.Assertions.assertAll
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.DynamicTest.dynamicTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestFactory
class TestHeapPolicyTest {
@TestFactory
fun shouldCalculateHeapWithinTheAvailableWorkerBudget(): List<DynamicTest> = listOf(
dynamicTest("the reported Jenkins availability runs below the former fixed floor") {
assertEquals(879L, TestHeapPolicy.calculateHeapMiB(2_460_004_352L, 1, 1))
},
dynamicTest("1 GiB reserves native memory proportionally") {
assertEquals(384L, TestHeapPolicy.calculateHeapMiB(GIB, 1, 1))
},
dynamicTest("2 GiB does not lose the whole worker budget to a fixed reserve") {
assertEquals(768L, TestHeapPolicy.calculateHeapMiB(2 * GIB, 1, 1))
},
dynamicTest("8 GiB permits a heap below 4 GiB") {
assertEquals(3072L, TestHeapPolicy.calculateHeapMiB(8 * GIB, 1, 1))
},
dynamicTest("10 GiB scales without a policy floor") {
assertEquals(3840L, TestHeapPolicy.calculateHeapMiB(10 * GIB, 1, 1))
},
dynamicTest("20 GiB is shared between two workers") {
assertEquals(3840L, TestHeapPolicy.calculateHeapMiB(20 * GIB, 2, 2))
},
dynamicTest("13 GiB leaves half and a proportional native reserve") {
assertEquals(4992L, TestHeapPolicy.calculateHeapMiB(13 * GIB, 1, 1))
},
dynamicTest("max workers caps four requested forks") {
assertEquals(6144L, TestHeapPolicy.calculateHeapMiB(32 * GIB, 4, 2))
},
dynamicTest("max forks caps four available workers") {
assertEquals(6144L, TestHeapPolicy.calculateHeapMiB(32 * GIB, 2, 4))
},
dynamicTest("fractional MiB rounds down") {
assertEquals(4992L, TestHeapPolicy.calculateHeapMiB(13 * GIB + 2 * MIB, 1, 1))
},
dynamicTest("an exact proportional increase is retained") {
assertEquals(4995L, TestHeapPolicy.calculateHeapMiB(13 * GIB + 8 * MIB, 1, 1))
},
dynamicTest("one byte below the proportional increase rounds down") {
assertEquals(4994L, TestHeapPolicy.calculateHeapMiB(13 * GIB + 8 * MIB - 1, 1, 1))
},
dynamicTest("the JVM technical minimum is not replaced with an application floor") {
assertEquals(2L, TestHeapPolicy.calculateHeapMiB(5_592_406, 1, 1))
},
dynamicTest("the JVM technical minimum accounts for two workers") {
assertEquals(2L, TestHeapPolicy.calculateHeapMiB(11_184_812, 2, 2))
},
dynamicTest("the largest positive long does not overflow") {
assertEquals(3_298_534_883_327L, TestHeapPolicy.calculateHeapMiB(Long.MAX_VALUE, 1, 1))
}
)
@TestFactory
fun shouldRejectOnlyBudgetsTooSmallToStartTheJvm(): List<DynamicTest> = listOf(
Triple(5_592_405L, 1, 1),
Triple(11_184_811L, 4, 2),
Triple(1L, 1, 1),
Triple(MIB, 1, 1),
Triple(2 * MIB, 1, 1),
Triple(4 * MIB, 1, 1)
).map { (available, forks, workers) ->
dynamicTest("[$available, $forks, $workers]") {
val failure = assertThrows(IllegalStateException::class.java) {
TestHeapPolicy.calculateHeapMiB(available, forks, workers)
}
val message = requireNotNull(failure.message)
assertAll(
{ assertTrue(message.contains("availableBytes=$available"), message) },
{ assertTrue(message.contains("effectiveWorkers=${minOf(forks, workers)}"), message) },
{ assertTrue(message.contains("JVM requires at least 2 MiB"), message) },
{ assertTrue(message.contains("Close other applications"), message) },
{ assertTrue(message.contains("reduce workers"), message) }
)
}
}
@TestFactory
fun shouldRejectNonpositiveAvailabilityOrWorkerLimits(): List<DynamicTest> = listOf(
Triple(0L, 1, 1),
Triple(-1L, 1, 1),
Triple(Long.MIN_VALUE, 1, 1),
Triple(10 * GIB, 0, 1),
Triple(10 * GIB, -1, 1),
Triple(10 * GIB, Int.MIN_VALUE, 1),
Triple(10 * GIB, 1, 0),
Triple(10 * GIB, 1, -1),
Triple(10 * GIB, 1, Int.MIN_VALUE)
).map { (available, forks, workers) ->
dynamicTest("[$available, $forks, $workers]") {
assertThrows(IllegalArgumentException::class.java) {
TestHeapPolicy.calculateHeapMiB(available, forks, workers)
}
}
}
@TestFactory
fun shouldParseOnlyRawFreeAndInactivePages(): List<DynamicTest> = listOf(
dynamicTest("4K pages ignore speculative and file-backed counts") {
assertEquals(
2_371_584L,
TestHeapPolicy.parseMacVmStat(
"""
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 123.
Pages speculative: 100.
Pages inactive: 456.
File-backed pages: 999.
""".trimIndent()
)
)
},
dynamicTest("16K pages accept reordered counters and whitespace") {
assertEquals(
81_920L,
TestHeapPolicy.parseMacVmStat(
" Mach Virtual Memory Statistics: (page size of 16384 bytes)\n" +
" Pages inactive : 3.\nPages active: 999.\nPages free:\t2.\n"
)
)
},
dynamicTest("both raw counters may be zero") {
assertEquals(0L, TestHeapPolicy.parseMacVmStat(vmStat("4096", "0", "0")))
},
dynamicTest("counters need not have trailing periods") {
assertEquals(20_480L, 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
fun shouldRejectMissingDuplicateMalformedNegativeOrOverflowingVmStatFields(): List<DynamicTest> {
val valid = vmStat("4096", "2", "3")
return listOf(
"",
"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.java) { TestHeapPolicy.parseMacVmStat(output) }
}
}
}
@Test
fun shouldReadPositiveAvailabilityFromTheCurrentOperatingSystem() {
assertTrue(TestHeapPolicy.readAvailableBytes() > 0)
}
private fun vmStat(pageSize: String, free: String, inactive: String): String =
"Mach Virtual Memory Statistics: (page size of $pageSize bytes)\n" +
"Pages free: $free.\nPages inactive: $inactive.\n"
companion object {
private const val MIB = 1_048_576L
private const val GIB = 1_073_741_824L
}
}