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.internal; 34 35 import static org.junit.Assert.assertNotNull; 36 import static org.junit.Assert.assertTrue; 37 38 import org.junit.Test; 39 import org.openjdk.jmc.rjmx.IConnectionHandle; 40 import org.openjdk.jmc.rjmx.subscription.IMRISubscription; 41 import org.openjdk.jmc.rjmx.subscription.IMRIValueListener; 42 import org.openjdk.jmc.rjmx.subscription.IUpdatePolicy; 43 import org.openjdk.jmc.rjmx.subscription.MRI; 44 import org.openjdk.jmc.rjmx.subscription.MRI.Type; 45 import org.openjdk.jmc.rjmx.subscription.MRIValueEvent; 46 import org.openjdk.jmc.rjmx.subscription.PolicyFactory; 47 import org.openjdk.jmc.rjmx.subscription.internal.IIntervalUpdatePolicy; 48 import org.openjdk.jmc.rjmx.subscription.internal.SimpleUpdatePolicy; 49 import org.openjdk.jmc.rjmx.subscription.internal.UpdatePolicyToolkit; 50 import org.openjdk.jmc.rjmx.test.RjmxTestCase; 51 import org.openjdk.jmc.rjmx.test.testutil.TestToolkit; 52 53 public class UpdatePolicyTest extends RjmxTestCase implements IMRIValueListener { 54 55 private static int TEST_TIMEOUT_TIME = 30000; 56 private int m_counter = 0; 57 58 @Test 59 public void testUpdatePolicyLookup() throws Exception { 60 helpUpdatePolicyLookup(getExistingAttribute()); 61 } 62 63 @Test 64 public void testNonExistingUpdatePolicyLookup() throws Exception { 65 helpUpdatePolicyLookup(getNonExistingAttribute()); 66 } 67 68 private void helpUpdatePolicyLookup(MRI mri) throws Exception { 69 IUpdatePolicy policy = UpdatePolicyToolkit.getUpdatePolicy(getConnectionHandle(), mri); 70 assertNotNull("Policy is null!", policy); //$NON-NLS-1$ 71 } 72 73 @Test 74 public void testChangeUpdatePolicy() throws Exception { 75 helpChangeUpdatePolicy(getExistingAttribute()); 76 } 77 78 @Test 79 public void testChangeNonExistsingUpdatePolicy() throws Exception { 80 helpChangeUpdatePolicy(getNonExistingAttribute()); 81 } 82 83 private void helpChangeUpdatePolicy(MRI mri) throws Exception { 84 IUpdatePolicy oldPolicy = UpdatePolicyToolkit.getUpdatePolicy(getConnectionHandle(), mri); 85 assertNotNull(oldPolicy); 86 int oldUpdateTime = getUpdateTime(getConnectionHandle(), mri); 87 int newUpdateTime = oldUpdateTime * 2; 88 UpdatePolicyToolkit.setUpdatePolicy(getConnectionHandle(), mri, 89 PolicyFactory.createSimpleUpdatePolicy(newUpdateTime)); 90 IUpdatePolicy newPolicy = UpdatePolicyToolkit.getUpdatePolicy(getConnectionHandle(), mri); 91 assertNotNull(newPolicy); 92 assertTrue(newPolicy != oldPolicy); 93 assertTrue(newPolicy instanceof SimpleUpdatePolicy); 94 assertTrue(getUpdateTime(getConnectionHandle(), mri) == newUpdateTime); 95 UpdatePolicyToolkit.setUpdatePolicy(getConnectionHandle(), mri, oldPolicy); 96 assertTrue(getUpdateTime(getConnectionHandle(), mri) == oldUpdateTime); 97 } 98 99 @Test 100 public void testChangeSubscribedUpdatePolicy() throws Exception { 101 try { 102 getAttributeSubscriptionService().addMRIValueListener(getExistingAttribute(), this); 103 IUpdatePolicy oldUpdatePolicy = UpdatePolicyToolkit.getUpdatePolicy(getConnectionHandle(), 104 getExistingAttribute()); 105 int oldUpdateTime = getUpdateTime(getConnectionHandle(), getExistingAttribute()); 106 IMRISubscription subscription = getAttributeSubscriptionService() 107 .getMRISubscription(getExistingAttribute()); 108 if (subscription.getUpdatePolicy() instanceof IIntervalUpdatePolicy) { 109 IIntervalUpdatePolicy subscriptionPolicy = (IIntervalUpdatePolicy) subscription.getUpdatePolicy(); 110 int oldSubscriptionUpdateTime = subscriptionPolicy.getIntervalTime(); 111 assertTrue(oldUpdateTime == oldSubscriptionUpdateTime); 112 } 113 synchronized (this) { 114 int oldCounter = m_counter; 115 for (int i = 0; i < 2; i++) { 116 this.wait(TEST_TIMEOUT_TIME); 117 } 118 assertTrue(m_counter > oldCounter); 119 } 120 UpdatePolicyToolkit.setUpdatePolicy(getConnectionHandle(), getExistingAttribute(), 121 PolicyFactory.createSimpleUpdatePolicy(oldUpdateTime * 10)); 122 int newUpdateTime = getUpdateTime(getConnectionHandle(), getExistingAttribute()); 123 assertTrue(oldUpdateTime * 10 == newUpdateTime); 124 assertTrue(subscription.getUpdatePolicy() instanceof IIntervalUpdatePolicy); 125 IIntervalUpdatePolicy subscriptionPolicy = (IIntervalUpdatePolicy) subscription.getUpdatePolicy(); 126 int newSubscriptionUpdateTime = subscriptionPolicy.getIntervalTime(); 127 assertTrue(newUpdateTime == newSubscriptionUpdateTime); 128 synchronized (this) { 129 int oldCounter = m_counter; 130 for (int i = 0; i < 2; i++) { 131 this.wait(TEST_TIMEOUT_TIME); 132 } 133 assertTrue(m_counter > oldCounter); 134 } 135 UpdatePolicyToolkit.setUpdatePolicy(getConnectionHandle(), getExistingAttribute(), oldUpdatePolicy); 136 } finally { 137 getAttributeSubscriptionService().removeMRIValueListener(this); 138 } 139 } 140 141 @Test 142 public void testDifferentSubscriptionTimes() throws Exception { 143 try { 144 getAttributeSubscriptionService().addMRIValueListener(getExistingAttribute(), this); 145 IUpdatePolicy oldUpdatePolicy = UpdatePolicyToolkit.getUpdatePolicy(getConnectionHandle(), 146 getExistingAttribute()); 147 // System.out.println("[start] - " + System.currentTimeMillis()); 148 for (int time = 500; time <= 4000; time *= 2) { 149 UpdatePolicyToolkit.setUpdatePolicy(getConnectionHandle(), getExistingAttribute(), 150 PolicyFactory.createSimpleUpdatePolicy(time)); 151 int newUpdateTime = getUpdateTime(getConnectionHandle(), getExistingAttribute()); 152 assertTrue(newUpdateTime == time); 153 IIntervalUpdatePolicy subscriptionPolicy = (IIntervalUpdatePolicy) getAttributeSubscriptionService() 154 .getMRISubscription(getExistingAttribute()).getUpdatePolicy(); 155 int newSubscriptionUpdateTime = subscriptionPolicy.getIntervalTime(); 156 assertTrue(newUpdateTime == newSubscriptionUpdateTime); 157 synchronized (this) { 158 int oldCounter = m_counter; 159 for (int i = 0; i < 3; i++) { 160 this.wait(time * 2); 161 // System.out.println("[" + time + "-" + i + "] - " + System.currentTimeMillis()); 162 } 163 assertTrue(m_counter > oldCounter); 164 } 165 } 166 UpdatePolicyToolkit.setUpdatePolicy(getConnectionHandle(), getExistingAttribute(), oldUpdatePolicy); 167 } finally { 168 getAttributeSubscriptionService().removeMRIValueListener(this); 169 } 170 } 171 172 private MRI getExistingAttribute() { 173 return new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", "UsedPhysicalMemorySize"); //$NON-NLS-1$ //$NON-NLS-2$ 174 } 175 176 private MRI getNonExistingAttribute() { 177 return new MRI(Type.ATTRIBUTE, "this.could.possible.not:really=exist,as=an", "attribute"); //$NON-NLS-1$ //$NON-NLS-2$ 178 } 179 180 @Override 181 public void valueChanged(MRIValueEvent event) { 182 synchronized (this) { 183 TestToolkit.println(event); 184 if (event.getValue() != null) { 185 m_counter += 1; 186 notifyAll(); 187 } 188 } 189 } 190 191 private static int getUpdateTime(IConnectionHandle handle, MRI attributeDescriptor) { 192 return ((IIntervalUpdatePolicy) UpdatePolicyToolkit.getUpdatePolicy(handle, attributeDescriptor)) 193 .getIntervalTime(); 194 } 195 }