Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/notification/NotifExecutorTest.java
38840 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 466154526* @summary Tests to use an executor to send notifications.27* @author Shanliang JIANG28* @run clean NotifExecutorTest29* @run build NotifExecutorTest30* @run main NotifExecutorTest31*/3233// java imports34//35import java.io.IOException;36import java.util.concurrent.*;3738// JMX imports39//40import javax.management.*;41import javax.management.remote.*;4243public class NotifExecutorTest {4445public static void main(String[] args) throws Exception {46System.out.println("Tests to use an executor to send notifications.");4748final MBeanServer mbs = MBeanServerFactory.createMBeanServer();49final ObjectName mbean = new ObjectName ("Default:name=NotificationEmitter");50final MyListener myLister = new MyListener();51final NotificationListener nullListener = new NotificationListener() {52public void handleNotification(Notification n, Object hb) {53// nothing54}55};56final Object[] params = new Object[] {new Integer(nb)};57final String[] signatures = new String[] {"java.lang.Integer"};5859// test with null executor60System.out.println(">>> Test with a null executor.");61mbs.registerMBean(new NotificationEmitter(null), mbean);6263mbs.addNotificationListener(mbean, myLister, null, null);64mbs.addNotificationListener(mbean, nullListener, null, null);6566mbs.invoke(mbean, "sendNotifications", params, signatures);67check(nb, 0);6869mbs.unregisterMBean(mbean);7071// test with an executor72System.out.println(">>> Test with a executor.");73mbs.registerMBean(new NotificationEmitter(74new NotifExecutorTest.MyExecutor()), mbean);75mbs.addNotificationListener(mbean, myLister, null, null);76mbs.addNotificationListener(mbean, nullListener, null, null);7778mbs.invoke(mbean, "sendNotifications", params, signatures);7980check(nb, nb*2);8182// test without listener83System.out.println(">>> Test without listener.");8485mbs.removeNotificationListener(mbean, myLister);86mbs.removeNotificationListener(mbean, nullListener);8788mbs.invoke(mbean, "sendNotifications", params, signatures);89check(0, 0);90}9192private static void check(int notifs, int called) throws Exception {93// Waiting...94synchronized (lock) {95for (int i = 0; i < 10; i++) {96if (receivedNotifs < notifs) {97lock.wait(1000);98}99}100}101102// Waiting again to ensure no more notifs103//104Thread.sleep(1000);105106// checking107synchronized (lock) {108if (receivedNotifs != notifs) {109throw new RuntimeException("The listener expected to receive " +110notifs + " notifs, but got " + receivedNotifs);111} else {112System.out.println(">>> The listener recieved as expected: "+receivedNotifs);113}114115if (calledTimes != called) {116throw new RuntimeException("The notif executor should be called " +117called + " times, but got " + calledTimes);118} else {119System.out.println(">>> The executor was called as expected: "+calledTimes);120}121}122123// clean124receivedNotifs = 0;125calledTimes = 0;126}127128129//--------------------------130// private classes131//--------------------------132private static class MyListener implements NotificationListener {133public void handleNotification(Notification notif, Object handback) {134synchronized(lock) {135if(++receivedNotifs >= nb) {136lock.notifyAll();137}138}139}140}141142public static class NotificationEmitter143extends NotificationBroadcasterSupport144implements NotificationEmitterMBean {145146public NotificationEmitter(Executor executor) {147super(executor);148}149150/**151* Send a Notification object with the specified times.152* The sequence number will be from zero to times-1.153*154* @param nb The number of notifications to send155*/156public void sendNotifications(Integer nb) {157System.out.println(">>> NotificationEmitter: asked to send " +158"notifications: " + nb);159160Notification notif;161for (int i = 1; i <= nb.intValue(); i++) {162notif = new Notification(null, this, ++seqno);163super.sendNotification(notif);164}165}166}167168public interface NotificationEmitterMBean {169public void sendNotifications(Integer nb);170}171172public static class MyExecutor extends ThreadPoolExecutor {173public MyExecutor() {174super(1, 1, 1L, TimeUnit.MILLISECONDS,175new ArrayBlockingQueue(nb*5));176}177178public synchronized void execute(Runnable job) {179synchronized(lock) {180calledTimes++;181}182183super.execute(job);184}185}186187private static int nb = 10;188private static int receivedNotifs = 0;189private static int[] lock = new int[0];190private static volatile long seqno;191192private static int calledTimes = 0;193}194195196