*/
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;
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 AbstractConcurrentDataBrokerTest {
protected static final class TestListener implements DataChangeListener {
private final SettableFuture<AsyncDataChangeEvent<InstanceIdentifier<?>, 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<InstanceIdentifier<?>, DataObject> arg) {
if (capture) {
event.set(arg);
+ } else {
+ initialEventLatch.countDown();
}
}
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;
}
}