ExecutorServiceWithTracing implements ExecutorService { @Override public <T> Future<T> submit(Callable<T> task) { return delegate.submit(new CallableWithTracing<>(task)); } 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 submit_callable_passes_through_to_delegate_with_tracing_wrapper() { Callable<?> origTaskMock = mock(Callable.class); Future<?> expectedResultMock = mock(Future.class); doReturn(expectedResultMock).when(executorServiceMock).submit(any(Callable.class)); TracingState expectedTracingState = generateTracingStateOnCurrentThread(); Future<?> result = instance.submit(origTaskMock); assertThat(result).isSameAs(expectedResultMock); verify(executorServiceMock).submit(callableCaptor.capture()); Callable<?> actualTask = callableCaptor.getValue(); verifyCallableWithTracingWrapper(actualTask, origTaskMock, expectedTracingState); verifyNoMoreInteractions(executorServiceMock); }
@Test public void submit_runnable_with_result_passes_through_to_delegate_with_tracing_wrapper() { Runnable origTaskMock = mock(Runnable.class); String resultArg = UUID.randomUUID().toString(); Future<String> expectedResultMock = mock(Future.class); doReturn(expectedResultMock).when(executorServiceMock).submit(any(Runnable.class), anyString()); TracingState expectedTracingState = generateTracingStateOnCurrentThread(); Future<String> result = instance.submit(origTaskMock, resultArg); assertThat(result).isSameAs(expectedResultMock); verify(executorServiceMock).submit(runnableCaptor.capture(), eq(resultArg)); Runnable actualTask = runnableCaptor.getValue(); verifyRunnableWithTracingWrapper(actualTask, origTaskMock, expectedTracingState); verifyNoMoreInteractions(executorServiceMock); }
@Test public void submit_runnable_passes_through_to_delegate_with_tracing_wrapper() { Runnable origTaskMock = mock(Runnable.class); Future<?> expectedResultMock = mock(Future.class); doReturn(expectedResultMock).when(executorServiceMock).submit(any(Runnable.class)); TracingState expectedTracingState = generateTracingStateOnCurrentThread(); Future<?> result = instance.submit(origTaskMock); assertThat(result).isSameAs(expectedResultMock); verify(executorServiceMock).submit(runnableCaptor.capture()); Runnable actualTask = runnableCaptor.getValue(); verifyRunnableWithTracingWrapper(actualTask, origTaskMock, expectedTracingState); verifyNoMoreInteractions(executorServiceMock); }