Skip to content

Commit

Permalink
8283279: [Testbug] Improve TestGetSwapSpaceSize
Browse files Browse the repository at this point in the history
Backport-of: a77160065bb6f62314711514f7694fe50f0dc35b
  • Loading branch information
jerboaa committed Apr 7, 2022
1 parent e78f47e commit 7d3a528
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
28 changes: 24 additions & 4 deletions test/jdk/jdk/internal/platform/docker/GetFreeSwapSpaceSize.java
@@ -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." );
}
}
@@ -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.");
}
}

1 comment on commit 7d3a528

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.