Address unit test issue
This is an amendment to gerrit 20469.
Unit test fails if test cases are executed in parallel.
The same issue has been addressed in branch 1.12 to 1.15 (gerrit 20700-20703)
Change-Id: I43f7bd2f2c0836cf478af3ed9f9c0e931e02d4c8
diff --git a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/InOrderFlowObjectiveManager.java b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/InOrderFlowObjectiveManager.java
index 3f4de99..457c6e6 100644
--- a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/InOrderFlowObjectiveManager.java
+++ b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/InOrderFlowObjectiveManager.java
@@ -63,7 +63,7 @@
private final Logger log = LoggerFactory.getLogger(getClass());
// TODO Make queue timeout configurable
- static final int OBJ_TIMEOUT_MS = 15000;
+ static int objTimeoutMs = 15000;
private Cache<FilteringObjQueueKey, Objective> filtObjQueueHead;
private Cache<ForwardingObjQueueKey, Objective> fwdObjQueueHead;
@@ -105,15 +105,15 @@
}
};
filtObjQueueHead = CacheBuilder.newBuilder()
- .expireAfterWrite(OBJ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
+ .expireAfterWrite(objTimeoutMs, TimeUnit.MILLISECONDS)
.removalListener(RemovalListeners.asynchronous(removalListener, filtCacheEventExecutor))
.build();
fwdObjQueueHead = CacheBuilder.newBuilder()
- .expireAfterWrite(OBJ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
+ .expireAfterWrite(objTimeoutMs, TimeUnit.MILLISECONDS)
.removalListener(RemovalListeners.asynchronous(removalListener, fwdCacheEventExecutor))
.build();
nextObjQueueHead = CacheBuilder.newBuilder()
- .expireAfterWrite(OBJ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
+ .expireAfterWrite(objTimeoutMs, TimeUnit.MILLISECONDS)
.removalListener(RemovalListeners.asynchronous(removalListener, nextCacheEventExecutor))
.build();
@@ -122,7 +122,7 @@
filtObjQueueHead.cleanUp();
fwdObjQueueHead.cleanUp();
nextObjQueueHead.cleanUp();
- }, 0, OBJ_TIMEOUT_MS, TimeUnit.MILLISECONDS);
+ }, 0, objTimeoutMs, TimeUnit.MILLISECONDS);
// Replace store delegate to make sure pendingForward and pendingNext are resubmitted to
// execute()
diff --git a/core/net/src/test/java/org/onosproject/net/flowobjective/impl/InOrderFlowObjectiveManagerTest.java b/core/net/src/test/java/org/onosproject/net/flowobjective/impl/InOrderFlowObjectiveManagerTest.java
index 9797442..f7ba5bc 100644
--- a/core/net/src/test/java/org/onosproject/net/flowobjective/impl/InOrderFlowObjectiveManagerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/flowobjective/impl/InOrderFlowObjectiveManagerTest.java
@@ -66,7 +66,9 @@
import java.util.Collection;
import java.util.List;
+import java.util.Objects;
import java.util.Random;
+import java.util.concurrent.atomic.AtomicInteger;
public class InOrderFlowObjectiveManagerTest {
private InOrderFlowObjectiveManager mgr;
@@ -96,6 +98,7 @@
// Delay flow objectives OFFSET + rand(0, BOUND) millis
private static final int DEFAULT_OFFSET = 10; // ms
private static final int DEFAULT_BOUND = 40; // ms
+ private static final int TIMEOUT_THRESH = 100; // ms
private static int offset = DEFAULT_OFFSET;
private static int bound = DEFAULT_BOUND;
@@ -131,16 +134,6 @@
private List<ForwardingObjective> expectFwdObjs = Lists.newCopyOnWriteArrayList(
Lists.newArrayList(FWD1, FWD2, FWD3, FWD4, FWD5, FWD6));
- private static boolean timeout = false;
- private static final ForwardingObjective FWD11 = buildFwdObjective(S1, NID2).add(new ObjectiveContext() {
- @Override
- public void onError(Objective objective, ObjectiveError error) {
- timeout = error.equals(ObjectiveError.INSTALLATIONTIMEOUT);
- }
- });
- private List<ForwardingObjective> expectFwdObjsTimeout = Lists.newCopyOnWriteArrayList(
- Lists.newArrayList(FWD11, FWD1, FWD2));
-
private List<Objective> actualObjs = Lists.newCopyOnWriteArrayList();
private Pipeliner pipeliner = new PipelinerAdapter() {
@@ -203,7 +196,6 @@
mgr.activate();
reset(mgr.flowObjectiveStore);
- timeout = false;
offset = DEFAULT_OFFSET;
bound = DEFAULT_BOUND;
actualObjs.clear();
@@ -246,12 +238,28 @@
@Test
public void forwardTimeout() {
+ final AtomicInteger counter = new AtomicInteger(0);
+ ForwardingObjective fwdTimeout = buildFwdObjective(S1, NID2).add(new ObjectiveContext() {
+ @Override
+ public void onError(Objective objective, ObjectiveError error) {
+ if (Objects.equals(ObjectiveError.INSTALLATIONTIMEOUT, error)) {
+ counter.incrementAndGet();
+ }
+ }
+ });
+ List<ForwardingObjective> expectFwdObjsTimeout = Lists.newCopyOnWriteArrayList(
+ Lists.newArrayList(fwdTimeout, FWD1, FWD2));
+
+ // Reduce timeout so the unit test doesn't have to wait many seconds
+ InOrderFlowObjectiveManager.objTimeoutMs = TIMEOUT_THRESH;
+ setUp();
+
expect(mgr.flowObjectiveStore.getNextGroup(NID1)).andReturn(NGRP1).times(2);
expect(mgr.flowObjectiveStore.getNextGroup(NID2)).andReturn(NGRP2).times(2);
replay(mgr.flowObjectiveStore);
// Force this objective to time out
- offset = InOrderFlowObjectiveManager.OBJ_TIMEOUT_MS + 500;
+ offset = InOrderFlowObjectiveManager.objTimeoutMs * 2;
expectFwdObjsTimeout.forEach(fwdObj -> mgr.forward(DEV1, fwdObj));
@@ -259,8 +267,8 @@
int expectedTime = (bound + offset) * 3;
assertAfter(expectedTime, expectedTime * 5, () -> assertEquals(expectFwdObjsTimeout.size(), actualObjs.size()));
- assertTrue(timeout);
- assertTrue(actualObjs.indexOf(FWD11) < actualObjs.indexOf(FWD1));
+ assertTrue(counter.get() != 0);
+ assertTrue(actualObjs.indexOf(fwdTimeout) < actualObjs.indexOf(FWD1));
verify(mgr.flowObjectiveStore);
}