@Test public void actionsSubmittedToServices() throws Exception { WritableSequence<ThreadPoolService<?>> iterable = prepIterable(service1, service3); long millisTimeout = 100L; when(guardRail.acquirePermits(1L)).thenReturn(null); when(pattern.getPrecipices(1L, submitTimeNanos)).thenReturn(iterable); when(guardRail1.acquirePermits(1L, submitTimeNanos)).thenReturn(null); when(guardRail3.acquirePermits(1L, submitTimeNanos)).thenReturn(null); PrecipiceFuture<TimeoutableResult, String> f = poolPattern.submit(action, millisTimeout); verifyZeroInteractions(service2); verify(executor1).execute(task1Captor.capture()); verify(executor3).execute(task2Captor.capture()); verify(timeoutService1).scheduleTimeout(any(Timeout.class), eq(millisTimeout), eq(10L)); verify(timeoutService3).scheduleTimeout(any(Timeout.class), eq(millisTimeout), eq(10L)); CancellableTask<TimeoutableResult, ?> task1 = task1Captor.getValue(); CancellableTask<TimeoutableResult, ?> task2 = task1Captor.getValue(); assertNull(f.getResult()); task1.run(); task2.run(); assertEquals(TimeoutableResult.SUCCESS, f.getResult()); assertEquals("Service1", f.getValue()); }
public <T> PrecipiceFuture<TimeoutableResult, T> submit(final PatternCallable<T, C> action, long millisTimeout) { long nanoTime = acquirePermit(); Sequence<ThreadPoolService<?>> services = pattern.getPrecipices(1L, nanoTime); if (services.isEmpty()) { return handleAllReject(nanoTime); } PrecipicePromise<TimeoutableResult, T> promise = Asynchronous.getPromise(guardRail, 1L, nanoTime); long adjustedTimeout = DelayQueueTimeoutService.adjustTimeout(millisTimeout); for (ThreadPoolService<?> service : services) { PrecipicePromise<TimeoutableResult, T> internal = Asynchronous.getPromise(service.guardRail(), 1L, nanoTime, promise); final C context = serviceToContext.get(service); ExecutorService executor = service.getExecutor(); DelayQueueTimeoutService timeoutService = service.getTimeoutService(); Callable<T> callable = new CallableWithContext<>(action, context); CancellableTask<TimeoutableResult, T> task = TaskFactory.createTask(callable, internal); executor.execute(task); timeoutService.scheduleTimeout(new ThreadPoolTimeout<>(task), adjustedTimeout, nanoTime); } return promise.future(); }
ThreadPoolPattern implements Precipice<TimeoutableResult, PatternRejected> { public <T> PrecipiceFuture<TimeoutableResult, T> submit(final PatternCallable<T, C> action, long millisTimeout) { long nanoTime = acquirePermit(); Sequence<ThreadPoolService<?>> services = pattern.getPrecipices(1L, nanoTime); if (services.isEmpty()) { return handleAllReject(nanoTime); } PrecipicePromise<TimeoutableResult, T> promise = Asynchronous.getPromise(guardRail, 1L, nanoTime); long adjustedTimeout = DelayQueueTimeoutService.adjustTimeout(millisTimeout); for (ThreadPoolService<?> service : services) { PrecipicePromise<TimeoutableResult, T> internal = Asynchronous.getPromise(service.guardRail(), 1L, nanoTime, promise); final C context = serviceToContext.get(service); ExecutorService executor = service.getExecutor(); DelayQueueTimeoutService timeoutService = service.getTimeoutService(); Callable<T> callable = new CallableWithContext<>(action, context); CancellableTask<TimeoutableResult, T> task = TaskFactory.createTask(callable, internal); executor.execute(task); timeoutService.scheduleTimeout(new ThreadPoolTimeout<>(task), adjustedTimeout, nanoTime); } return promise.future(); } }
ThreadPoolPattern implements Precipice<TimeoutableResult, PatternRejected> { public <T> PrecipiceFuture<TimeoutableResult, T> submit(final PatternCallable<T, C> action, long millisTimeout) { long nanoTime = acquirePermit(); Sequence<ThreadPoolService<?>> services = pattern.getPrecipices(1L, nanoTime); if (services.isEmpty()) { return handleAllReject(nanoTime); } PrecipicePromise<TimeoutableResult, T> promise = Asynchronous.getPromise(guardRail, 1L, nanoTime); long adjustedTimeout = DelayQueueTimeoutService.adjustTimeout(millisTimeout); for (ThreadPoolService<?> service : services) { PrecipicePromise<TimeoutableResult, T> internal = Asynchronous.getPromise(service.guardRail(), 1L, nanoTime, promise); final C context = serviceToContext.get(service); ExecutorService executor = service.getExecutor(); DelayQueueTimeoutService timeoutService = service.getTimeoutService(); Callable<T> callable = new CallableWithContext<>(action, context); CancellableTask<TimeoutableResult, T> task = TaskFactory.createTask(callable, internal); executor.execute(task); timeoutService.scheduleTimeout(new ThreadPoolTimeout<>(task), adjustedTimeout, nanoTime); } return promise.future(); } ThreadPoolPattern(Map<ThreadPoolService<?>, C> serviceToContext, GuardRail<TimeoutableResult,
            PatternRejected> guardRail, PatternStrategy strategy);  ThreadPoolPattern(Map<ThreadPoolService<?>, C> serviceToContext,
                             GuardRail<TimeoutableResult, PatternRejected> guardRail,
                             Pattern<TimeoutableResult, ThreadPoolService<?>> pattern); }
ThreadPoolPattern implements Precipice<TimeoutableResult, PatternRejected> { public <T> PrecipiceFuture<TimeoutableResult, T> submit(final PatternCallable<T, C> action, long millisTimeout) { long nanoTime = acquirePermit(); Sequence<ThreadPoolService<?>> services = pattern.getPrecipices(1L, nanoTime); if (services.isEmpty()) { return handleAllReject(nanoTime); } PrecipicePromise<TimeoutableResult, T> promise = Asynchronous.getPromise(guardRail, 1L, nanoTime); long adjustedTimeout = DelayQueueTimeoutService.adjustTimeout(millisTimeout); for (ThreadPoolService<?> service : services) { PrecipicePromise<TimeoutableResult, T> internal = Asynchronous.getPromise(service.guardRail(), 1L, nanoTime, promise); final C context = serviceToContext.get(service); ExecutorService executor = service.getExecutor(); DelayQueueTimeoutService timeoutService = service.getTimeoutService(); Callable<T> callable = new CallableWithContext<>(action, context); CancellableTask<TimeoutableResult, T> task = TaskFactory.createTask(callable, internal); executor.execute(task); timeoutService.scheduleTimeout(new ThreadPoolTimeout<>(task), adjustedTimeout, nanoTime); } return promise.future(); } ThreadPoolPattern(Map<ThreadPoolService<?>, C> serviceToContext, GuardRail<TimeoutableResult,
            PatternRejected> guardRail, PatternStrategy strategy);  ThreadPoolPattern(Map<ThreadPoolService<?>, C> serviceToContext,
                             GuardRail<TimeoutableResult, PatternRejected> guardRail,
                             Pattern<TimeoutableResult, ThreadPoolService<?>> pattern); @Override GuardRail<TimeoutableResult, PatternRejected> guardRail(); PrecipiceFuture<TimeoutableResult, T> submit(final PatternCallable<T, C> action, long millisTimeout); void shutdown(); }
ThreadPoolPattern implements Precipice<TimeoutableResult, PatternRejected> { public <T> PrecipiceFuture<TimeoutableResult, T> submit(final PatternCallable<T, C> action, long millisTimeout) { long nanoTime = acquirePermit(); Sequence<ThreadPoolService<?>> services = pattern.getPrecipices(1L, nanoTime); if (services.isEmpty()) { return handleAllReject(nanoTime); } PrecipicePromise<TimeoutableResult, T> promise = Asynchronous.getPromise(guardRail, 1L, nanoTime); long adjustedTimeout = DelayQueueTimeoutService.adjustTimeout(millisTimeout); for (ThreadPoolService<?> service : services) { PrecipicePromise<TimeoutableResult, T> internal = Asynchronous.getPromise(service.guardRail(), 1L, nanoTime, promise); final C context = serviceToContext.get(service); ExecutorService executor = service.getExecutor(); DelayQueueTimeoutService timeoutService = service.getTimeoutService(); Callable<T> callable = new CallableWithContext<>(action, context); CancellableTask<TimeoutableResult, T> task = TaskFactory.createTask(callable, internal); executor.execute(task); timeoutService.scheduleTimeout(new ThreadPoolTimeout<>(task), adjustedTimeout, nanoTime); } return promise.future(); } ThreadPoolPattern(Map<ThreadPoolService<?>, C> serviceToContext, GuardRail<TimeoutableResult,
            PatternRejected> guardRail, PatternStrategy strategy);  ThreadPoolPattern(Map<ThreadPoolService<?>, C> serviceToContext,
                             GuardRail<TimeoutableResult, PatternRejected> guardRail,
                             Pattern<TimeoutableResult, ThreadPoolService<?>> pattern); @Override GuardRail<TimeoutableResult, PatternRejected> guardRail(); PrecipiceFuture<TimeoutableResult, T> submit(final PatternCallable<T, C> action, long millisTimeout); void shutdown();  }