BUG-7673: Improve synchonization under BGP/PCEP Session
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / 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.bgp.rib.impl;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Stopwatch;
12 import com.google.common.util.concurrent.Uninterruptibles;
13 import io.netty.util.concurrent.Future;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.TimeUnit;
16 import java.util.function.Function;
17 import org.junit.Assert;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24
25 public final class CheckUtil {
26     private static final int TIMEOUT = 40;
27     private static final int LATCH_TIMEOUT = 10;
28     private static final int SLEEP_FOR = 20;
29     private static final int SLEEP_UNINTERRUPTIBLY = 50;
30     public static void checkReceivedMessages(final SimpleSessionListener listener, final int numberOfMessages)
31         throws ReadFailedException {
32         Stopwatch sw = Stopwatch.createStarted();
33         while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
34             if (listener.getListMsg().size() != numberOfMessages) {
35                 Uninterruptibles.sleepUninterruptibly(SLEEP_UNINTERRUPTIBLY, TimeUnit.MILLISECONDS);
36             } else {
37                 return;
38             }
39         }
40         Assert.fail();
41     }
42
43     public static <R, T extends DataObject> R readData(final DataBroker dataBroker, final InstanceIdentifier<T> iid, final Function<T, R> function)
44         throws ReadFailedException {
45         AssertionError lastError = null;
46         final Stopwatch sw = Stopwatch.createStarted();
47         while (sw.elapsed(TimeUnit.SECONDS) <= TIMEOUT) {
48             try (final ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
49                 final Optional<T> data = tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet();
50                 if (data.isPresent()) {
51                     try {
52                         return function.apply(data.get());
53                     } catch (final AssertionError e) {
54                         lastError = e;
55                         Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
56                     }
57                 }
58             }
59         }
60         Assert.fail(lastError.getMessage());
61         throw lastError;
62     }
63
64     @SuppressWarnings("unchecked")
65     public static <T extends Future> void waitFutureSuccess(final T future) {
66         final CountDownLatch latch = new CountDownLatch(1);
67         future.addListener(future1 -> latch.countDown());
68         Uninterruptibles.awaitUninterruptibly(latch, LATCH_TIMEOUT, TimeUnit.SECONDS);
69     }
70 }