X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fbinding%2Ftest%2FAbstractDataChangeListenerTest.java;h=2a5c0e8332819a567329bc703d765c365d9dd08a;hp=7742f372cf93cac20c1329780964d7680bb798b8;hb=47f9449f977820bc605bafa23e81ee2efe6d9f37;hpb=c46e223995956f1f759c551163c212947c1e2fb7 diff --git a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/AbstractDataChangeListenerTest.java b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/AbstractDataChangeListenerTest.java index 7742f372cf..2a5c0e8332 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/AbstractDataChangeListenerTest.java +++ b/opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/md/sal/binding/test/AbstractDataChangeListenerTest.java @@ -7,10 +7,13 @@ */ package org.opendaylight.controller.md.sal.binding.test; +import static org.junit.Assert.assertTrue; + +import com.google.common.util.concurrent.SettableFuture; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; - import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; @@ -18,23 +21,25 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import com.google.common.util.concurrent.SettableFuture; - -public abstract class AbstractDataChangeListenerTest extends AbstractDataBrokerTest { +public abstract class AbstractDataChangeListenerTest extends AbstractConcurrentDataBrokerTest { protected static final class TestListener implements DataChangeListener { private final SettableFuture, DataObject>> event; - private boolean capture = false; + private final CountDownLatch initialEventLatch; + private volatile boolean capture = false; - private TestListener() { + private TestListener(boolean expectInitialEvent) { event = SettableFuture.create(); + initialEventLatch = new CountDownLatch(expectInitialEvent ? 1 : 0); } @Override public void onDataChanged(final AsyncDataChangeEvent, DataObject> arg) { if (capture) { event.set(arg); + } else { + initialEventLatch.countDown(); } } @@ -50,16 +55,31 @@ public abstract class AbstractDataChangeListenerTest extends AbstractDataBrokerT return event.isDone(); } - public void startCapture() { + private void waitForInitialEvent() { + try { + assertTrue("Initial DataChangeEvent was not received", initialEventLatch.await(3, TimeUnit.SECONDS)); + } catch (InterruptedException e) { + throw new IllegalStateException(e); + } + this.capture = true; } } + protected AbstractDataChangeListenerTest() { + super(true); + } + protected final TestListener createListener(final LogicalDatastoreType store, final InstanceIdentifier path, final DataChangeScope scope) { - TestListener listener = new TestListener(); + return createListener(store, path, scope, true); + } + + protected final TestListener createListener(final LogicalDatastoreType store, final InstanceIdentifier path, + final DataChangeScope scope, boolean expectInitialEvent) { + TestListener listener = new TestListener(expectInitialEvent); getDataBroker().registerDataChangeListener(store, path, listener, scope); - listener.startCapture(); + listener.waitForInitialEvent(); return listener; } }