Bug 2337: Fix Tx already sealed failure on Tx commit
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / MockDataChangeListener.java
index f2f49d1bf3e21f048b393d5f5c14a6e9595e05d6..5bbdcae93c2241cd8b9e7a87613735b69841973d 100644 (file)
@@ -9,6 +9,10 @@ package org.opendaylight.controller.cluster.datastore.utils;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.Uninterruptibles;
+import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
@@ -16,8 +20,6 @@ import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import com.google.common.collect.Lists;
-import com.google.common.util.concurrent.Uninterruptibles;
 
 /**
  * A mock DataChangeListener implementation.
@@ -27,14 +29,21 @@ import com.google.common.util.concurrent.Uninterruptibles;
 public class MockDataChangeListener implements
                          AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> {
 
-    private final List<AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>>
-                                                               changeList = Lists.newArrayList();
-    private final CountDownLatch changeLatch;
-    private final int expChangeEventCount;
+    private final List<AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>> changeList =
+            Collections.synchronizedList(Lists.<AsyncDataChangeEvent<YangInstanceIdentifier,
+                                                NormalizedNode<?, ?>>>newArrayList());
+
+    private volatile CountDownLatch changeLatch;
+    private int expChangeEventCount;
 
     public MockDataChangeListener(int expChangeEventCount) {
+        reset(expChangeEventCount);
+    }
+
+    public void reset(int expChangeEventCount) {
         changeLatch = new CountDownLatch(expChangeEventCount);
         this.expChangeEventCount = expChangeEventCount;
+        changeList.clear();
     }
 
     @Override
@@ -44,8 +53,13 @@ public class MockDataChangeListener implements
     }
 
     public void waitForChangeEvents(YangInstanceIdentifier... expPaths) {
-        assertEquals("Change notifications complete", true,
-                Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS));
+        boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS);
+        if(!done) {
+            fail(String.format("Missing change notifications. Expected: %d. Actual: %d",
+                    expChangeEventCount, (expChangeEventCount - changeLatch.getCount())));
+        }
+
+        assertEquals("Change notifications complete", true, done);
 
         for(int i = 0; i < expPaths.length; i++) {
             assertTrue(String.format("Change %d does not contain %s", (i+1), expPaths[i]),