BUG-7003: Remove sleeping from Tests
[bgpcep.git] / testtool-util / src / main / java / org / opendaylight / protocol / util / CheckUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.util;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Stopwatch;
12 import com.google.common.base.Verify;
13 import com.google.common.util.concurrent.Uninterruptibles;
14 import io.netty.util.concurrent.Future;
15 import java.util.concurrent.CountDownLatch;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.TimeUnit;
18 import java.util.function.Function;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25
26 public final class CheckUtil {
27     private static final int LATCH_TIMEOUT = 10;
28     private static final int SLEEP_FOR = 200;
29     private static final int TIMEOUT = 30;
30     private CheckUtil() {
31         throw new UnsupportedOperationException();
32     }
33
34     @SuppressWarnings("unchecked")
35     public static <T extends Future> void waitFutureSuccess(final T future) {
36         final CountDownLatch latch = new CountDownLatch(1);
37         future.addListener(future1 -> latch.countDown());
38         Uninterruptibles.awaitUninterruptibly(latch, LATCH_TIMEOUT, TimeUnit.SECONDS);
39         Verify.verify(future.isSuccess());
40     }
41
42     public static <R, T extends DataObject> R readData(final DataBroker dataBroker, final InstanceIdentifier<T> iid,
43         final Function<T, R> function) throws ReadFailedException {
44         AssertionError lastError = null;
45         final Stopwatch sw = Stopwatch.createStarted();
46         while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
47             try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
48                 final Optional<T> data = tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet();
49                 if (data.isPresent()) {
50                     try {
51                         return function.apply(data.get());
52                     } catch (final AssertionError e) {
53                         lastError = e;
54                         Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
55                     }
56                 }
57             }
58         }
59         throw lastError;
60     }
61
62     public static <T extends DataObject> void checkNull(final DataBroker dataBroker, final InstanceIdentifier<T> iid)
63         throws ReadFailedException {
64         AssertionError lastError = null;
65         final Stopwatch sw = Stopwatch.createStarted();
66         while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
67             try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
68                 final com.google.common.base.Optional<T> data = tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet();
69                 try {
70                     assert !data.isPresent();
71                     return;
72                 } catch (final AssertionError e) {
73                     lastError = e;
74                     Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
75                 }
76             }
77         }
78         throw lastError;
79     }
80
81     public static void checkEquals(final CheckEquals function) throws Exception {
82         AssertionError lastError = null;
83         final Stopwatch sw = Stopwatch.createStarted();
84         while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
85             try {
86                 function.check();
87                 return;
88             } catch (final AssertionError e) {
89                 lastError = e;
90                 Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
91             }
92         }
93         throw lastError;
94     }
95
96     public static void checkReceivedMessages(final ListenerCheck listener, final int numberOfMessages)
97         throws ReadFailedException {
98         final Stopwatch sw = Stopwatch.createStarted();
99         while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
100             if (listener.getListMessageSize() != numberOfMessages) {
101                 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
102             } else {
103                 return;
104             }
105         }
106         throw new AssertionError("Expected " + numberOfMessages + " but received "
107             + listener.getListMessageSize());
108     }
109
110     public interface ListenerCheck {
111         int getListMessageSize();
112     }
113     @FunctionalInterface
114     public interface CheckEquals {
115         void check() throws ExecutionException, InterruptedException;
116     }
117 }