1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * The contents of this file are subject to the terms of either the Universal Permissive License 7 * v 1.0 as shown at http://oss.oracle.com/licenses/upl 8 * 9 * or the following license: 10 * 11 * Redistribution and use in source and binary forms, with or without modification, are permitted 12 * provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions 15 * and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 18 * conditions and the following disclaimer in the documentation and/or other materials provided with 19 * the distribution. 20 * 21 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to 22 * endorse or promote products derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 31 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 package org.openjdk.jmc.rjmx.test.subscription; 34 35 import static org.junit.Assert.assertNotNull; 36 import static org.junit.Assert.assertTrue; 37 import static org.junit.Assert.fail; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.openjdk.jmc.rjmx.IConnectionHandle; 42 import org.openjdk.jmc.rjmx.IServerHandle; 43 import org.openjdk.jmc.rjmx.subscription.IMRIValueListener; 44 import org.openjdk.jmc.rjmx.subscription.ISubscriptionService; 45 import org.openjdk.jmc.rjmx.subscription.MRI; 46 import org.openjdk.jmc.rjmx.subscription.MRI.Type; 47 import org.openjdk.jmc.rjmx.subscription.MRIValueEvent; 48 import org.openjdk.jmc.rjmx.test.LocalRJMXTestToolkit; 49 import org.openjdk.jmc.rjmx.test.RjmxTestCase; 50 import org.openjdk.jmc.rjmx.test.testutil.TestToolkit; 51 52 @SuppressWarnings("nls") 53 public class AttributeSubscriptionTest extends RjmxTestCase implements IMRIValueListener { 54 private static int TEST_TIMEOUT_TIME = 30000; 55 // private static int TEST_TIMEOUT_TIME = Integer.MAX_VALUE; 56 private int counter; 57 58 @Test 59 public void testSubscribeToCPULoad() throws Exception { 60 IConnectionHandle handle = IServerHandle.create(LocalRJMXTestToolkit.createDefaultDescriptor()).connect("Test"); 61 ISubscriptionService subscriptionService = handle.getServiceOrThrow(ISubscriptionService.class); 62 try { 63 MRI attributeDescriptor = new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", "SystemCpuLoad"); 64 subscriptionService.addMRIValueListener(attributeDescriptor, this); 65 synchronized (this) { 66 for (int i = 0; i < 4; i++) { 67 this.wait(TEST_TIMEOUT_TIME); 68 } 69 } 70 assertNotNull(subscriptionService.getLastMRIValueEvent(attributeDescriptor)); 71 } finally { 72 subscriptionService.removeMRIValueListener(this); 73 handle.close(); 74 } 75 assertTrue(getCounter() > 3); 76 } 77 78 @Test 79 public void testGetAttributeSubscriptionOneShot() throws Exception { 80 // Starting up a subscription on a one shot attribute. 81 IConnectionHandle handle = IServerHandle.create(LocalRJMXTestToolkit.createDefaultDescriptor()).connect("Test"); 82 ISubscriptionService subscriptionService = handle.getServiceOrThrow(ISubscriptionService.class); 83 try { 84 MRI availableProcessorsAttribute = new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", 85 "AvailableProcessors"); 86 87 subscriptionService.addMRIValueListener(availableProcessorsAttribute, this); 88 89 // Since it's a one shot and pretty fast, it may already have been retrieved... 90 if (subscriptionService.getLastMRIValueEvent(availableProcessorsAttribute) == null) { 91 synchronized (this) { 92 for (int i = 0; i < 2; i++) { 93 if (subscriptionService.getLastMRIValueEvent(availableProcessorsAttribute) != null) { 94 break; 95 } 96 wait(TEST_TIMEOUT_TIME); 97 } 98 } 99 } 100 assertNotNull(subscriptionService.getLastMRIValueEvent(availableProcessorsAttribute)); 101 assertNotNull(subscriptionService.getLastMRIValueEvent(availableProcessorsAttribute).getValue()); 102 } finally { 103 subscriptionService.removeMRIValueListener(this); 104 handle.close(); 105 } 106 } 107 108 @Test 109 public void testGetAttributeSubscriptionOne() { 110 try { 111 // Starting up a subscription on a one shot attribute. 112 MRI physicalMemoryUsedAttribute = new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", 113 "UsedPhysicalMemorySize"); 114 getAttributeSubscriptionService().addMRIValueListener(physicalMemoryUsedAttribute, this); 115 116 synchronized (this) { 117 wait(TEST_TIMEOUT_TIME); 118 } 119 assertNotNull(getAttributeSubscriptionService().getLastMRIValueEvent(physicalMemoryUsedAttribute)); 120 getAttributeSubscriptionService().removeMRIValueListener(this); 121 } catch (Exception e) { 122 e.printStackTrace(); 123 fail(e.getMessage()); 124 } 125 } 126 127 @Test 128 public void testGetAttributeSubscriptionTwo() { 129 try { 130 // Starting up a subscription on a one shot attribute. 131 MRI tcad = new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", "UsedPhysicalMemorySize"); 132 getAttributeSubscriptionService().addMRIValueListener(tcad, this); 133 MRI tstcad = new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", "FreePhysicalMemorySize"); 134 getAttributeSubscriptionService().addMRIValueListener(tstcad, this); 135 136 for (int i = 0; i < 7; i++) { 137 synchronized (this) { 138 wait(TEST_TIMEOUT_TIME); 139 if (getAttributeSubscriptionService().getLastMRIValueEvent(tcad) != null 140 && getAttributeSubscriptionService().getLastMRIValueEvent(tstcad) != null) { 141 break; 142 } 143 } 144 Thread.yield(); 145 } 146 assertNotNull(getAttributeSubscriptionService().getLastMRIValueEvent(tcad)); 147 assertNotNull(getAttributeSubscriptionService().getLastMRIValueEvent(tstcad)); 148 getAttributeSubscriptionService().removeMRIValueListener(this); 149 } catch (Exception e) { 150 e.printStackTrace(); 151 fail(e.getMessage()); 152 } 153 } 154 155 @Test 156 public void testGetSyntheticSubscription() { 157 try { 158 // Starting up a subscription on a one shot attribute. 159 MRI synthad = new MRI(Type.ATTRIBUTE, "java.lang:type=Memory", "HeapMemoryUsagePercent"); 160 getAttributeSubscriptionService().addMRIValueListener(synthad, this); 161 162 synchronized (this) { 163 wait(TEST_TIMEOUT_TIME); 164 } 165 assertNotNull(getAttributeSubscriptionService().getLastMRIValueEvent(synthad)); 166 getAttributeSubscriptionService().removeMRIValueListener(this); 167 } catch (Exception e) { 168 e.printStackTrace(); 169 fail(e.getMessage()); 170 } 171 } 172 173 @Test 174 public void testGetSyntheticPhysicalMemSubscription() { 175 try { 176 // Starting up a subscription on a one shot attribute. 177 178 MRI synthad = new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", "PhysicalMemoryUsagePercent"); 179 getAttributeSubscriptionService().addMRIValueListener(synthad, this); 180 181 synchronized (this) { 182 wait(TEST_TIMEOUT_TIME); 183 } 184 assertNotNull(getAttributeSubscriptionService().getLastMRIValueEvent(synthad)); 185 getAttributeSubscriptionService().removeMRIValueListener(this); 186 187 } catch (Exception e) { 188 e.printStackTrace(); 189 fail(e.getMessage()); 190 } 191 } 192 193 @Before 194 public void setUp() throws Exception { 195 counter = 0; 196 } 197 198 @Override 199 public void valueChanged(MRIValueEvent event) { 200 synchronized (this) { 201 TestToolkit.println(event); 202 if (event.getValue() != null) { 203 notify(); 204 } 205 incrementCounter(); 206 } 207 } 208 209 public synchronized int getCounter() { 210 return counter; 211 } 212 213 public synchronized void incrementCounter() { 214 counter += 1; 215 } 216 }