1 /* 2 * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package nsk.monitoring.MemoryNotificationInfo.from; 25 26 import java.lang.management.*; 27 import javax.management.*; 28 import javax.management.openmbean.*; 29 import java.util.List; 30 import java.util.concurrent.atomic.AtomicReference; 31 import java.util.concurrent.SynchronousQueue; 32 import nsk.share.*; 33 import nsk.share.gc.Algorithms; 34 import nsk.share.gc.Memory; 35 import nsk.share.gc.gp.GarbageUtils; 36 import nsk.monitoring.share.*; 37 import nsk.share.test.Stresser; 38 39 public class from001 { 40 41 private static boolean testFailed = false; 42 main(String[] args)43 public static void main(String[] args) { 44 45 ArgumentHandler argHandler = new ArgumentHandler(args); 46 Log log = new Log(System.out, argHandler); 47 48 log.display("MemoryNotificationInfo/from/from001/from001.java test started."); 49 50 MemoryMonitor monitor = Monitor.getMemoryMonitor(log, argHandler); 51 MBeanServer mbs = Monitor.getMBeanServer(); 52 53 // 1. Check null CompositeData - null must be returned 54 MemoryNotificationInfo result = MemoryNotificationInfo.from(null); 55 56 if (result != null) { 57 log.complain("FAILURE 1."); 58 log.complain("MemoryNotificationInfo.from(null) returned " + result 59 + ", expected: null."); 60 testFailed = true; 61 } 62 63 log.display("null CompositeData check passed."); 64 65 // 2. Check CompositeData that doest not represnt 66 // MemoryNotificationInfo - IllegalArgumentException must be thrown 67 68 ObjectName mbeanObjectName = null; 69 CompositeData cdata = null; 70 try { 71 mbeanObjectName = new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME); 72 cdata = (CompositeData )mbs.getAttribute(mbeanObjectName, 73 "HeapMemoryUsage"); 74 } catch (Exception e) { 75 log.complain("Unexpected exception " + e); 76 e.printStackTrace(log.getOutStream()); 77 testFailed = true; 78 } 79 80 try { 81 result = MemoryNotificationInfo.from(cdata); 82 log.complain("FAILURE 2."); 83 log.complain("MemoryNotificationInfo.from(CompositeData) returned " 84 + result + ", expected: IllegalArgumentException."); 85 testFailed = true; 86 } catch (IllegalArgumentException e) { 87 88 // Expected: CompositeData doest not represnt MemoryNotificationInfo 89 } 90 91 log.display("check for CompositeData doest not represnt MemoryNotificationInfo passed."); 92 93 // 3. Check correct CompositeData 94 Object poolObject = null; 95 try { 96 mbeanObjectName = new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME); 97 mbs.addNotificationListener(mbeanObjectName, new from001Listener(), 98 null, null); 99 List<?> pools = monitor.getMemoryPoolMBeans(); 100 if (pools.isEmpty()) { 101 log.complain("No Memory Pool Beans found. Test case will hang/fail."); 102 testFailed = true; 103 } 104 105 for (int i = 0; i < pools.size(); i++) { 106 Object pool = pools.get(i); 107 if (monitor.isUsageThresholdSupported(pool)) { 108 if (monitor.getType(pool).equals(MemoryType.HEAP)) { 109 poolObject = pool; 110 monitor.setUsageThreshold(pool, 1); 111 log.display("Usage threshold set for pool :" + poolObject); 112 break; 113 } 114 } 115 } 116 } catch (Exception e) { 117 log.complain("Unexpected exception " + e); 118 e.printStackTrace(log.getOutStream()); 119 testFailed = true; 120 } 121 122 if (testFailed) { 123 throw new TestFailure("TEST FAILED. See log."); 124 } 125 126 // eat memory just to emmit notification 127 Stresser stresser = new Stresser(args) { 128 129 @Override 130 public boolean continueExecution() { 131 return from001Listener.data.get() == null 132 && super.continueExecution(); 133 } 134 }; 135 stresser.start(0);// we use timeout, not iterations 136 GarbageUtils.eatMemory(stresser); 137 138 boolean messageNotRecieved = true; 139 while(messageNotRecieved) { 140 try { 141 from001Listener.queue.take(); 142 messageNotRecieved = false; 143 } catch (InterruptedException e) { 144 messageNotRecieved = true; 145 } 146 } 147 148 result = MemoryNotificationInfo.from(from001Listener.data.get()); 149 try { 150 ObjectName poolObjectName = new ObjectName(monitor.getName(poolObject)); 151 ObjectName resultObjectName = new ObjectName( 152 ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE + 153 ",name=" + result.getPoolName()); 154 155 log.display("poolObjectName : " + poolObjectName + 156 " resultObjectName : " + resultObjectName); 157 158 if (!poolObjectName.equals(resultObjectName)) { 159 log.complain("FAILURE 3."); 160 log.complain("Wrong pool name : " + resultObjectName + 161 ", expected : " + poolObjectName); 162 testFailed = true; 163 } 164 165 } catch (Exception e) { 166 log.complain("Unexpected exception " + e); 167 e.printStackTrace(log.getOutStream()); 168 testFailed = true; 169 } 170 171 if (testFailed) { 172 throw new TestFailure("TEST FAILED. See log."); 173 } 174 175 log.display("Test passed."); 176 } 177 } 178 179 180 class from001Listener implements NotificationListener { 181 182 static AtomicReference<CompositeData> data = new AtomicReference<CompositeData>(); 183 static SynchronousQueue<Object> queue = new SynchronousQueue<Object>(); 184 handleNotification(Notification notification, Object handback)185 public void handleNotification(Notification notification, Object handback) { 186 if (data.get() != null) 187 return; 188 data.set((CompositeData) notification.getUserData()); 189 190 boolean messageNotSent = true; 191 while(messageNotSent){ 192 try { 193 queue.put(new Object()); 194 messageNotSent = false; 195 } catch(InterruptedException e) { 196 messageNotSent = true; 197 } 198 } 199 } 200 201 } 202