Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jerboaa committed Sep 10, 2024
1 parent 5b21006 commit 4f87715
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,44 @@ expectedPath, new CpuLimit(10_000, 20_000) /* two cores */,

assertNotNull(cpu.getCgroupPath());
assertEquals(expectedPath, cpu.getCgroupPath());
assertEquals(2, cpu.setCgroupPaths.size());
List<String> expectedList = List.of("/a/b/c", "/a/b");
// All paths below /a/b/c/d => /, /a, /a/b, /a/b/c and /a/b
assertEquals(5, cpu.setCgroupPaths.size());
// The full hierarchy walk, plus one setPath() call to the expected path
// due to the lowest limit
List<String> expectedList = List.of("/a/b/c",
expectedPath,
"/a",
"/",
expectedPath);
assertEquals(expectedList, cpu.setCgroupPaths);
}

@Test
public void adjustedLimitTwoLowerLimits() {
String cgroupPath = "/a/b/c/d";
String pathWithLimit = "/a/b/c";
String expectedPath = "/a";
long hostCpus = CgroupMetrics.getTotalCpuCount0();
assumeTrue(hostCpus > 2); // Skip on systems < 3 host cpus.
Map<String, CpuLimit> limits = Map.of(cgroupPath, UNLIMITED,
pathWithLimit, new CpuLimit(10_000, 20_000), /* two cores */
"/a/b", UNLIMITED,
expectedPath, new CpuLimit(10_000, 10_000), /* one core */
"/", UNLIMITED);
MockCgroupSubsystemCpuController cpu = new MockCgroupSubsystemCpuController(true, cgroupPath, limits);
CgroupUtil.adjustController(cpu);

assertNotNull(cpu.getCgroupPath());
assertEquals(expectedPath, cpu.getCgroupPath());
// All paths below /a/b/c/d => /, /a, /a/b, /a/b/c and /a
assertEquals(5, cpu.setCgroupPaths.size());
// The full hierarchy walk, plus one setPath() call to the expected path
// due to the lowest limit
List<String> expectedList = List.of(pathWithLimit,
"/a/b",
expectedPath,
"/",
expectedPath);
assertEquals(expectedList, cpu.setCgroupPaths);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
public class CgroupSubsystemMemoryControllerTest {

private static final Long UNLIMITED = -1L;
private static final Long FIVE_HUNDRED_MB = (long)500 * 1024 * 1024 * 1024;
private static final int MB = 1024 * 1024;
private static final Long FIVE_HUNDRED_MB = (long)500 * MB;
private static final Long FOUR_HUNDRED_MB = (long)400 * MB;

@Test
public void noAdjustementNeededIsNoop() {
Expand Down Expand Up @@ -83,14 +85,37 @@ public void adjustedLimitAtRoot() {
"/a/b", UNLIMITED,
"/a", UNLIMITED,
expectedPath, FIVE_HUNDRED_MB);
MockCgroupSubsystemMemoryController cpu = new MockCgroupSubsystemMemoryController(true, cgroupPath, limits);
CgroupUtil.adjustController(cpu);

assertNotNull(cpu.getCgroupPath());
assertEquals(expectedPath, cpu.getCgroupPath());
assertEquals(4, cpu.setCgroupPaths.size());
List<String> expectedList = List.of("/a/b/c", "/a/b", "/a", "/");
assertEquals(expectedList, cpu.setCgroupPaths);
MockCgroupSubsystemMemoryController memory = new MockCgroupSubsystemMemoryController(true, cgroupPath, limits);
CgroupUtil.adjustController(memory);

assertNotNull(memory.getCgroupPath());
assertEquals(expectedPath, memory.getCgroupPath());
// All paths below /a/b/c/d => /a/b/c, /a/b, /a, / and /
assertEquals(5, memory.setCgroupPaths.size());
List<String> expectedList = List.of("/a/b/c", "/a/b", "/a", "/", "/");
assertEquals(expectedList, memory.setCgroupPaths);
}

@Test
public void adjustedLimitTwoLimits() {
String cgroupPath = "/a/b/c/d/e";
String expectedPath = "/a";
Map<String, Long> limits = Map.of(cgroupPath, FIVE_HUNDRED_MB,
"/a/b/c/d", UNLIMITED,
"/a/b/c", UNLIMITED,
"/a/b", UNLIMITED,
expectedPath, FOUR_HUNDRED_MB,
"/", UNLIMITED);
MockCgroupSubsystemMemoryController memory = new MockCgroupSubsystemMemoryController(true, cgroupPath, limits);
CgroupUtil.adjustController(memory);

assertNotNull(memory.getCgroupPath());
assertEquals(expectedPath, memory.getCgroupPath());
// All paths below /a/b/c/d/e => /a/b/c/d, /a/b/c, /a/b, /a, / and /a
assertEquals(6, memory.setCgroupPaths.size());
List<String> expectedList = List.of("/a/b/c/d", "/a/b/c",
"/a/b", "/a", "/", "/a");
assertEquals(expectedList, memory.setCgroupPaths);
}

private static class MockCgroupSubsystemMemoryController implements CgroupSubsystemMemoryController {
Expand Down

0 comments on commit 4f87715

Please sign in to comment.