Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/monitor/StartStopTest.java
38839 views
1
/*
2
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 6222826
27
* @summary Test that tasks are cancelled properly when
28
* monitors are started and stopped in a loop.
29
* @author Luis-Miguel Alventosa
30
* @library /lib/testlibrary
31
* @run clean StartStopTest
32
* @run build jdk.testlibrary.* StartStopTest
33
* @run main/othervm/timeout=300 StartStopTest 1
34
* @run main/othervm/timeout=300 StartStopTest 2
35
* @run main/othervm/timeout=300 StartStopTest 3
36
* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 1
37
* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 2
38
* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 3
39
* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 1
40
* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 2
41
* @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 3
42
*/
43
44
import java.util.concurrent.atomic.AtomicInteger;
45
import javax.management.MBeanServer;
46
import javax.management.MBeanServerFactory;
47
import javax.management.Notification;
48
import javax.management.NotificationListener;
49
import javax.management.ObjectName;
50
import javax.management.monitor.CounterMonitor;
51
import javax.management.monitor.GaugeMonitor;
52
import javax.management.monitor.Monitor;
53
import javax.management.monitor.MonitorNotification;
54
import javax.management.monitor.StringMonitor;
55
56
import jdk.testlibrary.Utils;
57
58
public class StartStopTest {
59
static int maxPoolSize;
60
static final AtomicInteger counter = new AtomicInteger();
61
62
// MBean class
63
public class ObservedObject implements ObservedObjectMBean {
64
volatile public boolean called = false;
65
public Integer getInteger() {
66
task("Integer");
67
return 0;
68
}
69
public Double getDouble() {
70
task("Double");
71
return 0.0;
72
}
73
public String getString() {
74
task("String");
75
return "";
76
}
77
private void task(String prop) {
78
called = true;
79
final int c = counter.incrementAndGet();
80
echo("\tTASK [" + c + "] in get" + prop);
81
}
82
}
83
84
// MBean interface
85
public interface ObservedObjectMBean {
86
public Integer getInteger();
87
public Double getDouble();
88
public String getString();
89
}
90
91
/**
92
* Run test
93
*/
94
public int runTest(int monitorType) throws Exception {
95
96
int nTasks = maxPoolSize + 2;
97
ObjectName[] mbeanNames = new ObjectName[nTasks];
98
ObservedObject[] monitored = new ObservedObject[nTasks];
99
ObjectName[] monitorNames = new ObjectName[nTasks];
100
Monitor[] monitor = new Monitor[nTasks];
101
String[] attributes = { "Integer", "Double", "String" };
102
103
try {
104
echo(">>> CREATE MBeanServer");
105
MBeanServer server = MBeanServerFactory.newMBeanServer();
106
107
String domain = server.getDefaultDomain();
108
109
for (int i = 0; i < nTasks; i++) {
110
mbeanNames[i] =
111
new ObjectName(":type=ObservedObject,instance=" + (i + 1));
112
monitored[i] = new ObservedObject();
113
echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
114
server.registerMBean(monitored[i], mbeanNames[i]);
115
switch (monitorType) {
116
case 1:
117
monitorNames[i] = new ObjectName(":type=CounterMonitor," +
118
"instance=" + (i + 1));
119
monitor[i] = new CounterMonitor();
120
break;
121
case 2:
122
monitorNames[i] = new ObjectName(":type=GaugeMonitor," +
123
"instance=" + (i + 1));
124
monitor[i] = new GaugeMonitor();
125
break;
126
case 3:
127
monitorNames[i] = new ObjectName(":type=StringMonitor," +
128
"instance=" + (i + 1));
129
monitor[i] = new StringMonitor();
130
break;
131
default:
132
echo("Unsupported monitor type");
133
return 1;
134
}
135
echo(">>> CREATE Monitor = " + monitorNames[i].toString());
136
server.registerMBean(monitor[i], monitorNames[i]);
137
monitor[i].addObservedObject(mbeanNames[i]);
138
monitor[i].setObservedAttribute(attributes[monitorType-1]);
139
monitor[i].setGranularityPeriod(50);
140
}
141
142
for (int j = 0; j < 2; j++) {
143
echo(">>> Start MONITORS");
144
for (int i = 0; i < nTasks; i++)
145
monitor[i].start();
146
echo(">>> MONITORS started");
147
doSleep(500);
148
echo(">>> Check FLAGS true");
149
for (int i = 0; i < nTasks; i++)
150
if (!monitored[i].called) {
151
echo("KO: At least one attribute was not called");
152
return 1;
153
}
154
echo(">>> FLAGS checked true");
155
echo(">>> Stop MONITORS");
156
for (int i = 0; i < nTasks; i++)
157
monitor[i].stop();
158
echo(">>> MONITORS stopped");
159
doSleep(500);
160
echo(">>> Set FLAGS to false");
161
for (int i = 0; i < nTasks; i++)
162
monitored[i].called = false;
163
echo(">>> FLAGS set to false");
164
echo(">>> Check FLAGS remain false");
165
for (int i = 0; i < nTasks; i++)
166
if (monitored[i].called) {
167
echo("KO: At least one attribute " +
168
"continued to get called");
169
return 1;
170
}
171
echo(">>> FLAGS checked false");
172
}
173
} finally {
174
for (int i = 0; i < nTasks; i++)
175
if (monitor[i] != null)
176
monitor[i].stop();
177
}
178
179
return 0;
180
}
181
182
/*
183
* Print message
184
*/
185
private static void echo(String message) {
186
System.out.println(message);
187
}
188
189
/*
190
* Standalone entry point.
191
*
192
* Run the test and report to stdout.
193
*/
194
public static void main (String args[]) throws Exception {
195
Integer size = Integer.getInteger("jmx.x.monitor.maximum.pool.size");
196
if (size == null) {
197
maxPoolSize = 10;
198
echo(">>> MAXIMUM POOL SIZE = 10 [default value]");
199
} else {
200
maxPoolSize = size.intValue() < 1 ? 1 : size.intValue();
201
echo(">>> MAXIMUM POOL SIZE = " + maxPoolSize);
202
}
203
StartStopTest test = new StartStopTest();
204
int error = test.runTest(Integer.parseInt(args[0]));
205
if (error > 0) {
206
echo(">>> Unhappy Bye, Bye!");
207
throw new IllegalStateException(
208
"Test FAILED: Unexpected Maximum Pool Size Overflow!");
209
} else {
210
echo(">>> Happy Bye, Bye!");
211
}
212
}
213
214
private static void doSleep(long ms) throws Exception {
215
Thread.sleep(Math.round(ms * Utils.TIMEOUT_FACTOR));
216
}
217
}
218
219