@Override public JobHandle scheduleRecurring( Group group, final Runnable runnable, long period, TimeUnit timeUnit ) { return scheduleRecurring( group, runnable, 0, period, timeUnit ); }
@Test( timeout = 10_000 ) public void shouldRunRecurringJob() throws Throwable { // Given long period = 10; int count = 5; life.start(); // When scheduler.scheduleRecurring( Group.INDEX_POPULATION, countInvocationsJob, period, MILLISECONDS ); awaitInvocationCount( count ); scheduler.shutdown(); // Then assert that the recurring job was stopped (when the scheduler was shut down) int actualInvocations = invocations.get(); sleep( period * 5 ); assertThat( invocations.get(), equalTo( actualInvocations ) ); }
@Test( timeout = 10_000 ) public void scheduledTasksThatThrowsShouldStop() throws Exception { CentralJobScheduler scheduler = new CentralJobScheduler(); scheduler.init(); BinaryLatch triggerLatch = new BinaryLatch(); RuntimeException boom = new RuntimeException( "boom" ); AtomicInteger triggerCounter = new AtomicInteger(); Runnable job = () -> { triggerCounter.incrementAndGet(); triggerLatch.release(); throw boom; }; scheduler.scheduleRecurring( Group.INDEX_POPULATION, job, 1, TimeUnit.MILLISECONDS ); triggerLatch.await(); Thread.sleep( 50 ); assertThat( triggerCounter.get(), is( 1 ) ); }
handles.add( scheduler.scheduleRecurring( Group.INDEX_POPULATION, task, Integer.MAX_VALUE, TimeUnit.MILLISECONDS ) ); handles.add( scheduler.scheduleRecurring( Group.INDEX_POPULATION, task, 0, Integer.MAX_VALUE, TimeUnit.MILLISECONDS ) );
@Test( timeout = 10_000 ) public void scheduledTasksThatThrowsMustPropagateException() throws Exception { CentralJobScheduler scheduler = new CentralJobScheduler(); scheduler.init(); RuntimeException boom = new RuntimeException( "boom" ); AtomicInteger triggerCounter = new AtomicInteger(); Runnable job = () -> { triggerCounter.incrementAndGet(); throw boom; }; JobHandle handle = scheduler.scheduleRecurring( Group.INDEX_POPULATION, job, 1, TimeUnit.MILLISECONDS ); try { handle.waitTermination(); fail( "waitTermination should have failed." ); } catch ( ExecutionException e ) { assertThat( e.getCause(), is( boom ) ); } }
@Test public void shouldCancelRecurringJob() throws Exception { // Given long period = 2; life.start(); JobHandle jobHandle = scheduler.scheduleRecurring( Group.INDEX_POPULATION, countInvocationsJob, period, MILLISECONDS ); awaitFirstInvocation(); // When jobHandle.cancel( false ); try { jobHandle.waitTermination(); fail( "Task should be terminated" ); } catch ( CancellationException ignored ) { // task should be canceled } // Then int recorded = invocations.get(); sleep( period * 100 ); // we can have task that is already running during cancellation so lets count it as well assertThat( invocations.get(), both( greaterThanOrEqualTo( recorded ) ).and( lessThanOrEqualTo( recorded + 1 ) ) ); }
@Override public JobHandle scheduleRecurring( Group group, final Runnable runnable, long period, TimeUnit timeUnit ) { return scheduleRecurring( group, runnable, 0, period, timeUnit ); }