BUG-7003: Remove sleeping from Tests
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / CheckUtil.java
index 6d0e3697dbccb6c6495878fd15e4bf6d8304e3bf..1f57beb8f2351ab38edcf1673668e895da9bc22c 100644 (file)
@@ -7,65 +7,22 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Stopwatch;
 import com.google.common.util.concurrent.Uninterruptibles;
-import io.netty.util.concurrent.Future;
-import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
-import java.util.function.Function;
 import org.junit.Assert;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl.State;
 
 public final class CheckUtil {
-    private static final int TIMEOUT = 40;
-    private static final int LATCH_TIMEOUT = 10;
-    private static final int SLEEP_FOR = 20;
-    private static final int SLEEP_UNINTERRUPTIBLY = 50;
-    static void checkReceivedMessages(final SimpleSessionListener listener, final int numberOfMessages)
-        throws ReadFailedException {
+    public static void checkIdleState(final SimpleSessionListener listener) {
         final Stopwatch sw = Stopwatch.createStarted();
-        while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
-            if (listener.getListMsg().size() != numberOfMessages) {
-                Uninterruptibles.sleepUninterruptibly(SLEEP_UNINTERRUPTIBLY, TimeUnit.MILLISECONDS);
+        while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
+            if (State.IDLE != listener.getState()) {
+                Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
             } else {
                 return;
             }
         }
         Assert.fail();
     }
-
-    public static <R, T extends DataObject> R readData(final DataBroker dataBroker, final InstanceIdentifier<T> iid,
-        final Function<T, R> function)
-        throws ReadFailedException {
-        AssertionError lastError = null;
-        final Stopwatch sw = Stopwatch.createStarted();
-        while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
-            try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
-                final Optional<T> data = tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet();
-                if (data.isPresent()) {
-                    try {
-                        return function.apply(data.get());
-                    } catch (final AssertionError e) {
-                        lastError = e;
-                        Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
-                    }
-                }
-            }
-        }
-        Assert.fail(lastError.getMessage());
-        throw lastError;
-    }
-
-    @SuppressWarnings("unchecked")
-    public static <T extends Future> void waitFutureSuccess(final T future) {
-        final CountDownLatch latch = new CountDownLatch(1);
-        future.addListener(future1 -> latch.countDown());
-        Uninterruptibles.awaitUninterruptibly(latch, LATCH_TIMEOUT, TimeUnit.SECONDS);
-    }
 }
\ No newline at end of file