Path: blob/master/test/jdk/javax/management/remote/mandatory/notif/NotificationBufferTest.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 the NotificationBuffer class.27* @author Eamonn McManus28* @modules java.management/com.sun.jmx.remote.internal29* java.management/com.sun.jmx.remote.util30* @run clean NotificationBufferTest31* @run build NotificationBufferTest NotificationSender NotificationSenderMBean32* @run main NotificationBufferTest33*/3435import java.util.Arrays;36import java.util.Collections;37import java.util.HashSet;38import java.util.List;39import java.util.Set;40import java.util.HashMap;4142import javax.management.MBeanServer;43import javax.management.MBeanServerFactory;44import javax.management.MBeanServerInvocationHandler;45import javax.management.MBeanServerNotification;46import javax.management.Notification;47import javax.management.NotificationFilter;48import javax.management.NotificationFilterSupport;49import javax.management.ObjectName;50import javax.management.loading.MLet;5152import javax.management.remote.NotificationResult;53import javax.management.remote.TargetedNotification;5455import com.sun.jmx.remote.internal.ArrayNotificationBuffer;56import com.sun.jmx.remote.internal.NotificationBufferFilter;57import com.sun.jmx.remote.internal.NotificationBuffer;5859public class NotificationBufferTest {6061public static void main(String[] args) {62// System.setProperty("java.util.logging.config.file",63// "../../../../logging.properties");64// // we are in <workspace>/build/test/JTwork/scratch65try {66// java.util.logging.LogManager.getLogManager().readConfiguration();67boolean ok = test();68if (ok) {69System.out.println("Test completed");70return;71} else {72System.out.println("Test failed!");73System.exit(1);74}75} catch (Exception e) {76System.err.println("Unexpected exception: " + e);77e.printStackTrace();78System.exit(1);79}80}8182private static boolean test() throws Exception {83MBeanServer mbs = MBeanServerFactory.createMBeanServer();8485Integer queuesize = new Integer(10);86HashMap env = new HashMap();87env.put(com.sun.jmx.remote.util.EnvHelp.BUFFER_SIZE_PROPERTY, queuesize);88final NotificationBuffer nb =89ArrayNotificationBuffer.getNotificationBuffer(mbs, env);9091final ObjectName senderName = new ObjectName("dom:type=sender");92final ObjectName wildcardName = new ObjectName("*:*");93final String notifType =94MBeanServerNotification.REGISTRATION_NOTIFICATION;9596Integer allListenerId = new Integer(99);97NotificationBufferFilter allListenerFilter =98makeFilter(allListenerId, wildcardName, null);99NotificationFilterSupport regFilter = new NotificationFilterSupport();100regFilter.enableType(notifType);101102// Get initial sequence number103NotificationResult nr =104nb.fetchNotifications(allListenerFilter, 0, 0L, 0);105int nnotifs = nr.getTargetedNotifications().length;106if (nnotifs > 0) {107System.out.println("Expected 0 notifs for initial fetch, " +108"got " + nnotifs);109return false;110}111System.out.println("Got 0 notifs for initial fetch, OK");112113long earliest = nr.getEarliestSequenceNumber();114long next = nr.getNextSequenceNumber();115if (earliest != next) {116System.out.println("Expected earliest==next in initial fetch, " +117"earliest=" + earliest + "; next=" + next);118return false;119}120System.out.println("Got earliest==next in initial fetch, OK");121122mbs.createMBean(MLet.class.getName(), null);123mbs.createMBean(NotificationSender.class.getName(), senderName);124125NotificationSenderMBean sender = (NotificationSenderMBean)126MBeanServerInvocationHandler.newProxyInstance(mbs,127senderName,128NotificationSenderMBean.class,129false);130131/* We test here that MBeans already present when the132NotificationBuffer was created get a listener for the133buffer, as do MBeans created later. The134MBeanServerDelegate was already present, while the135NotificationSender was created later. */136137// Check that the NotificationSender does indeed have a listener138/* Note we are dependent on the specifics of our JMX139implementation here. There is no guarantee that the MBean140creation listeners will have run to completion when141creation of the MBean returns. */142int nlisteners = sender.getListenerCount();143if (nlisteners != 1) {144System.out.println("Notification sender should have 1 listener, " +145"has " + nlisteners);146return false;147}148System.out.println("Notification sender has 1 listener, OK");149150// Now we should see two creation notifications151nr = nb.fetchNotifications(allListenerFilter, next, 0L,152Integer.MAX_VALUE);153TargetedNotification[] tns = nr.getTargetedNotifications();154if (tns.length != 2) {155System.out.println("Expected 2 notifs, got: " +156Arrays.asList(tns));157return false;158}159if (!(tns[0].getNotification() instanceof MBeanServerNotification)160|| !(tns[1].getNotification() instanceof MBeanServerNotification))161{162System.out.println("Expected 2 MBeanServerNotifications, got: " +163Arrays.asList(tns));164return false;165}166if (!tns[0].getListenerID().equals(tns[1].getListenerID())167|| !tns[0].getListenerID().equals(allListenerId)) {168System.out.println("Bad listener IDs: " + Arrays.asList(tns));169return false;170}171System.out.println("Got 2 different MBeanServerNotifications, OK");172173// If we ask for max 1 notifs, we should only get one174nr = nb.fetchNotifications(allListenerFilter, next, 0L, 1);175tns = nr.getTargetedNotifications();176if (tns.length != 1) {177System.out.println("Expected 1 notif, got: " + Arrays.asList(tns));178return false;179}180TargetedNotification tn1 = tns[0];181System.out.println("Got 1 notif when asked for 1, OK");182183// Now we should get the other one184nr = nb.fetchNotifications(allListenerFilter, nr.getNextSequenceNumber(),1850L, 1);186tns = nr.getTargetedNotifications();187if (tns.length != 1) {188System.out.println("Expected 1 notif, got: " + Arrays.asList(tns));189return false;190}191TargetedNotification tn2 = tns[0];192System.out.println("Got 1 notif when asked for 1 again, OK");193194if (tn1.getNotification() == tn2.getNotification()) {195System.out.println("Returned same notif twice: " + tn1);196return false;197}198System.out.println("2 creation notifs are different, OK");199200// Now we should get none (timeout is 0)201long oldNext = nr.getNextSequenceNumber();202nr = nb.fetchNotifications(allListenerFilter, oldNext, 0L,203Integer.MAX_VALUE);204tns = nr.getTargetedNotifications();205if (tns.length != 0) {206System.out.println("Expected 0 notifs, got: " +207Arrays.asList(tns));208return false;209}210System.out.println("Got 0 notifs with 0 timeout, OK");211if (nr.getNextSequenceNumber() != oldNext) {212System.out.println("Sequence number changed: " + oldNext + " -> " +213nr.getNextSequenceNumber());214return false;215}216System.out.println("Next seqno unchanged with 0 timeout, OK");217218// Check that timeouts work219long startTime = System.currentTimeMillis();220nr = nb.fetchNotifications(allListenerFilter, oldNext, 250L,221Integer.MAX_VALUE);222tns = nr.getTargetedNotifications();223if (tns.length != 0) {224System.out.println("Expected 0 notifs, got: " +225Arrays.asList(tns));226return false;227}228long endTime = System.currentTimeMillis();229long elapsed = endTime - startTime;230if (elapsed < 250L) {231System.out.println("Elapsed time shorter than timeout: " +232elapsed);233return false;234}235System.out.println("Timeout worked, OK");236237// Check that notification filtering works238NotificationFilter senderFilter = new NotificationFilter() {239public boolean isNotificationEnabled(Notification n) {240if (!(n instanceof MBeanServerNotification))241return false;242MBeanServerNotification mbsn = (MBeanServerNotification) n;243return (mbsn.getMBeanName().equals(senderName));244}245};246Integer senderListenerId = new Integer(88);247NotificationBufferFilter senderListenerFilter =248makeFilter(senderListenerId, wildcardName, senderFilter);249nr = nb.fetchNotifications(senderListenerFilter, 0, 1000L,250Integer.MAX_VALUE);251tns = nr.getTargetedNotifications();252if (tns.length != 1) {253System.out.println("Expected 1 notif, got: " + Arrays.asList(tns));254return false;255}256MBeanServerNotification mbsn =257(MBeanServerNotification) tns[0].getNotification();258if (!mbsn.getMBeanName().equals(senderName)) {259System.out.println("Expected notif with senderName, got: " +260mbsn + " (" + mbsn.getMBeanName() + ")");261return false;262}263System.out.println("Successfully applied NotificationFilter, OK");264265// Now send 8 notifs to fill up our 10-element buffer266sender.sendNotifs("tiddly.pom", 8);267nr = nb.fetchNotifications(allListenerFilter, 0, 1000L,268Integer.MAX_VALUE);269tns = nr.getTargetedNotifications();270if (tns.length != 10) {271System.out.println("Expected 10 notifs, got: " +272Arrays.asList(tns));273return false;274}275System.out.println("Got full buffer of 10 notifications, OK");276277// Check that the 10 notifs are the ones we expected278for (int i = 0; i < 10; i++) {279String expected =280(i < 2) ? notifType : "tiddly.pom";281String found = tns[i].getNotification().getType();282if (!found.equals(expected)) {283System.out.println("Notif " + i + " bad type: expected <" +284expected + ">, found <" + found + ">");285return false;286}287}288System.out.println("Notifs have right types, OK");289290// Check that ObjectName filtering works291NotificationBufferFilter senderNameFilter =292makeFilter(new Integer(66), senderName, null);293nr = nb.fetchNotifications(senderNameFilter, 0, 0L,294Integer.MAX_VALUE);295tns = nr.getTargetedNotifications();296if (tns.length != 8) {297System.out.println("Bad result from ObjectName filtering: " +298Arrays.asList(tns));299return false;300}301System.out.println("ObjectName filtering works, OK");302303// Send one more notif, which should cause the oldest one to drop304sender.sendNotifs("foo.bar", 1);305nr = nb.fetchNotifications(allListenerFilter, 0, 1000L,306Integer.MAX_VALUE);307if (nr.getEarliestSequenceNumber() <= earliest) {308System.out.println("Expected earliest to increase: " +309nr.getEarliestSequenceNumber() + " should be > "310+ earliest);311return false;312}313System.out.println("Earliest notif dropped, OK");314315// Check that the 10 notifs are the ones we expected316tns = nr.getTargetedNotifications();317for (int i = 0; i < 10; i++) {318String expected =319(i < 1) ? notifType320: (i < 9) ? "tiddly.pom" : "foo.bar";321String found = tns[i].getNotification().getType();322if (!found.equals(expected)) {323System.out.println("Notif " + i + " bad type: expected <" +324expected + ">, found <" + found + ">");325return false;326}327}328System.out.println("Notifs have right types, OK");329330// Apply a filter that only selects the first notif, with max notifs 1,331// then check that it skipped past the others even though it already332// had its 1 notif333NotificationBufferFilter firstFilter =334makeFilter(new Integer(55), wildcardName, regFilter);335nr = nb.fetchNotifications(firstFilter, 0, 1000L, 1);336tns = nr.getTargetedNotifications();337if (tns.length != 1338|| !tns[0].getNotification().getType().equals(notifType)) {339System.out.println("Unexpected return from filtered call: " +340Arrays.asList(tns));341return false;342}343nr = nb.fetchNotifications(allListenerFilter, nr.getNextSequenceNumber(),3440L, 1000);345tns = nr.getTargetedNotifications();346if (tns.length != 0) {347System.out.println("Expected 0 notifs, got: " +348Arrays.asList(tns));349return false;350}351352// Create a second, larger buffer, which should share the same notifs353nr = nb.fetchNotifications(allListenerFilter, 0,3541000L, Integer.MAX_VALUE);355queuesize = new Integer(20);356env.put(com.sun.jmx.remote.util.EnvHelp.BUFFER_SIZE_PROPERTY, queuesize);357NotificationBuffer nb2 =358ArrayNotificationBuffer.getNotificationBuffer(mbs, env);359NotificationResult nr2 =360nb2.fetchNotifications(allListenerFilter, 0,3611000L, Integer.MAX_VALUE);362if (nr.getEarliestSequenceNumber() != nr2.getEarliestSequenceNumber()363|| nr.getNextSequenceNumber() != nr2.getNextSequenceNumber()364|| !sameTargetedNotifs(nr.getTargetedNotifications(),365nr2.getTargetedNotifications()))366return false;367System.out.println("Adding second buffer preserved notif list, OK");368369// Check that the capacity is now 20370sender.sendNotifs("propter.hoc", 10);371nr2 = nb2.fetchNotifications(allListenerFilter, 0,3721000L, Integer.MAX_VALUE);373if (nr.getEarliestSequenceNumber() !=374nr2.getEarliestSequenceNumber()) {375System.out.println("Earliest seq number changed after notifs " +376"that should have fit");377return false;378}379TargetedNotification[] tns2 = new TargetedNotification[10];380Arrays.asList(nr2.getTargetedNotifications()).subList(0, 10).toArray(tns2);381if (!sameTargetedNotifs(nr.getTargetedNotifications(), tns2)) {382System.out.println("Early notifs changed after notifs " +383"that should have fit");384return false;385}386System.out.println("New notifications fit in now-larger buffer, OK");387388// Drop the second buffer and check that the capacity shrinks389nb2.dispose();390NotificationResult nr3 =391nb.fetchNotifications(allListenerFilter, 0,3921000L, Integer.MAX_VALUE);393if (nr3.getEarliestSequenceNumber() != nr.getNextSequenceNumber()) {394System.out.println("After shrink, notifs not dropped as expected");395return false;396}397if (nr3.getNextSequenceNumber() != nr2.getNextSequenceNumber()) {398System.out.println("After shrink, next seq no does not match");399return false;400}401tns2 = new TargetedNotification[10];402Arrays.asList(nr2.getTargetedNotifications()).subList(10, 20).toArray(tns2);403if (!sameTargetedNotifs(nr3.getTargetedNotifications(), tns2)) {404System.out.println("Later notifs not preserved after shrink");405return false;406}407System.out.println("Dropping second buffer shrank capacity, OK");408409// Final test: check that destroying the final shared buffer410// removes its listeners411nb.dispose();412nlisteners = sender.getListenerCount();413if (nlisteners != 0) {414System.out.println("Disposing buffer should leave 0 listeners, " +415"but notification sender has " + nlisteners);416return false;417}418System.out.println("Dropping first buffer drops listeners, OK");419420return true;421}422423private static boolean sameTargetedNotifs(TargetedNotification[] tn1,424TargetedNotification[] tn2) {425if (tn1.length != tn2.length) {426System.out.println("Not same length");427return false;428}429for (int i = 0; i < tn1.length; i++) {430TargetedNotification n1 = tn1[i];431TargetedNotification n2 = tn2[i];432if (n1.getNotification() != n2.getNotification()433|| !n1.getListenerID().equals(n2.getListenerID()))434return false;435}436return true;437}438439private static NotificationBufferFilter makeFilter(final Integer id,440final ObjectName pattern,441final NotificationFilter filter) {442return new NotificationBufferFilter() {443public void apply(List<TargetedNotification> notifs,444ObjectName source, Notification notif) {445if (pattern.apply(source)) {446if (filter == null || filter.isNotificationEnabled(notif))447notifs.add(new TargetedNotification(notif, id));448}449}450};451};452}453454455