Skip to content

Commit 7f86458

Browse files
authored
Fix duplicated ExecutorService wrap (#7245)
1 parent 3eb7ef0 commit 7f86458

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

context/src/main/java/io/opentelemetry/context/Context.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ default Executor wrap(Executor executor) {
268268
* making this the {@linkplain Context#current() current context} before each execution.
269269
*/
270270
default ExecutorService wrap(ExecutorService executor) {
271+
if (executor instanceof ContextExecutorService) {
272+
return executor;
273+
}
271274
return new ContextExecutorService(this, executor);
272275
}
273276

@@ -277,6 +280,9 @@ default ExecutorService wrap(ExecutorService executor) {
277280
* execution.
278281
*/
279282
default ScheduledExecutorService wrap(ScheduledExecutorService executor) {
283+
if (executor instanceof ContextScheduledExecutorService) {
284+
return executor;
285+
}
280286
return new ContextScheduledExecutorService(this, executor);
281287
}
282288

context/src/test/java/io/opentelemetry/context/ContextTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,24 @@ void wrapExecutor() {
361361
}
362362
}
363363

364+
@Test
365+
void wrapExecutorService() {
366+
// given
367+
ExecutorService executorService = Executors.newSingleThreadExecutor();
368+
369+
// when
370+
ExecutorService firstWrap = CAT.wrap(executorService);
371+
ExecutorService secondWrap = CAT.wrap(firstWrap);
372+
373+
// then
374+
assertThat(firstWrap).isInstanceOf(ContextExecutorService.class);
375+
assertThat(((ContextExecutorService) firstWrap).context()).isEqualTo(CAT);
376+
assertThat(((ContextExecutorService) firstWrap).delegate()).isEqualTo(executorService);
377+
assertThat(secondWrap).isInstanceOf(ContextExecutorService.class);
378+
assertThat(((ContextExecutorService) secondWrap).context()).isEqualTo(CAT);
379+
assertThat(((ContextExecutorService) secondWrap).delegate()).isEqualTo(executorService);
380+
}
381+
364382
@Nested
365383
@TestInstance(Lifecycle.PER_CLASS)
366384
class WrapExecutorService {
@@ -504,6 +522,25 @@ void invokeAnyTimeout() throws Exception {
504522
}
505523
}
506524

525+
@Test
526+
void wrapScheduledExecutorService() {
527+
// given
528+
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
529+
530+
// when
531+
ScheduledExecutorService firstWrap = CAT.wrap(executorService);
532+
ScheduledExecutorService secondWrap = CAT.wrap(firstWrap);
533+
534+
// then
535+
assertThat(firstWrap).isInstanceOf(ContextScheduledExecutorService.class);
536+
assertThat(((ContextScheduledExecutorService) firstWrap).context()).isEqualTo(CAT);
537+
assertThat(((ContextScheduledExecutorService) firstWrap).delegate()).isEqualTo(executorService);
538+
assertThat(secondWrap).isInstanceOf(ContextScheduledExecutorService.class);
539+
assertThat(((ContextScheduledExecutorService) secondWrap).context()).isEqualTo(CAT);
540+
assertThat(((ContextScheduledExecutorService) secondWrap).delegate())
541+
.isEqualTo(executorService);
542+
}
543+
507544
@Nested
508545
@TestInstance(Lifecycle.PER_CLASS)
509546
class WrapScheduledExecutorService {

0 commit comments

Comments
 (0)