public void testDelayedWorks() throws InterruptedException { final AppScheduledExecutorService service = new AppScheduledExecutorService(getName()); final List<LogInfo> log = Collections.synchronizedList(new ArrayList<>()); assertFalse(service.isShutdown()); assertFalse(service.isTerminated()); service.invokeAll( Collections.nCopies( service.getBackendPoolCorePoolSize() + 1, Executors.callable(EmptyRunnable.getInstance()))); // pre-start all threads int delay = 1000; long start = System.currentTimeMillis(); ScheduledFuture<?> f1 = service.schedule( () -> { log.add(new LogInfo(1)); TimeoutUtil.sleep(10); }, delay, TimeUnit.MILLISECONDS); assertFalse(service.isShutdown()); assertFalse(service.isTerminated()); ScheduledFuture<?> f2 = service.schedule( () -> { log.add(new LogInfo(2)); TimeoutUtil.sleep(10); }, delay, TimeUnit.MILLISECONDS); assertFalse(service.isShutdown()); assertFalse(service.isTerminated()); ScheduledFuture<?> f3 = service.schedule( () -> { log.add(new LogInfo(3)); TimeoutUtil.sleep(10); }, delay, TimeUnit.MILLISECONDS); assertFalse(service.isShutdown()); assertFalse(service.isTerminated()); Future<?> f4 = service.submit((Runnable) () -> log.add(new LogInfo(4))); assertFalse(service.isShutdown()); assertFalse(service.isTerminated()); assertFalse(f1.isDone()); assertFalse(f2.isDone()); assertFalse(f3.isDone()); TimeoutUtil.sleep(delay / 2); long elapsed = System.currentTimeMillis() - start; // can be > delay/2 on overloaded agent assertEquals(String.valueOf(f1.isDone()), elapsed > delay, f1.isDone()); assertEquals(String.valueOf(f2.isDone()), elapsed > delay, f2.isDone()); assertEquals(String.valueOf(f3.isDone()), elapsed > delay, f3.isDone()); assertTrue(f4.isDone()); TimeoutUtil.sleep(delay / 2 + 500); assertTrue(f1.isDone()); assertTrue(f2.isDone()); assertTrue(f3.isDone()); assertTrue(f4.isDone()); assertEquals(4, log.size()); assertEquals(4, log.get(0).runnable); List<Thread> threads = Arrays.asList(log.get(1).currentThread, log.get(2).currentThread, log.get(3).currentThread); assertEquals( threads.toString(), 3, new HashSet<>(threads).size()); // must be executed in parallel service.shutdownAppScheduledExecutorService(); assertTrue(service.awaitTermination(10, TimeUnit.SECONDS)); }