### Question:
TimerService { public synchronized Timer create( TimerName name, Group group, TimeoutHandler handler ) { Timer timer = new Timer( name, scheduler, log, group, handler ); timers.add( timer ); return timer; } TimerService( JobScheduler scheduler, LogProvider logProvider ); synchronized Timer create( TimerName name, Group group, TimeoutHandler handler ); synchronized Collection<Timer> getTimers( TimerName name ); synchronized void invoke( TimerName name );  }### Answer:
@Test public void shouldAwaitCancellationUnderRealScheduler() throws Throwable { JobScheduler scheduler = createInitialisedScheduler(); scheduler.start(); TimerService timerService = new TimerService( scheduler, FormattedLogProvider.toOutputStream( System.out ) ); CountDownLatch started = new CountDownLatch( 1 ); CountDownLatch finished = new CountDownLatch( 1 ); TimeoutHandler handlerA = timer -> { started.countDown(); finished.await(); }; TimeoutHandler handlerB = timer -> finished.countDown(); Timer timerA = timerService.create( Timers.TIMER_A, group, handlerA ); timerA.set( fixedTimeout( 0, SECONDS ) ); started.await(); Timer timerB = timerService.create( Timers.TIMER_B, group, handlerB ); timerB.set( fixedTimeout( 2, SECONDS ) ); timerA.cancel( SYNC_WAIT ); assertEquals( 0, finished.getCount() ); scheduler.stop(); scheduler.shutdown(); }
@Test public void shouldBeAbleToCancelBeforeHandlingWithRealScheduler() throws Throwable { JobScheduler scheduler = createInitialisedScheduler(); scheduler.start(); TimerService timerService = new TimerService( scheduler, FormattedLogProvider.toOutputStream( System.out ) ); TimeoutHandler handlerA = timer -> {}; Timer timer = timerService.create( Timers.TIMER_A, group, handlerA ); timer.set( fixedTimeout( 2, SECONDS ) ); timer.cancel( SYNC_WAIT ); scheduler.stop(); scheduler.shutdown(); }