aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/utils/misc/src/test/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/onos/utils/misc/src/test/java/org')
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/AbstractAccumulatorTest.java27
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimer.java26
-rw-r--r--framework/src/onos/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimerTest.java4
3 files changed, 39 insertions, 18 deletions
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/AbstractAccumulatorTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/AbstractAccumulatorTest.java
index db7224ad..8a409c3d 100644
--- a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/AbstractAccumulatorTest.java
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/AbstractAccumulatorTest.java
@@ -31,7 +31,10 @@ import static org.onlab.junit.TestTools.assertAfter;
public class AbstractAccumulatorTest {
- private final ManuallyAdvancingTimer timer = new ManuallyAdvancingTimer();
+ private final ManuallyAdvancingTimer timer = new ManuallyAdvancingTimer(true);
+
+ private static final int LONG_REAL_TIME_DELAY = 30;
+ private static final int SHORT_REAL_TIME_DELAY = 5;
@Test
@@ -52,7 +55,7 @@ public class AbstractAccumulatorTest {
accumulator.add(new TestItem("d"));
assertTrue("should not have fired yet", accumulator.batch.isEmpty());
accumulator.add(new TestItem("e"));
- timer.advanceTimeMillis(20, 10);
+ timer.advanceTimeMillis(20, LONG_REAL_TIME_DELAY);
assertFalse("should have fired", accumulator.batch.isEmpty());
assertEquals("incorrect batch", "abcde", accumulator.batch);
}
@@ -61,16 +64,16 @@ public class AbstractAccumulatorTest {
public void timeTrigger() {
TestAccumulator accumulator = new TestAccumulator();
accumulator.add(new TestItem("a"));
- timer.advanceTimeMillis(30, 1);
+ timer.advanceTimeMillis(30, SHORT_REAL_TIME_DELAY);
assertTrue("should not have fired yet", accumulator.batch.isEmpty());
accumulator.add(new TestItem("b"));
- timer.advanceTimeMillis(30, 1);
+ timer.advanceTimeMillis(30, SHORT_REAL_TIME_DELAY);
assertTrue("should not have fired yet", accumulator.batch.isEmpty());
accumulator.add(new TestItem("c"));
- timer.advanceTimeMillis(30, 1);
+ timer.advanceTimeMillis(30, SHORT_REAL_TIME_DELAY);
assertTrue("should not have fired yet", accumulator.batch.isEmpty());
accumulator.add(new TestItem("d"));
- timer.advanceTimeMillis(10, 10);
+ timer.advanceTimeMillis(10, LONG_REAL_TIME_DELAY);
assertFalse("should have fired", accumulator.batch.isEmpty());
assertEquals("incorrect batch", "abcd", accumulator.batch);
}
@@ -81,7 +84,7 @@ public class AbstractAccumulatorTest {
accumulator.add(new TestItem("a"));
assertTrue("should not have fired yet", accumulator.batch.isEmpty());
accumulator.add(new TestItem("b"));
- timer.advanceTimeMillis(70, 10);
+ timer.advanceTimeMillis(70, LONG_REAL_TIME_DELAY);
assertFalse("should have fired", accumulator.batch.isEmpty());
assertEquals("incorrect batch", "ab", accumulator.batch);
}
@@ -93,10 +96,10 @@ public class AbstractAccumulatorTest {
accumulator.add(new TestItem("a"));
assertTrue("should not have fired yet", accumulator.batch.isEmpty());
accumulator.add(new TestItem("b"));
- timer.advanceTimeMillis(80, 1);
+ timer.advanceTimeMillis(80, SHORT_REAL_TIME_DELAY);
assertTrue("should not have fired yet", accumulator.batch.isEmpty());
accumulator.ready = true;
- timer.advanceTimeMillis(80, 10);
+ timer.advanceTimeMillis(80, LONG_REAL_TIME_DELAY);
assertFalse("should have fired", accumulator.batch.isEmpty());
assertEquals("incorrect batch", "ab", accumulator.batch);
}
@@ -105,12 +108,12 @@ public class AbstractAccumulatorTest {
public void readyLongTrigger() {
TestAccumulator accumulator = new TestAccumulator();
accumulator.ready = false;
- timer.advanceTimeMillis(120, 1);
+ timer.advanceTimeMillis(120, SHORT_REAL_TIME_DELAY);
assertTrue("should not have fired yet", accumulator.batch.isEmpty());
accumulator.add(new TestItem("a"));
assertTrue("should not have fired yet", accumulator.batch.isEmpty());
accumulator.ready = true;
- timer.advanceTimeMillis(120, 10);
+ timer.advanceTimeMillis(120, LONG_REAL_TIME_DELAY);
assertFalse("should have fired", accumulator.batch.isEmpty());
assertEquals("incorrect batch", "a", accumulator.batch);
}
@@ -128,7 +131,7 @@ public class AbstractAccumulatorTest {
assertTrue("should not have fired yet", accumulator.batch.isEmpty());
accumulator.ready = true;
accumulator.add(new TestItem("g"));
- timer.advanceTimeMillis(10, 10);
+ timer.advanceTimeMillis(10, LONG_REAL_TIME_DELAY);
assertFalse("should have fired", accumulator.batch.isEmpty());
assertEquals("incorrect batch", "abcdefg", accumulator.batch);
}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimer.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimer.java
index 4116cbef..8fb008e8 100644
--- a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimer.java
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimer.java
@@ -65,6 +65,14 @@ public class ManuallyAdvancingTimer extends java.util.Timer {
/* Data structure for tracking tasks */
private final TaskQueue queue = new TaskQueue();
+ /* Whether execution should execute on the executor thread or the calling thread. */
+ private final boolean runLocally;
+
+ public ManuallyAdvancingTimer(boolean runLocally) {
+ this.runLocally = runLocally;
+ }
+
+
@Override
public void schedule(TimerTask task, long delay) {
if (!staticsPopulated) {
@@ -165,14 +173,16 @@ public class ManuallyAdvancingTimer extends java.util.Timer {
/**
* Advances the virtual time a certain number of millis triggers execution delays a certain amount to
- * allow time for execution.
+ * allow time for execution. If runLocally is true then all real time delays are ignored.
*
* @param virtualTimeAdvance the time to be advances in millis of simulated time.
* @param realTimeDelay the time to delay in real time to allow for processing.
*/
public void advanceTimeMillis(long virtualTimeAdvance, int realTimeDelay) {
timerKeeper.advanceTimeMillis(virtualTimeAdvance);
- delay(realTimeDelay);
+ if (!runLocally) {
+ delay(realTimeDelay);
+ }
}
/**
@@ -238,7 +248,11 @@ public class ManuallyAdvancingTimer extends java.util.Timer {
e.printStackTrace();
return false;
}
- executorService.execute(task);
+ if (runLocally) {
+ task.run();
+ } else {
+ executorService.execute(task);
+ }
return true;
} else {
//Calculate next execution time, using absolute value of period
@@ -253,7 +267,11 @@ public class ManuallyAdvancingTimer extends java.util.Timer {
}
//Schedule next execution
queue.insertOrdered(task);
- executorService.execute(task);
+ if (runLocally) {
+ task.run();
+ } else {
+ executorService.execute(task);
+ }
return true;
}
}
diff --git a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimerTest.java b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimerTest.java
index b8e1e85e..36b50e67 100644
--- a/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimerTest.java
+++ b/framework/src/onos/utils/misc/src/test/java/org/onlab/util/ManuallyAdvancingTimerTest.java
@@ -47,14 +47,14 @@ public class ManuallyAdvancingTimerTest {
private AtomicInteger tasksRunCount;
// FIXME if this class fails first try increasing the real time delay to account for heavy system load.
- private static final int REAL_TIME_DELAY = 1;
+ private static final int REAL_TIME_DELAY = 10;
/**
* Sets up the testing environment.
*/
@Before
public void setup() {
- timer = new ManuallyAdvancingTimer();
+ timer = new ManuallyAdvancingTimer(true);
idGenerator = new AtomicInteger(1);
tasksRunCount = new AtomicInteger(0);
taskList = Lists.newArrayList();