BUG-8858: remove sleeps from test driver
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / IdIntsListener.java
index 5767008d05cd4d3bfebc34de3ae6933d248be469..3e644393f163975fb13ab220da40b2e80ab00fda 100644 (file)
@@ -9,22 +9,30 @@
 package org.opendaylight.controller.clustering.it.provider.impl;
 
 import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.SettableFuture;
 import java.util.Collection;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
 import javax.annotation.Nonnull;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
+import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
-import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class IdIntsListener implements DOMDataTreeChangeListener {
+public class IdIntsListener implements ClusteredDOMDataTreeChangeListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(IdIntsListener.class);
+    private static final long SECOND_AS_NANO = 1000000000;
 
     private NormalizedNode<?, ?> localCopy = null;
+    private final AtomicLong lastNotifTimestamp = new AtomicLong(0);
+    private ScheduledExecutorService executorService;
+    private ScheduledFuture<?> scheduledFuture;
 
     @Override
     public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
@@ -32,18 +40,23 @@ public class IdIntsListener implements DOMDataTreeChangeListener {
         // There should only be one candidate reported
         Preconditions.checkState(changes.size() == 1);
 
+        lastNotifTimestamp.set(System.nanoTime());
+
         // do not log the change into debug, only use trace since it will lead to OOM on default heap settings
         LOG.debug("Received data tree changed");
 
         changes.forEach(change -> {
             if (change.getRootNode().getDataAfter().isPresent()) {
-                LOG.trace("Received change, data before: {}, data after: ", change.getRootNode().getDataBefore().get(),
+                LOG.trace("Received change, data before: {}, data after: ",
+                        change.getRootNode().getDataBefore().isPresent()
+                                ? change.getRootNode().getDataBefore().get() : "",
                         change.getRootNode().getDataAfter().get());
 
                 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
                     localCopy = change.getRootNode().getDataAfter().get();
                 } else {
-                    LOG.debug("Ignoring notification: {}", change);
+                    LOG.warn("Ignoring notification.");
+                    LOG.trace("Ignored notification content: {}", change);
                 }
             } else {
                 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
@@ -51,7 +64,39 @@ public class IdIntsListener implements DOMDataTreeChangeListener {
         });
     }
 
+    public boolean hasTriggered() {
+        return localCopy != null;
+    }
+
     public boolean checkEqual(final NormalizedNode<?, ?> expected) {
         return localCopy.equals(expected);
     }
+
+    public Future<Void> tryFinishProcessing() {
+        executorService = Executors.newSingleThreadScheduledExecutor();
+        final SettableFuture<Void> settableFuture = SettableFuture.create();
+
+        scheduledFuture = executorService.scheduleAtFixedRate(new CheckFinishedTask(settableFuture),
+                0, 1, TimeUnit.SECONDS);
+        return settableFuture;
+    }
+
+    private class CheckFinishedTask implements Runnable {
+
+        private final SettableFuture<Void> future;
+
+        CheckFinishedTask(final SettableFuture<Void> future) {
+            this.future = future;
+        }
+
+        @Override
+        public void run() {
+            if (System.nanoTime() - lastNotifTimestamp.get() > SECOND_AS_NANO * 4) {
+                scheduledFuture.cancel(false);
+                future.set(null);
+
+                executorService.shutdown();
+            }
+        }
+    }
 }