-
-
Save danielsilva/1447560 to your computer and use it in GitHub Desktop.
Support Timer class for unit tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
import java.util.PriorityQueue; | |
import java.util.Queue; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import static junit.framework.Assert.*; | |
public class SupportTimer extends Timer { | |
private static class SupportTask implements Runnable, Comparable<SupportTask> { | |
private final TimerTask task; | |
private final Long next; | |
private final Long period; | |
public SupportTask(TimerTask task, Long next) { | |
this(task, next, null); | |
} | |
public SupportTask(TimerTask task, Long next, Long period) { | |
this.task = task; | |
this.next = next; | |
this.period = period; | |
} | |
@Override | |
public int compareTo(SupportTask that) { | |
return this.next.compareTo(that.next); | |
} | |
public SupportTask getNext() { | |
if (period != null) | |
return new SupportTask(task, next + period, period)); | |
else | |
return new SupportTask(defaultValue(TimerTask), defaultValue(Long), defaultValue(Long))); | |
} | |
public boolean isCancelled() { | |
//damn hack | |
try { | |
Field field = TimerTask.class.getDeclaredField("state"); | |
field.setAccessible(true); | |
return field.get(task).equals(3); | |
} catch (Exception e) { | |
fail("Great bug: " + e); | |
return true; | |
} | |
} | |
@Override | |
public void run() { | |
task.run(); | |
} | |
public void assertSchedule(Long delay) { | |
assertSchedule(delay, null); | |
} | |
public void assertSchedule(Long delay, Long period) { | |
assertEquals(delay, this.next); | |
assertEquals(period, this.period); | |
assertFalse(isCancelled()); | |
} | |
} | |
private long time = 0; | |
private Queue<SupportTask> tasks = new PriorityQueue<SupportTask>(); | |
@Override | |
public void schedule(TimerTask task, long delay) { | |
tasks.add(new SupportTask(task, delay)); | |
} | |
@Override | |
public void schedule(TimerTask task, long delay, long period) { | |
tasks.add(new SupportTask(task, delay, period)); | |
} | |
public long getTime() { | |
return time; | |
} | |
private void cleanUp() { | |
while (!tasks.isEmpty() && tasks.peek().isCancelled()) | |
tasks.poll(); | |
} | |
private SupportTask prepareToRun() { | |
assertMoreTasks(); | |
SupportTask task = tasks.poll(); | |
time = task.next; | |
tasks.Add(task.getNext()); | |
return task; | |
} | |
public void runNext() { | |
prepareToRun().run(); | |
} | |
public void runNextAt(long expectedTime) { | |
SupportTask task = prepareToRun(); | |
assertEquals(expectedTime, this.time); | |
task.run(); | |
} | |
public void assertSchedule(Long delay) { | |
assertMoreTasks(); | |
tasks.peek().assertSchedule(delay); | |
} | |
public void assertSchedule(Long delay, Long period) { | |
assertMoreTasks(); | |
tasks.peek().assertSchedule(delay, period); | |
} | |
public void assertMoreTasks() { | |
cleanUp(); | |
assertFalse("Should have more tasks scheduled", tasks.isEmpty()); | |
} | |
public void assertNoMoreTasks() { | |
cleanUp(); | |
assertTrue("Should have no more tasks scheduled", tasks.isEmpty()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment