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