[dna-commits] DNA SVN: r1416 - trunk/dna-jcr/src/test/java/org/jboss/dna/jcr.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Tue Dec 8 15:52:18 EST 2009


Author: elvisisking
Date: 2009-12-08 15:52:17 -0500 (Tue, 08 Dec 2009)
New Revision: 1416

Modified:
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrObservationManagerTest.java
Log:
DNA-579 JcrObservationManagerTest Should Check To See If Listeners Receive Too Many Events: Changed JcrObservationManagerTest  CountDownLatch to use the expected number of EventIterators (which is usually only one) instead of the expected number of events. Then when the going through the iterator I determine if the correct number of events have been received. An EventIterator is received each time a "transaction" is committed.

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrObservationManagerTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrObservationManagerTest.java	2009-12-07 21:12:58 UTC (rev 1415)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrObservationManagerTest.java	2009-12-08 20:52:17 UTC (rev 1416)
@@ -129,7 +129,18 @@
                               String[] uuids,
                               String[] nodeTypeNames,
                               boolean noLocal ) throws Exception {
-        TestListener listener = new TestListener(eventsExpected, eventTypes);
+        return addListener(eventsExpected, 1, eventTypes, absPath, isDeep, uuids, nodeTypeNames, noLocal);
+    }
+
+    TestListener addListener( int eventsExpected,
+                              int numIterators,
+                              int eventTypes,
+                              String absPath,
+                              boolean isDeep,
+                              String[] uuids,
+                              String[] nodeTypeNames,
+                              boolean noLocal ) throws Exception {
+        TestListener listener = new TestListener(eventsExpected, numIterators, eventTypes);
         this.session.getWorkspace().getObservationManager().addEventListener(listener,
                                                                              eventTypes,
                                                                              absPath,
@@ -913,7 +924,7 @@
 
         // register listeners
         TestListener addNodeListener = addListener(1, Event.NODE_ADDED, null, false, null, null, false);
-        TestListener removeNodeListener = addListener(2, Event.NODE_REMOVED, null, false, null, null, false);
+        TestListener removeNodeListener = addListener(2, 2, Event.NODE_REMOVED, null, false, null, null, false);
 
         // move node
         String oldPath = n2.getPath();
@@ -1645,13 +1656,12 @@
         private final CountDownLatch latch;
 
         public TestListener( int expectedEvents,
+                             int numIterators,
                              int eventTypes ) {
             this.eventTypes = eventTypes;
             this.expectedEvents = expectedEvents;
             this.events = new ArrayList<Event>();
-
-            // if no events are expected set it to 1 and let the timeout stop the test
-            this.latch = new CountDownLatch((this.expectedEvents == 0) ? 1 : this.expectedEvents);
+            this.latch = new CountDownLatch(numIterators);
         }
 
         public int getActualEventCount() {
@@ -1676,22 +1686,31 @@
          * @see javax.jcr.observation.EventListener#onEvent(javax.jcr.observation.EventIterator)
          */
         public void onEvent( EventIterator itr ) {
-            long position = itr.getPosition();
+            // this is called each time a "transaction" is committed. Most times this means after a session.save. But there are
+            // other times, like a workspace.move and a node.lock
+            try {
+                long position = itr.getPosition();
 
-            // iterator position must be set initially zero
-            if (position == 0) {
-                while (itr.hasNext()) {
-                    try {
+                // iterator position must be set initially zero
+                if (position == 0) {
+                    while (itr.hasNext()) {
                         Event event = itr.nextEvent();
+
                         // check iterator position
                         if (++position != itr.getPosition()) {
                             this.errorMessage = "EventIterator position was " + itr.getPosition() + " and should be " + position;
                             break;
                         }
 
+                        // add event to collection and increment total
                         this.events.add(event);
                         ++this.eventsProcessed;
 
+                        // check to make sure we haven't received too many events
+                        if (this.eventsProcessed > this.expectedEvents) {
+                            break;
+                        }
+
                         // check event type
                         int eventType = event.getType();
 
@@ -1699,18 +1718,18 @@
                             this.errorMessage = "Received a wrong event type of " + eventType;
                             break;
                         }
-                    } finally {
-                        // This has to be done LAST, otherwise waitForEvents() will return before the above stuff is done
-                        this.latch.countDown();
                     }
+                } else {
+                    this.errorMessage = "EventIterator position was not initially set to zero";
                 }
-            } else {
-                this.errorMessage = "EventIterator position was not initially set to zero";
+            } finally {
+                // This has to be done LAST, otherwise waitForEvents() will return before the above stuff is done
+                this.latch.countDown();
             }
         }
 
         public void waitForEvents() throws Exception {
-            this.latch.await(5, TimeUnit.SECONDS);
+            this.latch.await(2000, TimeUnit.MILLISECONDS);
         }
     }
 



More information about the dna-commits mailing list