TimerImpl implements ITimer { @Override public ITimerTask schedule(final ITask task, final long time) throws TaskScheduleException { return new TimerTaskImpl(task, timer, time); } TimerImpl(final Timer timer); @Override ITimerTask schedule(final ITask task, final long time);  }
@Test public void Should_resolveQueueSchedulingTask() throws Exception { ITimer timer = new TimerImpl(timerMock); long tm = System.currentTimeMillis() + 1337; timer.schedule(taskMock, tm); verifyStatic(); IOC.resolve(same(taskQueueKey)); ArgumentCaptor<TimerTask> timerTaskArgumentCaptor = ArgumentCaptor.forClass(TimerTask.class); ArgumentCaptor<Long> timeArgumentCaptor = ArgumentCaptor.forClass(Long.class); verify(timerMock).schedule(timerTaskArgumentCaptor.capture(), timeArgumentCaptor.capture()); assertTrue(1000L > Math.abs(1337 - timeArgumentCaptor.getValue())); timerTaskArgumentCaptor.getValue().run(); verify(taskQueueMock).put(same(taskMock)); }
@Test public void Should_javaTimerTaskImplementationHandleInterruptedException() throws Exception { doThrow(InterruptedException.class).when(taskQueueMock).put(same(taskMock)); ITimer timer = new TimerImpl(timerMock); timer.schedule(taskMock, 0); ArgumentCaptor<TimerTask> timerTaskArgumentCaptor = ArgumentCaptor.forClass(TimerTask.class); verify(timerMock).schedule(timerTaskArgumentCaptor.capture(), anyLong()); assertFalse(Thread.interrupted()); timerTaskArgumentCaptor.getValue().run(); assertTrue(Thread.interrupted()); }
@Test(expected = TaskScheduleException.class) public void Should_throwWhenCannotResolveQueue() throws Exception { when(IOC.resolve(same(taskQueueKey))).thenThrow(ResolutionException.class); ITimer timer = new TimerImpl(timerMock); timer.schedule(taskMock, 0); }
@Test public void Should_cancelCancelTheTask() throws Exception { ITimer timer = new TimerImpl(realTimer); timer.schedule(taskMock, System.currentTimeMillis() + 500).cancel(); Thread.sleep(500); verify(taskQueueMock, times(0)).put(any()); }