User: ejort
Date: 02/02/02 06:56:32
Modified: src/main/org/jboss/test/jbossmx/compliance/monitor
BasicTestCase.java
Log:
Synchronization in multi-threaded tests
Revision Changes Path
1.2 +70 -183
jbosstest/src/main/org/jboss/test/jbossmx/compliance/monitor/BasicTestCase.java
Index: BasicTestCase.java
===================================================================
RCS file:
/cvsroot/jboss/jbosstest/src/main/org/jboss/test/jbossmx/compliance/monitor/BasicTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BasicTestCase.java 2002/02/02 03:16:21 1.1
+++ BasicTestCase.java 2002/02/02 14:56:32 1.2
@@ -83,6 +83,7 @@
* Test simple counter notification.
*/
public void testCounterSimpleNotification()
+ throws Exception
{
try
{
@@ -90,24 +91,11 @@
observedObject = new ObjectName("Monitor:type=CounterSupport");
observedAttribute = "Value";
startCounterService(false, 0, 0, 10);
- sleep(WAIT);
- assertEquals(0, receivedNotifications.size());
-
- setAttribute(new Integer(10));
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
- setAttribute(new Integer(9));
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute(new Integer(10));
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
- }
- catch (Exception e)
- {
- fail(e.toString());
+ setAttribute(null, 0);
+ setAttribute(new Integer(10), 1);
+ setAttribute(new Integer(9), 1);
+ setAttribute(new Integer(10), 2);
}
finally
{
@@ -119,6 +107,7 @@
* Test a counter in difference mode.
*/
public void testCounterDifferenceNotification()
+ throws Exception
{
try
{
@@ -126,28 +115,12 @@
observedObject = new ObjectName("Monitor:type=CounterSupport");
observedAttribute = "Value";
startCounterService(true, 0, 0, 10);
- sleep(WAIT);
- assertEquals(0, receivedNotifications.size());
-
- setAttribute(new Integer(10));
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute(new Integer(9));
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
- setAttribute(new Integer(10));
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute(new Integer(20));
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
- }
- catch (Exception e)
- {
- fail(e.toString());
+ setAttribute(null, 0);
+ setAttribute(new Integer(10), 1);
+ setAttribute(new Integer(9), 1);
+ setAttribute(new Integer(10), 1);
+ setAttribute(new Integer(20), 2);
}
finally
{
@@ -159,6 +132,7 @@
* Test simple gauge notification high and low.
*/
public void testGaugeSimpleBothNotification()
+ throws Exception
{
try
{
@@ -166,36 +140,14 @@
observedObject = new ObjectName("Monitor:type=GaugeSupport");
observedAttribute = "Value";
startGaugeService(true, true, false, 10, 0);
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute(new Integer(10));
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
-
- setAttribute(new Integer(9));
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
-
- setAttribute(new Integer(10));
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
-
- setAttribute(new Integer(0));
- sleep(WAIT);
- assertEquals(3, receivedNotifications.size());
- setAttribute(new Integer(1));
- sleep(WAIT);
- assertEquals(3, receivedNotifications.size());
-
- setAttribute(new Integer(0));
- sleep(WAIT);
- assertEquals(3, receivedNotifications.size());
- }
- catch (Exception e)
- {
- fail(e.toString());
+ setAttribute(null, 1);
+ setAttribute(new Integer(10), 2);
+ setAttribute(new Integer(9), 2);
+ setAttribute(new Integer(10), 2);
+ setAttribute(new Integer(0), 3);
+ setAttribute(new Integer(1), 3);
+ setAttribute(new Integer(0), 3);
}
finally
{
@@ -207,6 +159,7 @@
* Test simple gauge notification high.
*/
public void testGaugeSimpleHighNotification()
+ throws Exception
{
try
{
@@ -214,33 +167,14 @@
observedObject = new ObjectName("Monitor:type=GaugeSupport");
observedAttribute = "Value";
startGaugeService(true, false, false, 10, 0);
- sleep(WAIT);
- assertEquals(0, receivedNotifications.size());
-
- setAttribute(new Integer(10));
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute(new Integer(9));
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute(new Integer(10));
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute(new Integer(0));
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
- setAttribute(new Integer(10));
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
+ setAttribute(null, 0);
+ setAttribute(new Integer(10),1 );
+ setAttribute(new Integer(9), 1);
+ setAttribute(new Integer(10), 1);
+ setAttribute(new Integer(0), 1);
+ setAttribute(new Integer(10), 2);
}
- catch (Exception e)
- {
- fail(e.toString());
- }
finally
{
stopMonitorService();
@@ -251,6 +185,7 @@
* Test simple gauge notification low.
*/
public void testGaugeSimpleLowNotification()
+ throws Exception
{
try
{
@@ -258,33 +193,14 @@
observedObject = new ObjectName("Monitor:type=GaugeSupport");
observedAttribute = "Value";
startGaugeService(false, true, false, 10, 0);
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute(new Integer(10));
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute(new Integer(9));
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute(new Integer(0));
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
-
- setAttribute(new Integer(1));
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
- setAttribute(new Integer(0));
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
+ setAttribute(null, 1);
+ setAttribute(new Integer(10), 1);
+ setAttribute(new Integer(9), 1);
+ setAttribute(new Integer(0), 2);
+ setAttribute(new Integer(1), 2);
+ setAttribute(new Integer(0), 2);
}
- catch (Exception e)
- {
- fail(e.toString());
- }
finally
{
stopMonitorService();
@@ -295,6 +211,7 @@
* Test a String notification (both match and differ).
*/
public void testStringBothNotification()
+ throws Exception
{
try
{
@@ -302,21 +219,11 @@
observedObject = new ObjectName("Monitor:type=StringSupport");
observedAttribute = "Value";
startStringService(true, true, "test");
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute("test");
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
- setAttribute("not-test");
- sleep(WAIT);
- assertEquals(3, receivedNotifications.size());
+ setAttribute(null, 1);
+ setAttribute("test", 2);
+ setAttribute("not-test", 3);
}
- catch (Exception e)
- {
- fail(e.toString());
- }
finally
{
stopMonitorService();
@@ -327,6 +234,7 @@
* Test a String notification (just match).
*/
public void testStringMatchNotification()
+ throws Exception
{
try
{
@@ -334,21 +242,11 @@
observedObject = new ObjectName("Monitor:type=StringSupport");
observedAttribute = "Value";
startStringService(true, false, "test");
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute("test");
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
- setAttribute("not-test");
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
+ setAttribute(null, 1);
+ setAttribute("test", 2);
+ setAttribute("not-test", 2);
}
- catch (Exception e)
- {
- fail(e.toString());
- }
finally
{
stopMonitorService();
@@ -359,6 +257,7 @@
* Test a String notification (just differ).
*/
public void testStringDifferNotification()
+ throws Exception
{
try
{
@@ -366,21 +265,11 @@
observedObject = new ObjectName("Monitor:type=StringSupport");
observedAttribute = "Value";
startStringService(false, true, "test");
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
-
- setAttribute("test");
- sleep(WAIT);
- assertEquals(1, receivedNotifications.size());
- setAttribute("not-test");
- sleep(WAIT);
- assertEquals(2, receivedNotifications.size());
+ setAttribute(null, 1);
+ setAttribute("test", 1);
+ setAttribute("not-test", 2);
}
- catch (Exception e)
- {
- fail(e.toString());
- }
finally
{
stopMonitorService();
@@ -511,14 +400,31 @@
}
/**
- * Set an attribute
- * @param value the value to set
+ * Set an attribute and check the correct notifications are received
+ * @param value the value to set, null is past at the start - only
+ * the check is perform
+ * @param expected the expected number of notifications after setting
+ * the value
*/
- private void setAttribute(Object value)
+ private void setAttribute(Object value, int expected)
throws Exception
{
- Attribute attribute = new Attribute(observedAttribute, value);
- server.setAttribute(observedObject, attribute);
+ // Set the attribute unless the test has just started
+ if (value != null)
+ {
+ Attribute attribute = new Attribute(observedAttribute, value);
+ server.setAttribute(observedObject, attribute);
+ }
+
+ // Wait for the notification
+ synchronized (receivedNotifications)
+ {
+ if (receivedNotifications.size() > expected )
+ fail("too many notifications");
+ if (receivedNotifications.size() <= expected )
+ receivedNotifications.wait(WAIT);
+ assertEquals(expected, receivedNotifications.size());
+ }
}
/**
@@ -529,29 +435,10 @@
*/
public void handleNotification(Notification notification, Object handback)
{
- receivedNotifications.add(notification);
- }
-
- /**
- * Calculate the time using an offset from the current time.
- * @param offset the offset from the current time
- * @return the calculated time
- */
- private long calcTime(long offset)
- {
- return System.currentTimeMillis() + offset;
- }
-
- /**
- * Sleep for some time.
- * @param sleep the number of millis to sleep.
- */
- private void sleep(long sleep)
- {
- try
+ synchronized (receivedNotifications)
{
- Thread.currentThread().sleep(sleep);
+ receivedNotifications.add(notification);
+ receivedNotifications.notifyAll();
}
- catch (InterruptedException ignored) {}
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development