<artifactId>testutils</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.awaitility</groupId>
+ <artifactId>awaitility</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
private static final int THREADPOOL_SIZE = Runtime.getRuntime().availableProcessors();
private static final long RETRY_WAIT_BASE_TIME = 100;
+ // package local instead of private for TestDataStoreJobCoordinator
+ final ForkJoinPool fjPool;
+ final Map<Integer, Map<String, JobQueue>> jobQueueMap = new ConcurrentHashMap<>();
+
private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
- private final ForkJoinPool fjPool;
- private final Map<Integer, Map<String, JobQueue>> jobQueueMap = new ConcurrentHashMap<>();
private final ReentrantLock reentrantLock = new ReentrantLock();
private final Condition waitCondition = reentrantLock.newCondition();
return true;
}
+
}
--- /dev/null
+/*
+ * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.genius.datastoreutils;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.concurrent.TimeUnit;
+import org.awaitility.Awaitility;
+
+/**
+ * DataStoreJobCoordinator companion class with wait method.
+ *
+ * @author Michael Vorburger
+ */
+public class TestDataStoreJobCoordinator {
+
+ /**
+ * Waits, by blocking calling thread, for all previously enqueued jobs to be processed.
+ *
+ * <p>THIS METHOD IS ONLY INTENDED FOR TESTS, AND SHOULD NOT BE CALLED IN PRODUCTION CODE.
+ */
+ public void waitForAllJobs(Duration duration) {
+ DataStoreJobCoordinator dataStoreJobCoordinator = DataStoreJobCoordinator.getInstance();
+
+ dataStoreJobCoordinator.jobQueueMap.values().forEach(map -> map.values().forEach(jobQueue ->
+ Awaitility.await(TestDataStoreJobCoordinator.class.getName())
+ .atMost(duration.get(ChronoUnit.SECONDS), TimeUnit.SECONDS).until(() ->
+ jobQueue.getWaitingEntries().isEmpty())));
+
+ dataStoreJobCoordinator.fjPool.awaitQuiescence(duration.get(ChronoUnit.SECONDS), TimeUnit.SECONDS);
+ }
+
+ public void waitForAllJobs() {
+ waitForAllJobs(Duration.ofSeconds(5));
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.genius.datastoreutils.tests;
+
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import java.util.List;
+import java.util.concurrent.Callable;
+import org.junit.Test;
+import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
+import org.opendaylight.genius.datastoreutils.TestDataStoreJobCoordinator;
+
+/**
+ * Unit test for DataStoreJobCoordinator.
+ *
+ * @author Michael Vorburger
+ */
+public class DataStoreJobCoordinatorTest {
+
+ private static class TestCallable implements Callable<List<ListenableFuture<Void>>> {
+
+ boolean wasCalled = false;
+
+ @Override
+ public List<ListenableFuture<Void>> call() throws Exception {
+ wasCalled = true;
+ return null;
+ }
+ }
+
+ @Test
+ public void testWait() {
+ DataStoreJobCoordinator dataStoreJobCoordinator = DataStoreJobCoordinator.getInstance();
+ TestCallable testCallable = new TestCallable();
+ dataStoreJobCoordinator.enqueueJob(getClass().getName().toString(), testCallable);
+ new TestDataStoreJobCoordinator().waitForAllJobs();
+ assertTrue(testCallable.wasCalled);
+ }
+
+}