|
1 | 1 | /* |
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
24 | 24 | import com.sun.management.OperatingSystemMXBean; |
25 | 25 | import java.lang.management.ManagementFactory; |
26 | 26 |
|
| 27 | +// Usage: |
| 28 | +// GetFreeSwapSpaceSize <memoryAlloc> <expectedMemory> <memorySwapAlloc> <expectedSwap> |
27 | 29 | public class GetFreeSwapSpaceSize { |
28 | 30 | 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 | + } |
30 | 42 | 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... "); |
31 | 51 | for (int i = 0; i < 100; i++) { |
32 | 52 | long size = osBean.getFreeSwapSpaceSize(); |
33 | 53 | if (size < 0) { |
34 | | - System.out.println("Error: getFreeSwapSpaceSize returns " + size); |
35 | | - System.exit(-1); |
| 54 | + throw new RuntimeException("Test failed! getFreeSwapSpaceSize returns " + size); |
36 | 55 | } |
37 | 56 | } |
| 57 | + System.out.println("TestGetFreeSwapSpaceSize PASSED." ); |
38 | 58 | } |
39 | 59 | } |
0 commit comments