User: ejort
Date: 02/02/02 06:56:32
Modified: src/main/org/jboss/test/jbossmx/compliance/timer
BasicTestCase.java
Log:
Synchronization in multi-threaded tests
Revision Changes Path
1.2 +50 -48
jbosstest/src/main/org/jboss/test/jbossmx/compliance/timer/BasicTestCase.java
Index: BasicTestCase.java
===================================================================
RCS file:
/cvsroot/jboss/jbosstest/src/main/org/jboss/test/jbossmx/compliance/timer/BasicTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BasicTestCase.java 2002/02/02 03:16:24 1.1
+++ BasicTestCase.java 2002/02/02 14:56:32 1.2
@@ -60,6 +60,7 @@
* Test a single notification works.
*/
public void testSingleNotification()
+ throws Exception
{
try
{
@@ -67,18 +68,10 @@
Integer id = addNotification("test", "hello", "data", calcTime(PERIOD),
0, 1);
- sleep(WAIT);
+ expectNotifications(1);
- assertEquals(1, receivedNotifications.size());
-
- // It should NOT be be registered!
- assertEquals(null, getNotificationType(id));
-
- stopTimerService();
- }
- catch (Exception e)
- {
- fail(e.toString());
+ if (getNotificationType(id) != null)
+ fail("Single notification still registered");
}
finally
{
@@ -90,25 +83,19 @@
* Test a repeated notification works.
*/
public void testRepeatedNotification()
+ throws Exception
{
try
{
startTimerService();
Integer id = addNotification("test", "hello", "data", calcTime(PERIOD),
PERIOD, REPEATS);
- sleep(WAIT * REPEATS);
-
- assertEquals(REPEATS, receivedNotifications.size());
+ expectNotifications(1);
+ expectNotifications(2);
- // It should NOT be be registered!
- assertEquals(null, getNotificationType(id));
-
- stopTimerService();
+ if (getNotificationType(id) != null)
+ fail("Repeated notification still registered");
}
- catch (Exception e)
- {
- fail(e.toString());
- }
finally
{
stopTimerService();
@@ -119,6 +106,7 @@
* Test infinite notification works.
*/
public void testInfiniteNotification()
+ throws Exception
{
try
{
@@ -126,18 +114,11 @@
Integer id = addNotification("test", "hello", "data", calcTime(PERIOD),
PERIOD, 0);
- sleep(WAIT * REPEATS);
-
- assertEquals(REPEATS, receivedNotifications.size());
-
- // It should still be registered!
- assertEquals("test", getNotificationType(id));
+ expectNotifications(1);
+ expectNotifications(2);
- stopTimerService();
- }
- catch (Exception e)
- {
- fail(e.toString());
+ if (getNotificationType(id) == null)
+ fail("Infinite notification not registered");
}
finally
{
@@ -167,12 +148,14 @@
/**
* Remove everything used by this test. Cannot report failures because
- * the test might have failed earlier.
+ * the test might have failed earlier. All notifications are removed,
+ * the RI hangs otherwise.
*/
private void stopTimerService()
{
try
{
+ server.invoke(timerName, "removeAllNotifications", new Object[0], new
String[0]);
server.invoke(timerName, "stop", new Object[0], new String[0]);
server.unregisterMBean(timerName);
MBeanServerFactory.releaseMBeanServer(server);
@@ -187,8 +170,34 @@
* @param handback not used
*/
public void handleNotification(Notification notification, Object handback)
+ {
+ synchronized (receivedNotifications)
+ {
+ receivedNotifications.add(notification);
+ receivedNotifications.notifyAll();
+ }
+ }
+
+ /**
+ * Wait for the timer notification and see if we have the correct number
+ * hopefully this should synchronize this test with the timer thread.
+ *
+ * @param expected the number of notifications expected
+ * @throws Exception when the notifications are incorrect
+ */
+ public void expectNotifications(int expected)
+ throws Exception
{
- receivedNotifications.add(notification);
+ synchronized (receivedNotifications)
+ {
+ if (receivedNotifications.size() > expected)
+ fail("too many notifications");
+ if (receivedNotifications.size() < expected)
+ {
+ receivedNotifications.wait(WAIT);
+ }
+ assertEquals(expected, receivedNotifications.size());
+ }
}
/**
@@ -222,6 +231,12 @@
private String getNotificationType(Integer id)
throws Exception
{
+ // This is called after the last expected notification
+ // The timer thread has notified us, but hasn't had time
+ // to remove the notification, give it chance, before
+ // checking for correct behaviour.
+ Thread.yield();
+
return (String) server.invoke(timerName, "getNotificationType",
new Object[] { id },
new String[] { "java.lang.Integer" });
@@ -235,18 +250,5 @@
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
- {
- Thread.currentThread().sleep(sleep);
- }
- catch (InterruptedException ignored) {}
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development