mirror of
https://github.com/opennetworkinglab/onos.git
synced 2025-10-16 01:41:26 +02:00
Added the option to execute on the local thread.
Change-Id: I168eafb2eb57387a7ce5861ff6b8f2b89afd81dd
This commit is contained in:
parent
584107a683
commit
f27dba65f5
@ -31,7 +31,7 @@ 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;
|
||||
|
@ -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,15 +173,17 @@ 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);
|
||||
if (!runLocally) {
|
||||
delay(realTimeDelay);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the task and submits it to the queue.
|
||||
@ -238,7 +248,11 @@ public class ManuallyAdvancingTimer extends java.util.Timer {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
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);
|
||||
if (runLocally) {
|
||||
task.run();
|
||||
} else {
|
||||
executorService.execute(task);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ public class ManuallyAdvancingTimerTest {
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
timer = new ManuallyAdvancingTimer();
|
||||
timer = new ManuallyAdvancingTimer(true);
|
||||
idGenerator = new AtomicInteger(1);
|
||||
tasksRunCount = new AtomicInteger(0);
|
||||
taskList = Lists.newArrayList();
|
||||
|
Loading…
x
Reference in New Issue
Block a user