X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-inmemory-datastore%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fstore%2Fimpl%2FDatastoreTestTask.java;h=ed17aa55cfb852fa5724272f87f8d298f1f694cb;hp=26987a6fba6426169ee7df98340c8ac0f0f4be47;hb=258d8039ac144aeee2efa7943228c0fc6cdaf651;hpb=6d73d16b194435ea1ea783a37d1b51fc1f558a1f diff --git a/opendaylight/md-sal/sal-inmemory-datastore/src/test/java/org/opendaylight/controller/md/sal/dom/store/impl/DatastoreTestTask.java b/opendaylight/md-sal/sal-inmemory-datastore/src/test/java/org/opendaylight/controller/md/sal/dom/store/impl/DatastoreTestTask.java index 26987a6fba..ed17aa55cf 100644 --- a/opendaylight/md-sal/sal-inmemory-datastore/src/test/java/org/opendaylight/controller/md/sal/dom/store/impl/DatastoreTestTask.java +++ b/opendaylight/md-sal/sal-inmemory-datastore/src/test/java/org/opendaylight/controller/md/sal/dom/store/impl/DatastoreTestTask.java @@ -8,10 +8,13 @@ package org.opendaylight.controller.md.sal.dom.store.impl; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.SettableFuture; import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; - +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener; @@ -23,9 +26,6 @@ import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.SettableFuture; - public class DatastoreTestTask { private final DOMStore store; @@ -37,17 +37,19 @@ public class DatastoreTestTask { private WriteTransactionCustomizer cleanup; private YangInstanceIdentifier changePath; private DataChangeScope changeScope; - private boolean postSetup = false; + private volatile boolean postSetup = false; private final ChangeEventListener internalListener; + private final TestDCLExecutorService dclExecutorService; - public DatastoreTestTask(final DOMStore datastore) { + public DatastoreTestTask(final DOMStore datastore, final TestDCLExecutorService dclExecutorService) { this.store = datastore; + this.dclExecutorService = dclExecutorService; internalListener = new ChangeEventListener(); } public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope, - final AsyncDataChangeListener> changeListener) { - this.changeListener = changeListener; + final AsyncDataChangeListener> listener) { + this.changeListener = listener; this.changePath = path; this.changeScope = scope; return this; @@ -59,27 +61,27 @@ public class DatastoreTestTask { return this; } - public DatastoreTestTask setup(final WriteTransactionCustomizer setup) { - this.setup = setup; + public DatastoreTestTask setup(final WriteTransactionCustomizer customizer) { + this.setup = customizer; return this; } - public DatastoreTestTask test(final WriteTransactionCustomizer write) { - this.write = write; + public DatastoreTestTask test(final WriteTransactionCustomizer customizer) { + this.write = customizer; return this; } - public DatastoreTestTask read(final ReadTransactionVerifier read) { - this.read = read; + public DatastoreTestTask read(final ReadTransactionVerifier customizer) { + this.read = customizer; return this; } - public DatastoreTestTask cleanup(final WriteTransactionCustomizer cleanup) { - this.cleanup = cleanup; + public DatastoreTestTask cleanup(final WriteTransactionCustomizer customizer) { + this.cleanup = customizer; return this; } - public void run() throws InterruptedException, ExecutionException { + public void run() throws Exception { if (setup != null) { execute(setup); } @@ -89,13 +91,17 @@ public class DatastoreTestTask { } Preconditions.checkState(write != null, "Write Transaction must be set."); + postSetup = true; + dclExecutorService.afterTestSetup(); + execute(write); if (registration != null) { registration.close(); } + if (changeListener != null) { - changeListener.onDataChanged(internalListener.receivedChange.get()); + changeListener.onDataChanged(getChangeEvent()); } if (read != null) { read.verify(store.newReadOnlyTransaction()); @@ -105,8 +111,17 @@ public class DatastoreTestTask { } } - public Future>> getChangeEvent() { - return internalListener.receivedChange; + public AsyncDataChangeEvent> getChangeEvent() throws Exception { + return internalListener.receivedChange.get(10, TimeUnit.SECONDS); + } + + public void verifyNoChangeEvent() throws Exception { + try { + Object unexpected = internalListener.receivedChange.get(500, TimeUnit.MILLISECONDS); + fail("Got unexpected AsyncDataChangeEvent from the Future: " + unexpected); + } catch (TimeoutException e) { + // Expected + } } private void execute(final WriteTransactionCustomizer writeCustomizer) throws InterruptedException, @@ -120,18 +135,18 @@ public class DatastoreTestTask { } public interface WriteTransactionCustomizer { - public void customize(DOMStoreReadWriteTransaction tx); + void customize(DOMStoreReadWriteTransaction tx); } public interface ReadTransactionVerifier { - public void verify(DOMStoreReadTransaction tx); + void verify(DOMStoreReadTransaction tx); } private final class ChangeEventListener implements AsyncDataChangeListener> { - protected final SettableFuture>> receivedChange = SettableFuture - .create(); + protected final SettableFuture>> + receivedChange = SettableFuture.create(); @Override public void onDataChanged(final AsyncDataChangeEvent> change) { @@ -143,32 +158,15 @@ public class DatastoreTestTask { public static final WriteTransactionCustomizer simpleWrite(final YangInstanceIdentifier path, final NormalizedNode data) { - return new WriteTransactionCustomizer() { - - @Override - public void customize(final DOMStoreReadWriteTransaction tx) { - tx.write(path, data); - } - }; + return tx -> tx.write(path, data); } public static final WriteTransactionCustomizer simpleMerge(final YangInstanceIdentifier path, final NormalizedNode data) { - return new WriteTransactionCustomizer() { - - @Override - public void customize(final DOMStoreReadWriteTransaction tx) { - tx.merge(path, data); - } - }; + return tx -> tx.merge(path, data); } public static final WriteTransactionCustomizer simpleDelete(final YangInstanceIdentifier path) { - return new WriteTransactionCustomizer() { - @Override - public void customize(final DOMStoreReadWriteTransaction tx) { - tx.delete(path); - } - }; + return tx -> tx.delete(path); } }