Skip to content

Commit 7d3a528

Browse files
committed
8283279: [Testbug] Improve TestGetSwapSpaceSize
Backport-of: a77160065bb6f62314711514f7694fe50f0dc35b
1 parent e78f47e commit 7d3a528

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

test/jdk/jdk/internal/platform/docker/GetFreeSwapSpaceSize.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
2+
* Copyright (C) 2020, 2022 THL A29 Limited, a Tencent company. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,16 +24,36 @@
2424
import com.sun.management.OperatingSystemMXBean;
2525
import java.lang.management.ManagementFactory;
2626

27+
// Usage:
28+
// GetFreeSwapSpaceSize <memoryAlloc> <expectedMemory> <memorySwapAlloc> <expectedSwap>
2729
public class GetFreeSwapSpaceSize {
2830
public static void main(String[] args) {
29-
System.out.println("TestGetFreeSwapSpaceSize");
31+
if (args.length != 4) {
32+
throw new RuntimeException("Unexpected arguments. Expected 4, got " + args.length);
33+
}
34+
String memoryAlloc = args[0];
35+
long expectedMemory = Long.parseLong(args[1]);
36+
String memorySwapAlloc = args[2];
37+
long expectedSwap = Long.parseLong(args[3]);
38+
System.out.println("TestGetFreeSwapSpaceSize (memory=" + memoryAlloc + ", memorySwap=" + memorySwapAlloc + ")");
39+
if (expectedSwap != 0) {
40+
throw new RuntimeException("Precondition of test not met: Expected swap size of 0, got: " + expectedSwap);
41+
}
3042
OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
43+
long osBeanTotalSwap = osBean.getTotalSwapSpaceSize();
44+
// Premise of this test is to test on a system where --memory and --memory-swap are set to
45+
// the same amount via the container engine (i.e. no swap). In that case the OSBean must
46+
// not report negative values for free swap space. Assert this precondition.
47+
if (osBeanTotalSwap != expectedSwap) {
48+
throw new RuntimeException("OperatingSystemMXBean.getTotalSwapSpaceSize() reported " + osBeanTotalSwap + " expected " + expectedSwap);
49+
}
50+
System.out.println("TestGetFreeSwapSpaceSize precondition met, osBeanTotalSwap = " + expectedSwap + ". Running test... ");
3151
for (int i = 0; i < 100; i++) {
3252
long size = osBean.getFreeSwapSpaceSize();
3353
if (size < 0) {
34-
System.out.println("Error: getFreeSwapSpaceSize returns " + size);
35-
System.exit(-1);
54+
throw new RuntimeException("Test failed! getFreeSwapSpaceSize returns " + size);
3655
}
3756
}
57+
System.out.println("TestGetFreeSwapSpaceSize PASSED." );
3858
}
3959
}

test/jdk/jdk/internal/platform/docker/TestGetFreeSwapSpaceSize.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
2+
* Copyright (C) 2020, 2022 THL A29 Limited, a Tencent company. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,7 +36,7 @@
3636
import jdk.test.lib.process.OutputAnalyzer;
3737

3838
public class TestGetFreeSwapSpaceSize {
39-
private static final String imageName = Common.imageName("memory");
39+
private static final String imageName = Common.imageName("osbeanSwapSpace");
4040

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

6060
private static void testGetFreeSwapSpaceSize(String memoryAllocation, String expectedMemory,
61-
String swapAllocation, String expectedSwap) throws Exception {
61+
String memorySwapAllocation, String expectedSwap) throws Exception {
6262
Common.logNewTestCase("TestGetFreeSwapSpaceSize");
6363

6464
DockerRunOptions opts = Common.newOpts(imageName, "GetFreeSwapSpaceSize")
65+
.addClassOptions(memoryAllocation, expectedMemory, memorySwapAllocation, expectedSwap)
6566
.addDockerOpts(
6667
"--memory", memoryAllocation,
67-
"--memory-swap", swapAllocation
68+
"--memory-swap", memorySwapAllocation
6869
);
6970

7071
OutputAnalyzer out = DockerTestUtils.dockerRunJava(opts);
7172
out.shouldHaveExitValue(0)
72-
.shouldContain("TestGetFreeSwapSpaceSize");
73+
.shouldContain("TestGetFreeSwapSpaceSize PASSED.");
7374
}
7475
}

0 commit comments

Comments
 (0)