ExecutorServiceWithTracing implements ExecutorService { @Override public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException { return delegate.invokeAny(convertToCallableWithTracingList(tasks)); } ExecutorServiceWithTracing(ExecutorService delegate); static ExecutorServiceWithTracing withTracing(ExecutorService delegate); @Override void shutdown(); @Override List<Runnable> shutdownNow(); @Override boolean isShutdown(); @Override boolean isTerminated(); @Override boolean awaitTermination(long timeout, TimeUnit unit); @Override Future<T> submit(Callable<T> task); @Override Future<T> submit(Runnable task, T result); @Override Future<?> submit(Runnable task); @Override List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks); @Override List<Future<T>> invokeAll(
        Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit
    ); @Override T invokeAny(Collection<? extends Callable<T>> tasks); @Override T invokeAny(
        Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit
    ); @Override void execute(Runnable command);  }
@Test public void invokeAny_no_timeout_passes_through_to_delegate_with_tracing_wrapped_tasks() throws InterruptedException, ExecutionException { List<Callable<Object>> origTasks = Arrays.asList(mock(Callable.class), mock(Callable.class)); Object expectedResultMock = mock(Object.class); doReturn(expectedResultMock).when(executorServiceMock).invokeAny(any(Collection.class)); TracingState expectedTracingState = generateTracingStateOnCurrentThread(); Object result = instance.invokeAny(origTasks); assertThat(result).isSameAs(expectedResultMock); verify(executorServiceMock).invokeAny(collectionCaptor.capture()); List<Callable<Object>> actualTasks = (List<Callable<Object>>) collectionCaptor.getValue(); assertThat(actualTasks).hasSameSizeAs(origTasks); for (int i = 0; i < actualTasks.size(); i++) { Callable<Object> actualTask = actualTasks.get(i); Callable<Object> origTaskMock = origTasks.get(i); verifyCallableWithTracingWrapper(actualTask, origTaskMock, expectedTracingState); } verifyNoMoreInteractions(executorServiceMock); }
@Test public void invokeAny_with_timeout_passes_through_to_delegate_with_tracing_wrapped_tasks() throws InterruptedException, TimeoutException, ExecutionException { List<Callable<Object>> origTasks = Arrays.asList(mock(Callable.class), mock(Callable.class)); Object expectedResultMock = mock(Object.class); doReturn(expectedResultMock).when(executorServiceMock) .invokeAny(any(Collection.class), anyLong(), any(TimeUnit.class)); long timeoutValue = 42; TimeUnit timeoutTimeUnit = TimeUnit.MINUTES; TracingState expectedTracingState = generateTracingStateOnCurrentThread(); Object result = instance.invokeAny(origTasks, timeoutValue, timeoutTimeUnit); assertThat(result).isSameAs(expectedResultMock); verify(executorServiceMock).invokeAny(collectionCaptor.capture(), eq(timeoutValue), eq(timeoutTimeUnit)); List<Callable<Object>> actualTasks = (List<Callable<Object>>) collectionCaptor.getValue(); assertThat(actualTasks).hasSameSizeAs(origTasks); for (int i = 0; i < actualTasks.size(); i++) { Callable<Object> actualTask = actualTasks.get(i); Callable<Object> origTaskMock = origTasks.get(i); verifyCallableWithTracingWrapper(actualTask, origTaskMock, expectedTracingState); } verifyNoMoreInteractions(executorServiceMock); }