< prev index next >

application/tests/org.openjdk.jmc.rjmx.test/src/test/java/org/openjdk/jmc/rjmx/test/subscription/AttributeSubscriptionTest.java

Print this page




  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"); //$NON-NLS-1$ //$NON-NLS-2$
  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", //$NON-NLS-1$
  85                                         "AvailableProcessors"); //$NON-NLS-1$
  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", //$NON-NLS-1$
 113                                         "UsedPhysicalMemorySize"); //$NON-NLS-1$
 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"); //$NON-NLS-1$ //$NON-NLS-2$
 132                         getAttributeSubscriptionService().addMRIValueListener(tcad, this);
 133                         MRI tstcad = new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", "FreePhysicalMemorySize"); //$NON-NLS-1$ //$NON-NLS-2$
 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"); //$NON-NLS-1$ //$NON-NLS-2$
 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"); //$NON-NLS-1$ //$NON-NLS-2$
 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


  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
< prev index next >