public static void runTest(int offset, int counter[], int derivedGauge[], int threshold[]) throws Exception { // Retrieve the platform MBean server // System.out.println("\nRetrieve the platform MBean server"); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); String domain = mbs.getDefaultDomain(); // Create and register TestMBean // ObjectName name = new ObjectName(domain + ":type=" + Test.class.getName() + ",offset=" + offset); mbs.createMBean(Test.class.getName(), name); TestMBean mbean = (TestMBean) MBeanServerInvocationHandler.newProxyInstance( mbs, name, TestMBean.class, false); // Create and register CounterMonitorMBean // ObjectName cmn = new ObjectName(domain + ":type=" + CounterMonitor.class.getName() + ",offset=" + offset); CounterMonitor m = new CounterMonitor(); mbs.registerMBean(m, cmn); CounterMonitorMBean cm = (CounterMonitorMBean) MBeanServerInvocationHandler.newProxyInstance( mbs, cmn, CounterMonitorMBean.class, true); ((NotificationEmitter) cm).addNotificationListener( new Listener(), null, null); cm.addObservedObject(name); cm.setObservedAttribute("Counter"); cm.setGranularityPeriod(100); cm.setInitThreshold(1); cm.setOffset(offset); cm.setModulus(5); cm.setNotify(true); // Start the monitor // System.out.println("\nStart monitoring..."); cm.start(); // Play with counter // for (int i = 0; i < counter.length; i++) { mbean.setCounter(counter[i]); System.out.println("\nCounter = " + mbean.getCounter()); Integer derivedGaugeValue; // either pass or test timeout (killed by test harness) // see 8025207 do { Thread.sleep(150); derivedGaugeValue = (Integer) cm.getDerivedGauge(name); } while (derivedGaugeValue.intValue() != derivedGauge[i]); Number thresholdValue = cm.getThreshold(name); System.out.println("Threshold = " + thresholdValue); if (thresholdValue.intValue() != threshold[i]) { System.out.println("Wrong threshold! Current value = " + thresholdValue + " Expected value = " + threshold[i]); System.out.println("\nStop monitoring..."); cm.stop(); throw new IllegalArgumentException("wrong threshold"); } } // Stop the monitor // System.out.println("\nStop monitoring..."); cm.stop(); }
void run() throws Exception { final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); final ObjectName observedName = new ObjectName("a:b=c"); final ObjectName monitorName = new ObjectName("a:type=Monitor"); mbs.registerMBean(new CounterMonitor(), monitorName); final CounterMonitorMBean monitorProxy = JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class); final TestMBean observedProxy = JMX.newMBeanProxy(mbs, observedName, TestMBean.class); final Runnable sensitiveThing = new Runnable() { public void run() { doSensitiveThing(monitorProxy, observedName); } }; final Runnable nothing = new Runnable() { public void run() {} }; final Runnable withinGetAttribute = (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing; mbs.registerMBean(new Test(withinGetAttribute), observedName); monitorProxy.addObservedObject(observedName); monitorProxy.setObservedAttribute("Thing"); monitorProxy.setInitThreshold(100); monitorProxy.setGranularityPeriod(10L); // 10 ms monitorProxy.setNotify(true); final int initGetCount = observedProxy.getGetCount(); monitorProxy.start(); System.out.println("Checking GetCount, possible deadlock if timeout."); do { // 8038322. Until timeout of testing harness Thread.sleep(200); } while ((observedProxy.getGetCount()) == initGetCount); System.out.println("Done!"); // This won't show up as a deadlock in CTRL-\ or in // ThreadMXBean.findDeadlockedThreads(), because they don't // see that thread A is waiting for thread B (B.join()), and // thread B is waiting for a lock held by thread A // Now we know the monitor has observed the initial value, // so if we want to test notify behaviour we can trigger by // exceeding the threshold. if (when == When.IN_NOTIFY) { final AtomicInteger notifCount = new AtomicInteger(); final NotificationListener listener = new NotificationListener() { public void handleNotification(Notification n, Object h) { Thread t = new Thread(sensitiveThing); t.start(); try { t.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } notifCount.incrementAndGet(); } }; mbs.addNotificationListener(monitorName, listener, null, null); observedProxy.setThing(1000); System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout."); do { Thread.sleep(200); } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness System.out.println("Done"); } }
abstract void doSensitiveThing(CounterMonitorMBean monitorProxy, ObjectName observedName);
void run() throws Exception { final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); final ObjectName observedName = new ObjectName("a:b=c"); final ObjectName monitorName = new ObjectName("a:type=Monitor"); mbs.registerMBean(new CounterMonitor(), monitorName); final CounterMonitorMBean monitorProxy = JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class); final TestMBean observedProxy = JMX.newMBeanProxy(mbs, observedName, TestMBean.class); final Runnable sensitiveThing = new Runnable() { public void run() { doSensitiveThing(monitorProxy, observedName); } }; final Runnable nothing = new Runnable() { public void run() {} }; final Runnable withinGetAttribute = (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing; mbs.registerMBean(new Test(withinGetAttribute), observedName); monitorProxy.addObservedObject(observedName); monitorProxy.setObservedAttribute("Thing"); monitorProxy.setInitThreshold(100); monitorProxy.setGranularityPeriod(10L); // 10 ms monitorProxy.setNotify(true); monitorProxy.start(); final int initGetCount = observedProxy.getGetCount(); int getCount = initGetCount; for (int i = 0; i < 500; i++) { // 500 * 10 = 5 seconds getCount = observedProxy.getGetCount(); if (getCount != initGetCount) break; Thread.sleep(10); } if (getCount <= initGetCount) throw new Exception("Test failed: presumable deadlock"); // This won't show up as a deadlock in CTRL-\ or in // ThreadMXBean.findDeadlockedThreads(), because they don't // see that thread A is waiting for thread B (B.join()), and // thread B is waiting for a lock held by thread A // Now we know the monitor has observed the initial value, // so if we want to test notify behaviour we can trigger by // exceeding the threshold. if (when == When.IN_NOTIFY) { final AtomicInteger notifCount = new AtomicInteger(); final NotificationListener listener = new NotificationListener() { public void handleNotification(Notification n, Object h) { Thread t = new Thread(sensitiveThing); t.start(); try { t.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } notifCount.incrementAndGet(); } }; mbs.addNotificationListener(monitorName, listener, null, null); observedProxy.setThing(1000); for (int i = 0; i < 500 && notifCount.get() == 0; i++) Thread.sleep(10); if (notifCount.get() == 0) throw new Exception("Test failed: presumable deadlock"); } }
public static void runTest(int offset, int counter[], int derivedGauge[], int threshold[]) throws Exception { // Retrieve the platform MBean server // System.out.println("\nRetrieve the platform MBean server"); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); String domain = mbs.getDefaultDomain(); // Create and register TestMBean // ObjectName name = new ObjectName(domain + ":type=" + Test.class.getName() + ",offset=" + offset); mbs.createMBean(Test.class.getName(), name); TestMBean mbean = (TestMBean) MBeanServerInvocationHandler.newProxyInstance( mbs, name, TestMBean.class, false); // Create and register CounterMonitorMBean // ObjectName cmn = new ObjectName(domain + ":type=" + CounterMonitor.class.getName() + ",offset=" + offset); CounterMonitor m = new CounterMonitor(); mbs.registerMBean(m, cmn); CounterMonitorMBean cm = (CounterMonitorMBean) MBeanServerInvocationHandler.newProxyInstance( mbs, cmn, CounterMonitorMBean.class, true); ((NotificationEmitter) cm).addNotificationListener( new Listener(), null, null); cm.addObservedObject(name); cm.setObservedAttribute("Counter"); cm.setGranularityPeriod(100); cm.setInitThreshold(1); cm.setOffset(offset); cm.setModulus(5); cm.setNotify(true); // Start the monitor // System.out.println("\nStart monitoring..."); cm.start(); // Play with counter // for (int i = 0; i < counter.length; i++) { mbean.setCounter(counter[i]); System.out.println("\nCounter = " + mbean.getCounter()); Thread.sleep(300); Integer derivedGaugeValue = (Integer) cm.getDerivedGauge(name); System.out.println("Derived Gauge = " + derivedGaugeValue); if (derivedGaugeValue.intValue() != derivedGauge[i]) { System.out.println("Wrong derived gauge! Current value = " + derivedGaugeValue + " Expected value = " + derivedGauge[i]); System.out.println("\nStop monitoring..."); cm.stop(); throw new IllegalArgumentException("wrong derived gauge"); } Number thresholdValue = cm.getThreshold(name); System.out.println("Threshold = " + thresholdValue); if (thresholdValue.intValue() != threshold[i]) { System.out.println("Wrong threshold! Current value = " + thresholdValue + " Expected value = " + threshold[i]); System.out.println("\nStop monitoring..."); cm.stop(); throw new IllegalArgumentException("wrong threshold"); } Thread.sleep(300); } // Stop the monitor // System.out.println("\nStop monitoring..."); cm.stop(); }