TestDataStoreJobCoordinator with waitForAllJobs() methods req. in tests 61/48061/4
authorMichael Vorburger <vorburger@redhat.com>
Mon, 7 Nov 2016 15:56:34 +0000 (16:56 +0100)
committerMichael Vorburger <vorburger@redhat.com>
Thu, 10 Nov 2016 15:24:41 +0000 (16:24 +0100)
Change-Id: Id0b4087b3417bbe8098eac0322164fd5752292ab
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
mdsalutil/mdsalutil-api/pom.xml
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/DataStoreJobCoordinator.java
mdsalutil/mdsalutil-api/src/test/java/org/opendaylight/genius/datastoreutils/TestDataStoreJobCoordinator.java [new file with mode: 0644]
mdsalutil/mdsalutil-api/src/test/java/org/opendaylight/genius/datastoreutils/tests/DataStoreJobCoordinatorTest.java [new file with mode: 0644]

index 7d1538b6bea26854453cb73fd62adfbc82e3c2da..6fd2d96c8dab8e2093b53a353086d9b31118718c 100644 (file)
@@ -71,6 +71,11 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
     <artifactId>testutils</artifactId>
     <scope>test</scope>
   </dependency>
+  <dependency>
+    <groupId>org.awaitility</groupId>
+    <artifactId>awaitility</artifactId>
+    <scope>test</scope>
+  </dependency>
 </dependencies>
  <build>
     <plugins>
index 6eb0a75a5373e75a157f9e5f8e84f586c540d2a9..7221b2b599a21f8a9c7dc703d8c3dddb8b81e1ae 100755 (executable)
@@ -32,9 +32,11 @@ public class DataStoreJobCoordinator {
     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();
 
@@ -325,4 +327,5 @@ public class DataStoreJobCoordinator {
 
         return true;
     }
+
 }
diff --git a/mdsalutil/mdsalutil-api/src/test/java/org/opendaylight/genius/datastoreutils/TestDataStoreJobCoordinator.java b/mdsalutil/mdsalutil-api/src/test/java/org/opendaylight/genius/datastoreutils/TestDataStoreJobCoordinator.java
new file mode 100644 (file)
index 0000000..c8397a7
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * 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));
+    }
+}
diff --git a/mdsalutil/mdsalutil-api/src/test/java/org/opendaylight/genius/datastoreutils/tests/DataStoreJobCoordinatorTest.java b/mdsalutil/mdsalutil-api/src/test/java/org/opendaylight/genius/datastoreutils/tests/DataStoreJobCoordinatorTest.java
new file mode 100644 (file)
index 0000000..c268889
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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);
+    }
+
+}