Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/notif/UnexpectedNotifTest.java
38867 views
/*1* Copyright (c) 2003, 2008, 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* @run clean UnexpectedNotifTest30* @run build UnexpectedNotifTest31* @run main UnexpectedNotifTest32*/3334import java.util.ArrayList;35import java.util.Collections;36import java.util.List;37import java.util.Map;38import javax.management.MBeanNotificationInfo;39import javax.management.MBeanServer;40import javax.management.MBeanServerConnection;41import javax.management.MBeanServerFactory;42import javax.management.Notification;43import javax.management.NotificationBroadcasterSupport;44import javax.management.NotificationListener;45import javax.management.ObjectName;46import javax.management.remote.JMXConnector;47import javax.management.remote.JMXConnectorFactory;48import javax.management.remote.JMXConnectorServer;49import javax.management.remote.JMXConnectorServerFactory;50import javax.management.remote.JMXServiceURL;51//52import javax.management.remote.rmi.RMIConnectorServer;5354public class UnexpectedNotifTest {5556public static void main(String[] args) throws Exception {57List<String> protos = new ArrayList<String>();58protos.add("rmi");59try {60Class.forName("javax.management.remote.jmxmp.JMXMPConnectorServer");61protos.add("jmxmp");62} catch (ClassNotFoundException e) {63// OK: JMXMP not present so don't test it.64}65for (String proto : protos)66test(proto);67}6869private static void test(String proto) throws Exception {70System.out.println("Unexpected notifications test for protocol " +71proto);72MBeanServer mbs = null;73try {74// Create a MBeanServer75//76mbs = MBeanServerFactory.createMBeanServer();7778// Create a NotificationEmitter MBean79//80mbean = new ObjectName ("Default:name=NotificationEmitter");81mbs.registerMBean(new NotificationEmitter(), mbean);8283// Create a connector server84//85url = new JMXServiceURL("service:jmx:" + proto + "://");8687server = JMXConnectorServerFactory.newJMXConnectorServer(url,88null,89mbs);9091mbs.registerMBean(92server,93new ObjectName("Default:name=ConnectorServer"));9495server.start();9697url = server.getAddress();9899for (int j = 0; j < 2; j++) {100test();101}102} finally {103// Stop server104//105server.stop();106// Release the MBeanServer107//108MBeanServerFactory.releaseMBeanServer(mbs);109}110}111112private static void test() throws Exception {113// Create client114//115JMXConnector connector = JMXConnectorFactory.connect(url);116MBeanServerConnection client = connector.getMBeanServerConnection();117118// Add listener at the client side119//120client.addNotificationListener(mbean, listener, null, null);121122// Cleanup123//124receivedNotifs = 0;125126// Ask to send notifs127//128Object[] params = new Object[] {new Integer(nb)};129String[] signatures = new String[] {"java.lang.Integer"};130131client.invoke(mbean, "sendNotifications", params, signatures);132133// Waiting...134//135synchronized (lock) {136for (int i = 0; i < 10; i++) {137if (receivedNotifs < nb) {138lock.wait(1000);139}140}141}142143// Waiting again to ensure no more notifs144//145Thread.sleep(3000);146147synchronized (lock) {148if (receivedNotifs != nb) {149throw new Exception("The client expected to receive " +150nb + " notifs, but got " + receivedNotifs);151}152}153154// Remove listener155//156client.removeNotificationListener(mbean, listener);157158connector.close();159}160161//--------------------------162// private classes163//--------------------------164165private static class Listener implements NotificationListener {166public void handleNotification(Notification notif, Object handback) {167System.out.println("Received: " + notif + " (" +168notif.getSequenceNumber() + ")");169synchronized(lock) {170if(++receivedNotifs == nb) {171lock.notifyAll();172} else if (receivedNotifs > nb) {173System.out.println("The client expected to receive " +174nb + " notifs, but got at least " +175receivedNotifs);176System.exit(1);177}178}179}180}181182public static class NotificationEmitter183extends NotificationBroadcasterSupport184implements NotificationEmitterMBean {185186/**187* Returns a NotificationInfo object containing the name of the Java188* class of the notification and the notification types sent by this189* notification broadcaster.190*/191public MBeanNotificationInfo[] getNotificationInfo() {192193MBeanNotificationInfo[] ntfInfoArray = new MBeanNotificationInfo[1];194195String[] ntfTypes = new String[1];196ntfTypes[0] = myType;197198ntfInfoArray[0] = new MBeanNotificationInfo(199ntfTypes,200"javax.management.Notification",201"Notifications sent by the NotificationEmitter");202return ntfInfoArray;203}204205/**206* Send a Notification object with the specified times.207* The sequence number will be from zero to times-1.208*209* @param nb The number of notifications to send210*/211public void sendNotifications(Integer nb) {212System.out.println("NotificationEmitter: asked to send " +213"notifications: " + nb);214215Notification notif;216for (int i = 1; i <= nb.intValue(); i++) {217notif = new Notification(myType, this, ++seqno);218sendNotification(notif);219}220}221222private String myType = "notification.my_notification";223}224225public interface NotificationEmitterMBean {226public void sendNotifications(Integer nb);227}228229private static JMXConnectorServer server;230private static JMXServiceURL url;231private static ObjectName mbean;232private static NotificationListener listener = new Listener();233234private static int nb = 10;235private static int receivedNotifs = 0;236private static int[] lock = new int[0];237private static volatile long seqno;238}239240241