Path: blob/master/test/jdk/javax/management/remote/mandatory/notif/UnexpectedNotifTest.java
41155 views
/*1* Copyright (c) 2003, 2015, 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 765432126* @summary Tests whether a listener receives notifs emitted before the27* listener is registered.28* @author Shanliang JIANG29*30* @run clean UnexpectedNotifTest31* @run build UnexpectedNotifTest32* @run main UnexpectedNotifTest33*/3435import java.util.ArrayList;36import java.util.Collections;37import java.util.List;38import java.util.Map;39import javax.management.MBeanNotificationInfo;40import javax.management.MBeanServer;41import javax.management.MBeanServerConnection;42import javax.management.MBeanServerFactory;43import javax.management.Notification;44import javax.management.NotificationBroadcasterSupport;45import javax.management.NotificationListener;46import javax.management.ObjectName;47import javax.management.remote.JMXConnector;48import javax.management.remote.JMXConnectorFactory;49import javax.management.remote.JMXConnectorServer;50import javax.management.remote.JMXConnectorServerFactory;51import javax.management.remote.JMXServiceURL;52//53import javax.management.remote.rmi.RMIConnectorServer;5455public class UnexpectedNotifTest {5657public static void main(String[] args) throws Exception {58List<String> protos = new ArrayList<String>();59protos.add("rmi");60try {61Class.forName("javax.management.remote.jmxmp.JMXMPConnectorServer");62protos.add("jmxmp");63} catch (ClassNotFoundException e) {64// OK: JMXMP not present so don't test it.65}66for (String proto : protos)67test(proto);68}6970private static void test(String proto) throws Exception {71System.out.println("Unexpected notifications test for protocol " +72proto);73MBeanServer mbs = null;74try {75// Create a MBeanServer76//77mbs = MBeanServerFactory.createMBeanServer();7879// Create a NotificationEmitter MBean80//81mbean = new ObjectName ("Default:name=NotificationEmitter");82mbs.registerMBean(new NotificationEmitter(), mbean);8384// Create a connector server85//86url = new JMXServiceURL("service:jmx:" + proto + "://");8788server = JMXConnectorServerFactory.newJMXConnectorServer(url,89null,90mbs);9192mbs.registerMBean(93server,94new ObjectName("Default:name=ConnectorServer"));9596server.start();9798url = server.getAddress();99100for (int j = 0; j < 2; j++) {101test();102}103} finally {104// Stop server105//106server.stop();107// Release the MBeanServer108//109MBeanServerFactory.releaseMBeanServer(mbs);110}111}112113private static void test() throws Exception {114// Create client115//116JMXConnector connector = JMXConnectorFactory.connect(url);117MBeanServerConnection client = connector.getMBeanServerConnection();118119// Add listener at the client side120//121client.addNotificationListener(mbean, listener, null, null);122123// Cleanup124//125receivedNotifs = 0;126127// Ask to send notifs128//129Object[] params = new Object[] {new Integer(nb)};130String[] signatures = new String[] {"java.lang.Integer"};131132client.invoke(mbean, "sendNotifications", params, signatures);133134// Waiting...135//136synchronized (lock) {137for (int i = 0; i < 10; i++) {138if (receivedNotifs < nb) {139lock.wait(1000);140}141}142}143144// Waiting again to ensure no more notifs145//146Thread.sleep(3000);147148synchronized (lock) {149if (receivedNotifs != nb) {150throw new Exception("The client expected to receive " +151nb + " notifs, but got " + receivedNotifs);152}153}154155// Remove listener156//157client.removeNotificationListener(mbean, listener);158159connector.close();160}161162//--------------------------163// private classes164//--------------------------165166private static class Listener implements NotificationListener {167public void handleNotification(Notification notif, Object handback) {168System.out.println("Received: " + notif + " (" +169notif.getSequenceNumber() + ")");170synchronized(lock) {171if(++receivedNotifs == nb) {172lock.notifyAll();173} else if (receivedNotifs > nb) {174System.out.println("The client expected to receive " +175nb + " notifs, but got at least " +176receivedNotifs);177System.exit(1);178}179}180}181}182183public static class NotificationEmitter184extends NotificationBroadcasterSupport185implements NotificationEmitterMBean {186187/**188* Returns a NotificationInfo object containing the name of the Java189* class of the notification and the notification types sent by this190* notification broadcaster.191*/192public MBeanNotificationInfo[] getNotificationInfo() {193194MBeanNotificationInfo[] ntfInfoArray = new MBeanNotificationInfo[1];195196String[] ntfTypes = new String[1];197ntfTypes[0] = myType;198199ntfInfoArray[0] = new MBeanNotificationInfo(200ntfTypes,201"javax.management.Notification",202"Notifications sent by the NotificationEmitter");203return ntfInfoArray;204}205206/**207* Send a Notification object with the specified times.208* The sequence number will be from zero to times-1.209*210* @param nb The number of notifications to send211*/212public void sendNotifications(Integer nb) {213System.out.println("NotificationEmitter: asked to send " +214"notifications: " + nb);215216Notification notif;217for (int i = 1; i <= nb.intValue(); i++) {218notif = new Notification(myType, this, ++seqno);219sendNotification(notif);220}221}222223private String myType = "notification.my_notification";224}225226public interface NotificationEmitterMBean {227public void sendNotifications(Integer nb);228}229230private static JMXConnectorServer server;231private static JMXServiceURL url;232private static ObjectName mbean;233private static NotificationListener listener = new Listener();234235private static int nb = 10;236private static int receivedNotifs = 0;237private static int[] lock = new int[0];238private static volatile long seqno;239}240241242