1
1
/*
2
- * Copyright (c) 2005, 2012 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2005, 2022 , Oracle and/or its affiliates. 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
22
22
*/
23
23
24
24
/*
25
- *
26
- *
27
- * @bug 6336608 6511738
25
+ * @test
26
+ * @bug 6336608 6511738 6367473
28
27
* @summary Basic unit test of OperatingSystemMXBean.getSystemLoadAverage()
29
28
* @author Mandy Chung
29
+ *
30
+ * @library /test/lib
31
+ *
32
+ * @run testng GetSystemLoadAverage
30
33
*/
31
34
32
35
/*
42
45
* running on Windows.
43
46
*/
44
47
45
- import java .lang .management .*;
46
- import java .io .*;
48
+ import jdk .test .lib .Platform ;
49
+ import org .testng .annotations .Test ;
50
+
51
+ import java .io .InputStreamReader ;
52
+ import java .io .Reader ;
53
+ import java .lang .management .ManagementFactory ;
54
+ import java .lang .management .OperatingSystemMXBean ;
47
55
48
56
public class GetSystemLoadAverage {
49
57
@@ -52,31 +60,41 @@ public class GetSystemLoadAverage {
52
60
53
61
// The system load average may be changing due to other jobs running.
54
62
// Allow some delta.
55
- private static double DELTA = 0.05 ;
56
-
57
- public static void main (String args []) throws Exception {
58
- if (args .length > 1 ) {
59
- throw new IllegalArgumentException ("Unexpected number of args " + args .length );
60
- }
61
-
62
- if (args .length == 0 ) {
63
- // On Linux or Solaris
64
- checkLoadAvg ();
65
- } else {
66
- // On Windows, the system load average is expected to be -1.0
67
- if (!args [0 ].equals ("-1.0" )) {
68
- throw new IllegalArgumentException ("Invalid argument: " + args [0 ]);
69
- } else {
70
- double loadavg = mbean .getSystemLoadAverage ();
71
- if (loadavg != -1.0 ) {
72
- throw new RuntimeException ("Expected load average : -1.0" +
73
- " but getSystemLoadAverage returned: " +
74
- loadavg );
63
+ private static final double DELTA = 0.05 ;
64
+
65
+ private static final int MAX_RETRIES = 5 ;
66
+ private static final int WAIT_TIME_MS = 5000 ;
67
+
68
+ @ Test (timeOut = (300 + WAIT_TIME_MS ) * MAX_RETRIES )
69
+ void testSystemLoadAvg () throws Exception {
70
+ for (int i = 1 ; i <= MAX_RETRIES ; i ++) {
71
+ try {
72
+ System .out .println (String .format ("Run %d: TestSystemLoadAvg" , i ));
73
+ if (!Platform .isWindows ()) {
74
+ // On Linux or Mac
75
+ checkLoadAvg ();
76
+ } else {
77
+ // On Windows, the system load average is expected to be -1.0
78
+ double loadavg = mbean .getSystemLoadAverage ();
79
+ if (loadavg != -1.0 ) {
80
+ throw new RuntimeException ("Expected load average : -1.0" +
81
+ " but getSystemLoadAverage returned: " + loadavg );
82
+ }
83
+ }
84
+ System .out .println (String .format ("Run %d: TestSystemLoadAvg test passed" , i ));
85
+ return ;
86
+ } catch (Exception e ) {
87
+ System .out .println (
88
+ String .format ("TEST FAILED: TestSystemLoadAvg test " + "failed %d runs" ,
89
+ i ));
90
+ if (i == MAX_RETRIES ) {
91
+ throw e ;
75
92
}
93
+
94
+ System .out .println ("Wait for 5 seconds" );
95
+ Thread .sleep (WAIT_TIME_MS );
76
96
}
77
97
}
78
-
79
- System .out .println ("Test passed." );
80
98
}
81
99
82
100
private static String LOAD_AVERAGE_TEXT
@@ -132,5 +150,4 @@ private static String commandOutput(Process p) throws Exception {
132
150
p .exitValue ();
133
151
return output ;
134
152
}
135
-
136
153
}
0 commit comments