Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/notif/NotificationBufferTest.java
38867 views
/*1* Copyright (c) 2003, 2006, 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 the NotificationBuffer class.27* @author Eamonn McManus28* @run clean NotificationBufferTest29* @run build NotificationBufferTest NotificationSender NotificationSenderMBean30* @run main NotificationBufferTest31*/3233import java.util.Arrays;34import java.util.Collections;35import java.util.HashSet;36import java.util.List;37import java.util.Set;38import java.util.HashMap;3940import javax.management.MBeanServer;41import javax.management.MBeanServerFactory;42import javax.management.MBeanServerInvocationHandler;43import javax.management.MBeanServerNotification;44import javax.management.Notification;45import javax.management.NotificationFilter;46import javax.management.NotificationFilterSupport;47import javax.management.ObjectName;48import javax.management.loading.MLet;4950import javax.management.remote.NotificationResult;51import javax.management.remote.TargetedNotification;5253import com.sun.jmx.remote.internal.ArrayNotificationBuffer;54import com.sun.jmx.remote.internal.NotificationBufferFilter;55import com.sun.jmx.remote.internal.NotificationBuffer;5657public class NotificationBufferTest {5859public static void main(String[] args) {60// System.setProperty("java.util.logging.config.file",61// "../../../../logging.properties");62// // we are in <workspace>/build/test/JTwork/scratch63try {64// java.util.logging.LogManager.getLogManager().readConfiguration();65boolean ok = test();66if (ok) {67System.out.println("Test completed");68return;69} else {70System.out.println("Test failed!");71System.exit(1);72}73} catch (Exception e) {74System.err.println("Unexpected exception: " + e);75e.printStackTrace();76System.exit(1);77}78}7980private static boolean test() throws Exception {81MBeanServer mbs = MBeanServerFactory.createMBeanServer();8283Integer queuesize = new Integer(10);84HashMap env = new HashMap();85env.put(com.sun.jmx.remote.util.EnvHelp.BUFFER_SIZE_PROPERTY, queuesize);86final NotificationBuffer nb =87ArrayNotificationBuffer.getNotificationBuffer(mbs, env);8889final ObjectName senderName = new ObjectName("dom:type=sender");90final ObjectName wildcardName = new ObjectName("*:*");91final String notifType =92MBeanServerNotification.REGISTRATION_NOTIFICATION;9394Integer allListenerId = new Integer(99);95NotificationBufferFilter allListenerFilter =96makeFilter(allListenerId, wildcardName, null);97NotificationFilterSupport regFilter = new NotificationFilterSupport();98regFilter.enableType(notifType);99100// Get initial sequence number101NotificationResult nr =102nb.fetchNotifications(allListenerFilter, 0, 0L, 0);103int nnotifs = nr.getTargetedNotifications().length;104if (nnotifs > 0) {105System.out.println("Expected 0 notifs for initial fetch, " +106"got " + nnotifs);107return false;108}109System.out.println("Got 0 notifs for initial fetch, OK");110111long earliest = nr.getEarliestSequenceNumber();112long next = nr.getNextSequenceNumber();113if (earliest != next) {114System.out.println("Expected earliest==next in initial fetch, " +115"earliest=" + earliest + "; next=" + next);116return false;117}118System.out.println("Got earliest==next in initial fetch, OK");119120mbs.createMBean(MLet.class.getName(), null);121mbs.createMBean(NotificationSender.class.getName(), senderName);122123NotificationSenderMBean sender = (NotificationSenderMBean)124MBeanServerInvocationHandler.newProxyInstance(mbs,125senderName,126NotificationSenderMBean.class,127false);128129/* We test here that MBeans already present when the130NotificationBuffer was created get a listener for the131buffer, as do MBeans created later. The132MBeanServerDelegate was already present, while the133NotificationSender was created later. */134135// Check that the NotificationSender does indeed have a listener136/* Note we are dependent on the specifics of our JMX137implementation here. There is no guarantee that the MBean138creation listeners will have run to completion when139creation of the MBean returns. */140int nlisteners = sender.getListenerCount();141if (nlisteners != 1) {142System.out.println("Notification sender should have 1 listener, " +143"has " + nlisteners);144return false;145}146System.out.println("Notification sender has 1 listener, OK");147148// Now we should see two creation notifications149nr = nb.fetchNotifications(allListenerFilter, next, 0L,150Integer.MAX_VALUE);151TargetedNotification[] tns = nr.getTargetedNotifications();152if (tns.length != 2) {153System.out.println("Expected 2 notifs, got: " +154Arrays.asList(tns));155return false;156}157if (!(tns[0].getNotification() instanceof MBeanServerNotification)158|| !(tns[1].getNotification() instanceof MBeanServerNotification))159{160System.out.println("Expected 2 MBeanServerNotifications, got: " +161Arrays.asList(tns));162return false;163}164if (!tns[0].getListenerID().equals(tns[1].getListenerID())165|| !tns[0].getListenerID().equals(allListenerId)) {166System.out.println("Bad listener IDs: " + Arrays.asList(tns));167return false;168}169System.out.println("Got 2 different MBeanServerNotifications, OK");170171// If we ask for max 1 notifs, we should only get one172nr = nb.fetchNotifications(allListenerFilter, next, 0L, 1);173tns = nr.getTargetedNotifications();174if (tns.length != 1) {175System.out.println("Expected 1 notif, got: " + Arrays.asList(tns));176return false;177}178TargetedNotification tn1 = tns[0];179System.out.println("Got 1 notif when asked for 1, OK");180181// Now we should get the other one182nr = nb.fetchNotifications(allListenerFilter, nr.getNextSequenceNumber(),1830L, 1);184tns = nr.getTargetedNotifications();185if (tns.length != 1) {186System.out.println("Expected 1 notif, got: " + Arrays.asList(tns));187return false;188}189TargetedNotification tn2 = tns[0];190System.out.println("Got 1 notif when asked for 1 again, OK");191192if (tn1.getNotification() == tn2.getNotification()) {193System.out.println("Returned same notif twice: " + tn1);194return false;195}196System.out.println("2 creation notifs are different, OK");197198// Now we should get none (timeout is 0)199long oldNext = nr.getNextSequenceNumber();200nr = nb.fetchNotifications(allListenerFilter, oldNext, 0L,201Integer.MAX_VALUE);202tns = nr.getTargetedNotifications();203if (tns.length != 0) {204System.out.println("Expected 0 notifs, got: " +205Arrays.asList(tns));206return false;207}208System.out.println("Got 0 notifs with 0 timeout, OK");209if (nr.getNextSequenceNumber() != oldNext) {210System.out.println("Sequence number changed: " + oldNext + " -> " +211nr.getNextSequenceNumber());212return false;213}214System.out.println("Next seqno unchanged with 0 timeout, OK");215216// Check that timeouts work217long startTime = System.currentTimeMillis();218nr = nb.fetchNotifications(allListenerFilter, oldNext, 250L,219Integer.MAX_VALUE);220tns = nr.getTargetedNotifications();221if (tns.length != 0) {222System.out.println("Expected 0 notifs, got: " +223Arrays.asList(tns));224return false;225}226long endTime = System.currentTimeMillis();227long elapsed = endTime - startTime;228if (elapsed < 250L) {229System.out.println("Elapsed time shorter than timeout: " +230elapsed);231return false;232}233System.out.println("Timeout worked, OK");234235// Check that notification filtering works236NotificationFilter senderFilter = new NotificationFilter() {237public boolean isNotificationEnabled(Notification n) {238if (!(n instanceof MBeanServerNotification))239return false;240MBeanServerNotification mbsn = (MBeanServerNotification) n;241return (mbsn.getMBeanName().equals(senderName));242}243};244Integer senderListenerId = new Integer(88);245NotificationBufferFilter senderListenerFilter =246makeFilter(senderListenerId, wildcardName, senderFilter);247nr = nb.fetchNotifications(senderListenerFilter, 0, 1000L,248Integer.MAX_VALUE);249tns = nr.getTargetedNotifications();250if (tns.length != 1) {251System.out.println("Expected 1 notif, got: " + Arrays.asList(tns));252return false;253}254MBeanServerNotification mbsn =255(MBeanServerNotification) tns[0].getNotification();256if (!mbsn.getMBeanName().equals(senderName)) {257System.out.println("Expected notif with senderName, got: " +258mbsn + " (" + mbsn.getMBeanName() + ")");259return false;260}261System.out.println("Successfully applied NotificationFilter, OK");262263// Now send 8 notifs to fill up our 10-element buffer264sender.sendNotifs("tiddly.pom", 8);265nr = nb.fetchNotifications(allListenerFilter, 0, 1000L,266Integer.MAX_VALUE);267tns = nr.getTargetedNotifications();268if (tns.length != 10) {269System.out.println("Expected 10 notifs, got: " +270Arrays.asList(tns));271return false;272}273System.out.println("Got full buffer of 10 notifications, OK");274275// Check that the 10 notifs are the ones we expected276for (int i = 0; i < 10; i++) {277String expected =278(i < 2) ? notifType : "tiddly.pom";279String found = tns[i].getNotification().getType();280if (!found.equals(expected)) {281System.out.println("Notif " + i + " bad type: expected <" +282expected + ">, found <" + found + ">");283return false;284}285}286System.out.println("Notifs have right types, OK");287288// Check that ObjectName filtering works289NotificationBufferFilter senderNameFilter =290makeFilter(new Integer(66), senderName, null);291nr = nb.fetchNotifications(senderNameFilter, 0, 0L,292Integer.MAX_VALUE);293tns = nr.getTargetedNotifications();294if (tns.length != 8) {295System.out.println("Bad result from ObjectName filtering: " +296Arrays.asList(tns));297return false;298}299System.out.println("ObjectName filtering works, OK");300301// Send one more notif, which should cause the oldest one to drop302sender.sendNotifs("foo.bar", 1);303nr = nb.fetchNotifications(allListenerFilter, 0, 1000L,304Integer.MAX_VALUE);305if (nr.getEarliestSequenceNumber() <= earliest) {306System.out.println("Expected earliest to increase: " +307nr.getEarliestSequenceNumber() + " should be > "308+ earliest);309return false;310}311System.out.println("Earliest notif dropped, OK");312313// Check that the 10 notifs are the ones we expected314tns = nr.getTargetedNotifications();315for (int i = 0; i < 10; i++) {316String expected =317(i < 1) ? notifType318: (i < 9) ? "tiddly.pom" : "foo.bar";319String found = tns[i].getNotification().getType();320if (!found.equals(expected)) {321System.out.println("Notif " + i + " bad type: expected <" +322expected + ">, found <" + found + ">");323return false;324}325}326System.out.println("Notifs have right types, OK");327328// Apply a filter that only selects the first notif, with max notifs 1,329// then check that it skipped past the others even though it already330// had its 1 notif331NotificationBufferFilter firstFilter =332makeFilter(new Integer(55), wildcardName, regFilter);333nr = nb.fetchNotifications(firstFilter, 0, 1000L, 1);334tns = nr.getTargetedNotifications();335if (tns.length != 1336|| !tns[0].getNotification().getType().equals(notifType)) {337System.out.println("Unexpected return from filtered call: " +338Arrays.asList(tns));339return false;340}341nr = nb.fetchNotifications(allListenerFilter, nr.getNextSequenceNumber(),3420L, 1000);343tns = nr.getTargetedNotifications();344if (tns.length != 0) {345System.out.println("Expected 0 notifs, got: " +346Arrays.asList(tns));347return false;348}349350// Create a second, larger buffer, which should share the same notifs351nr = nb.fetchNotifications(allListenerFilter, 0,3521000L, Integer.MAX_VALUE);353queuesize = new Integer(20);354env.put(com.sun.jmx.remote.util.EnvHelp.BUFFER_SIZE_PROPERTY, queuesize);355NotificationBuffer nb2 =356ArrayNotificationBuffer.getNotificationBuffer(mbs, env);357NotificationResult nr2 =358nb2.fetchNotifications(allListenerFilter, 0,3591000L, Integer.MAX_VALUE);360if (nr.getEarliestSequenceNumber() != nr2.getEarliestSequenceNumber()361|| nr.getNextSequenceNumber() != nr2.getNextSequenceNumber()362|| !sameTargetedNotifs(nr.getTargetedNotifications(),363nr2.getTargetedNotifications()))364return false;365System.out.println("Adding second buffer preserved notif list, OK");366367// Check that the capacity is now 20368sender.sendNotifs("propter.hoc", 10);369nr2 = nb2.fetchNotifications(allListenerFilter, 0,3701000L, Integer.MAX_VALUE);371if (nr.getEarliestSequenceNumber() !=372nr2.getEarliestSequenceNumber()) {373System.out.println("Earliest seq number changed after notifs " +374"that should have fit");375return false;376}377TargetedNotification[] tns2 = new TargetedNotification[10];378Arrays.asList(nr2.getTargetedNotifications()).subList(0, 10).toArray(tns2);379if (!sameTargetedNotifs(nr.getTargetedNotifications(), tns2)) {380System.out.println("Early notifs changed after notifs " +381"that should have fit");382return false;383}384System.out.println("New notifications fit in now-larger buffer, OK");385386// Drop the second buffer and check that the capacity shrinks387nb2.dispose();388NotificationResult nr3 =389nb.fetchNotifications(allListenerFilter, 0,3901000L, Integer.MAX_VALUE);391if (nr3.getEarliestSequenceNumber() != nr.getNextSequenceNumber()) {392System.out.println("After shrink, notifs not dropped as expected");393return false;394}395if (nr3.getNextSequenceNumber() != nr2.getNextSequenceNumber()) {396System.out.println("After shrink, next seq no does not match");397return false;398}399tns2 = new TargetedNotification[10];400Arrays.asList(nr2.getTargetedNotifications()).subList(10, 20).toArray(tns2);401if (!sameTargetedNotifs(nr3.getTargetedNotifications(), tns2)) {402System.out.println("Later notifs not preserved after shrink");403return false;404}405System.out.println("Dropping second buffer shrank capacity, OK");406407// Final test: check that destroying the final shared buffer408// removes its listeners409nb.dispose();410nlisteners = sender.getListenerCount();411if (nlisteners != 0) {412System.out.println("Disposing buffer should leave 0 listeners, " +413"but notification sender has " + nlisteners);414return false;415}416System.out.println("Dropping first buffer drops listeners, OK");417418return true;419}420421private static boolean sameTargetedNotifs(TargetedNotification[] tn1,422TargetedNotification[] tn2) {423if (tn1.length != tn2.length) {424System.out.println("Not same length");425return false;426}427for (int i = 0; i < tn1.length; i++) {428TargetedNotification n1 = tn1[i];429TargetedNotification n2 = tn2[i];430if (n1.getNotification() != n2.getNotification()431|| !n1.getListenerID().equals(n2.getListenerID()))432return false;433}434return true;435}436437private static NotificationBufferFilter makeFilter(final Integer id,438final ObjectName pattern,439final NotificationFilter filter) {440return new NotificationBufferFilter() {441public void apply(List<TargetedNotification> notifs,442ObjectName source, Notification notif) {443if (pattern.apply(source)) {444if (filter == null || filter.isNotificationEnabled(notif))445notifs.add(new TargetedNotification(notif, id));446}447}448};449};450}451452453