blob: d1681316102c735d915da88fccae8079aa9c3aac [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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 * @run clean StartStopTest
31 * @run build StartStopTest
32 * @run main/othervm/timeout=300 StartStopTest 1
33 * @run main/othervm/timeout=300 StartStopTest 2
34 * @run main/othervm/timeout=300 StartStopTest 3
35 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 1
36 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 2
37 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=5 StartStopTest 3
38 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 1
39 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 2
40 * @run main/othervm/timeout=300 -Djmx.x.monitor.maximum.pool.size=-5 StartStopTest 3
41 */
42
43import java.util.concurrent.atomic.AtomicInteger;
44import javax.management.MBeanServer;
45import javax.management.MBeanServerFactory;
46import javax.management.Notification;
47import javax.management.NotificationListener;
48import javax.management.ObjectName;
49import javax.management.monitor.CounterMonitor;
50import javax.management.monitor.GaugeMonitor;
51import javax.management.monitor.Monitor;
52import javax.management.monitor.MonitorNotification;
53import javax.management.monitor.StringMonitor;
54
55public class StartStopTest {
56
57 static int maxPoolSize;
58 static final AtomicInteger counter = new AtomicInteger();
59
60 // MBean class
61 public class ObservedObject implements ObservedObjectMBean {
62 public boolean called = false;
63 public Integer getInteger() {
64 task("Integer");
65 return 0;
66 }
67 public Double getDouble() {
68 task("Double");
69 return 0.0;
70 }
71 public String getString() {
72 task("String");
73 return "";
74 }
75 private void task(String prop) {
76 called = true;
77 final int c = counter.incrementAndGet();
78 echo("\tTASK [" + c + "] in get" + prop);
79 }
80 }
81
82 // MBean interface
83 public interface ObservedObjectMBean {
84 public Integer getInteger();
85 public Double getDouble();
86 public String getString();
87 }
88
89 /**
90 * Run test
91 */
92 public int runTest(int monitorType) throws Exception {
93
94 int nTasks = maxPoolSize + 2;
95 ObjectName[] mbeanNames = new ObjectName[nTasks];
96 ObservedObject[] monitored = new ObservedObject[nTasks];
97 ObjectName[] monitorNames = new ObjectName[nTasks];
98 Monitor[] monitor = new Monitor[nTasks];
99 String[] attributes = { "Integer", "Double", "String" };
100
101 try {
102 echo(">>> CREATE MBeanServer");
103 MBeanServer server = MBeanServerFactory.newMBeanServer();
104
105 String domain = server.getDefaultDomain();
106
107 for (int i = 0; i < nTasks; i++) {
108 mbeanNames[i] =
109 new ObjectName(":type=ObservedObject,instance=" + (i + 1));
110 monitored[i] = new ObservedObject();
111 echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
112 server.registerMBean(monitored[i], mbeanNames[i]);
113 switch (monitorType) {
114 case 1:
115 monitorNames[i] = new ObjectName(":type=CounterMonitor," +
116 "instance=" + (i + 1));
117 monitor[i] = new CounterMonitor();
118 break;
119 case 2:
120 monitorNames[i] = new ObjectName(":type=GaugeMonitor," +
121 "instance=" + (i + 1));
122 monitor[i] = new GaugeMonitor();
123 break;
124 case 3:
125 monitorNames[i] = new ObjectName(":type=StringMonitor," +
126 "instance=" + (i + 1));
127 monitor[i] = new StringMonitor();
128 break;
129 default:
130 echo("Unsupported monitor type");
131 return 1;
132 }
133 echo(">>> CREATE Monitor = " + monitorNames[i].toString());
134 server.registerMBean(monitor[i], monitorNames[i]);
135 monitor[i].addObservedObject(mbeanNames[i]);
136 monitor[i].setObservedAttribute(attributes[monitorType-1]);
137 monitor[i].setGranularityPeriod(50);
138 }
139
140 for (int j = 0; j < 2; j++) {
141 echo(">>> Start MONITORS");
142 for (int i = 0; i < nTasks; i++)
143 monitor[i].start();
144 echo(">>> MONITORS started");
145 Thread.sleep(500);
146 echo(">>> Check FLAGS true");
147 for (int i = 0; i < nTasks; i++)
148 if (!monitored[i].called) {
149 echo("KO: At least one attribute was not called");
150 return 1;
151 }
152 echo(">>> FLAGS checked true");
153 echo(">>> Stop MONITORS");
154 for (int i = 0; i < nTasks; i++)
155 monitor[i].stop();
156 echo(">>> MONITORS stopped");
157 Thread.sleep(500);
158 echo(">>> Set FLAGS to false");
159 for (int i = 0; i < nTasks; i++)
160 monitored[i].called = false;
161 echo(">>> FLAGS set to false");
162 echo(">>> Check FLAGS remain false");
163 for (int i = 0; i < nTasks; i++)
164 if (monitored[i].called) {
165 echo("KO: At least one attribute " +
166 "continued to get called");
167 return 1;
168 }
169 echo(">>> FLAGS checked false");
170 }
171 } finally {
172 for (int i = 0; i < nTasks; i++)
173 if (monitor[i] != null)
174 monitor[i].stop();
175 }
176
177 return 0;
178 }
179
180 /*
181 * Print message
182 */
183 private static void echo(String message) {
184 System.out.println(message);
185 }
186
187 /*
188 * Standalone entry point.
189 *
190 * Run the test and report to stdout.
191 */
192 public static void main (String args[]) throws Exception {
193 Integer size = Integer.getInteger("jmx.x.monitor.maximum.pool.size");
194 if (size == null) {
195 maxPoolSize = 10;
196 echo(">>> MAXIMUM POOL SIZE = 10 [default value]");
197 } else {
198 maxPoolSize = size.intValue() < 1 ? 1 : size.intValue();
199 echo(">>> MAXIMUM POOL SIZE = " + maxPoolSize);
200 }
201 StartStopTest test = new StartStopTest();
202 int error = test.runTest(Integer.parseInt(args[0]));
203 if (error > 0) {
204 echo(">>> Unhappy Bye, Bye!");
205 throw new IllegalStateException(
206 "Test FAILED: Unexpected Maximum Pool Size Overflow!");
207 } else {
208 echo(">>> Happy Bye, Bye!");
209 }
210 }
211}