Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions test/jdk/jdk/internal/platform/docker/GetFreeSwapSpaceSize.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
* Copyright (C) 2020, 2022 THL A29 Limited, a Tencent company. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,16 +24,36 @@
import com.sun.management.OperatingSystemMXBean;
import java.lang.management.ManagementFactory;

// Usage:
// GetFreeSwapSpaceSize <memoryAlloc> <expectedMemory> <memorySwapAlloc> <expectedSwap>
public class GetFreeSwapSpaceSize {
public static void main(String[] args) {
System.out.println("TestGetFreeSwapSpaceSize");
if (args.length != 4) {
throw new RuntimeException("Unexpected arguments. Expected 4, got " + args.length);
}
String memoryAlloc = args[0];
long expectedMemory = Long.parseLong(args[1]);
String memorySwapAlloc = args[2];
long expectedSwap = Long.parseLong(args[3]);
System.out.println("TestGetFreeSwapSpaceSize (memory=" + memoryAlloc + ", memorySwap=" + memorySwapAlloc + ")");
if (expectedSwap != 0) {
throw new RuntimeException("Precondition of test not met: Expected swap size of 0, got: " + expectedSwap);
}
OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long osBeanTotalSwap = osBean.getTotalSwapSpaceSize();
// Premise of this test is to test on a system where --memory and --memory-swap are set to
// the same amount via the container engine (i.e. no swap). In that case the OSBean must
// not report negative values for free swap space. Assert this precondition.
if (osBeanTotalSwap != expectedSwap) {
throw new RuntimeException("OperatingSystemMXBean.getTotalSwapSpaceSize() reported " + osBeanTotalSwap + " expected " + expectedSwap);
}
System.out.println("TestGetFreeSwapSpaceSize precondition met, osBeanTotalSwap = " + expectedSwap + ". Running test... ");
for (int i = 0; i < 100; i++) {
long size = osBean.getFreeSwapSpaceSize();
if (size < 0) {
System.out.println("Error: getFreeSwapSpaceSize returns " + size);
System.exit(-1);
throw new RuntimeException("Test failed! getFreeSwapSpaceSize returns " + size);
}
}
System.out.println("TestGetFreeSwapSpaceSize PASSED." );
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
* Copyright (C) 2020, 2022 THL A29 Limited, a Tencent company. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -36,7 +36,7 @@
import jdk.test.lib.process.OutputAnalyzer;

public class TestGetFreeSwapSpaceSize {
private static final String imageName = Common.imageName("memory");
private static final String imageName = Common.imageName("osbeanSwapSpace");

public static void main(String[] args) throws Exception {
if (!DockerTestUtils.canTestDocker()) {
Expand All @@ -58,17 +58,18 @@ public static void main(String[] args) throws Exception {
}

private static void testGetFreeSwapSpaceSize(String memoryAllocation, String expectedMemory,
String swapAllocation, String expectedSwap) throws Exception {
String memorySwapAllocation, String expectedSwap) throws Exception {
Common.logNewTestCase("TestGetFreeSwapSpaceSize");

DockerRunOptions opts = Common.newOpts(imageName, "GetFreeSwapSpaceSize")
.addClassOptions(memoryAllocation, expectedMemory, memorySwapAllocation, expectedSwap)
.addDockerOpts(
"--memory", memoryAllocation,
"--memory-swap", swapAllocation
"--memory-swap", memorySwapAllocation
);

OutputAnalyzer out = DockerTestUtils.dockerRunJava(opts);
out.shouldHaveExitValue(0)
.shouldContain("TestGetFreeSwapSpaceSize");
.shouldContain("TestGetFreeSwapSpaceSize PASSED.");
}
}