Committed by
Gerrit Code Review
Added the option to execute on the local thread.
Change-Id: I168eafb2eb57387a7ce5861ff6b8f2b89afd81dd
Showing
3 changed files
with
21 additions
and
3 deletions
... | @@ -31,7 +31,7 @@ import static org.onlab.junit.TestTools.assertAfter; | ... | @@ -31,7 +31,7 @@ import static org.onlab.junit.TestTools.assertAfter; |
31 | public class AbstractAccumulatorTest { | 31 | public class AbstractAccumulatorTest { |
32 | 32 | ||
33 | 33 | ||
34 | - private final ManuallyAdvancingTimer timer = new ManuallyAdvancingTimer(); | 34 | + private final ManuallyAdvancingTimer timer = new ManuallyAdvancingTimer(true); |
35 | 35 | ||
36 | private static final int LONG_REAL_TIME_DELAY = 30; | 36 | private static final int LONG_REAL_TIME_DELAY = 30; |
37 | private static final int SHORT_REAL_TIME_DELAY = 5; | 37 | private static final int SHORT_REAL_TIME_DELAY = 5; | ... | ... |
... | @@ -65,6 +65,14 @@ public class ManuallyAdvancingTimer extends java.util.Timer { | ... | @@ -65,6 +65,14 @@ public class ManuallyAdvancingTimer extends java.util.Timer { |
65 | /* Data structure for tracking tasks */ | 65 | /* Data structure for tracking tasks */ |
66 | private final TaskQueue queue = new TaskQueue(); | 66 | private final TaskQueue queue = new TaskQueue(); |
67 | 67 | ||
68 | + /* Whether execution should execute on the executor thread or the calling thread. */ | ||
69 | + private final boolean runLocally; | ||
70 | + | ||
71 | + public ManuallyAdvancingTimer(boolean runLocally) { | ||
72 | + this.runLocally = runLocally; | ||
73 | + } | ||
74 | + | ||
75 | + | ||
68 | @Override | 76 | @Override |
69 | public void schedule(TimerTask task, long delay) { | 77 | public void schedule(TimerTask task, long delay) { |
70 | if (!staticsPopulated) { | 78 | if (!staticsPopulated) { |
... | @@ -165,15 +173,17 @@ public class ManuallyAdvancingTimer extends java.util.Timer { | ... | @@ -165,15 +173,17 @@ public class ManuallyAdvancingTimer extends java.util.Timer { |
165 | 173 | ||
166 | /** | 174 | /** |
167 | * Advances the virtual time a certain number of millis triggers execution delays a certain amount to | 175 | * Advances the virtual time a certain number of millis triggers execution delays a certain amount to |
168 | - * allow time for execution. | 176 | + * allow time for execution. If runLocally is true then all real time delays are ignored. |
169 | * | 177 | * |
170 | * @param virtualTimeAdvance the time to be advances in millis of simulated time. | 178 | * @param virtualTimeAdvance the time to be advances in millis of simulated time. |
171 | * @param realTimeDelay the time to delay in real time to allow for processing. | 179 | * @param realTimeDelay the time to delay in real time to allow for processing. |
172 | */ | 180 | */ |
173 | public void advanceTimeMillis(long virtualTimeAdvance, int realTimeDelay) { | 181 | public void advanceTimeMillis(long virtualTimeAdvance, int realTimeDelay) { |
174 | timerKeeper.advanceTimeMillis(virtualTimeAdvance); | 182 | timerKeeper.advanceTimeMillis(virtualTimeAdvance); |
183 | + if (!runLocally) { | ||
175 | delay(realTimeDelay); | 184 | delay(realTimeDelay); |
176 | } | 185 | } |
186 | + } | ||
177 | 187 | ||
178 | /** | 188 | /** |
179 | * Sets up the task and submits it to the queue. | 189 | * Sets up the task and submits it to the queue. |
... | @@ -238,7 +248,11 @@ public class ManuallyAdvancingTimer extends java.util.Timer { | ... | @@ -238,7 +248,11 @@ public class ManuallyAdvancingTimer extends java.util.Timer { |
238 | e.printStackTrace(); | 248 | e.printStackTrace(); |
239 | return false; | 249 | return false; |
240 | } | 250 | } |
251 | + if (runLocally) { | ||
252 | + task.run(); | ||
253 | + } else { | ||
241 | executorService.execute(task); | 254 | executorService.execute(task); |
255 | + } | ||
242 | return true; | 256 | return true; |
243 | } else { | 257 | } else { |
244 | //Calculate next execution time, using absolute value of period | 258 | //Calculate next execution time, using absolute value of period |
... | @@ -253,7 +267,11 @@ public class ManuallyAdvancingTimer extends java.util.Timer { | ... | @@ -253,7 +267,11 @@ public class ManuallyAdvancingTimer extends java.util.Timer { |
253 | } | 267 | } |
254 | //Schedule next execution | 268 | //Schedule next execution |
255 | queue.insertOrdered(task); | 269 | queue.insertOrdered(task); |
270 | + if (runLocally) { | ||
271 | + task.run(); | ||
272 | + } else { | ||
256 | executorService.execute(task); | 273 | executorService.execute(task); |
274 | + } | ||
257 | return true; | 275 | return true; |
258 | } | 276 | } |
259 | } | 277 | } | ... | ... |
... | @@ -54,7 +54,7 @@ public class ManuallyAdvancingTimerTest { | ... | @@ -54,7 +54,7 @@ public class ManuallyAdvancingTimerTest { |
54 | */ | 54 | */ |
55 | @Before | 55 | @Before |
56 | public void setup() { | 56 | public void setup() { |
57 | - timer = new ManuallyAdvancingTimer(); | 57 | + timer = new ManuallyAdvancingTimer(true); |
58 | idGenerator = new AtomicInteger(1); | 58 | idGenerator = new AtomicInteger(1); |
59 | tasksRunCount = new AtomicInteger(0); | 59 | tasksRunCount = new AtomicInteger(0); |
60 | taskList = Lists.newArrayList(); | 60 | taskList = Lists.newArrayList(); | ... | ... |
-
Please register or login to post a comment