Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions context/src/main/java/io/opentelemetry/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ default Executor wrap(Executor executor) {
* making this the {@linkplain Context#current() current context} before each execution.
*/
default ExecutorService wrap(ExecutorService executor) {
if (executor instanceof ContextExecutorService) {
return executor;
}
return new ContextExecutorService(this, executor);
}

Expand All @@ -277,6 +280,9 @@ default ExecutorService wrap(ExecutorService executor) {
* execution.
*/
default ScheduledExecutorService wrap(ScheduledExecutorService executor) {
if (executor instanceof ContextScheduledExecutorService) {
return executor;
}
return new ContextScheduledExecutorService(this, executor);
}

Expand Down
37 changes: 37 additions & 0 deletions context/src/test/java/io/opentelemetry/context/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,24 @@ void wrapExecutor() {
}
}

@Test
void wrapExecutorService() {
// given
ExecutorService executorService = Executors.newSingleThreadExecutor();

// when
ExecutorService firstWrap = CAT.wrap(executorService);
ExecutorService secondWrap = CAT.wrap(firstWrap);

// then
assertThat(firstWrap).isInstanceOf(ContextExecutorService.class);
assertThat(((ContextExecutorService) firstWrap).context()).isEqualTo(CAT);
assertThat(((ContextExecutorService) firstWrap).delegate()).isEqualTo(executorService);
assertThat(secondWrap).isInstanceOf(ContextExecutorService.class);
assertThat(((ContextExecutorService) secondWrap).context()).isEqualTo(CAT);
assertThat(((ContextExecutorService) secondWrap).delegate()).isEqualTo(executorService);
}

@Nested
@TestInstance(Lifecycle.PER_CLASS)
class WrapExecutorService {
Expand Down Expand Up @@ -504,6 +522,25 @@ void invokeAnyTimeout() throws Exception {
}
}

@Test
void wrapScheduledExecutorService() {
// given
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

// when
ScheduledExecutorService firstWrap = CAT.wrap(executorService);
ScheduledExecutorService secondWrap = CAT.wrap(firstWrap);

// then
assertThat(firstWrap).isInstanceOf(ContextScheduledExecutorService.class);
assertThat(((ContextScheduledExecutorService) firstWrap).context()).isEqualTo(CAT);
assertThat(((ContextScheduledExecutorService) firstWrap).delegate()).isEqualTo(executorService);
assertThat(secondWrap).isInstanceOf(ContextScheduledExecutorService.class);
assertThat(((ContextScheduledExecutorService) secondWrap).context()).isEqualTo(CAT);
assertThat(((ContextScheduledExecutorService) secondWrap).delegate())
.isEqualTo(executorService);
}

@Nested
@TestInstance(Lifecycle.PER_CLASS)
class WrapScheduledExecutorService {
Expand Down
Loading