diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java index 09326eebd..a48918859 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java @@ -18,6 +18,9 @@ package org.springframework.cloud.sleuth.instrument.messaging; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; @@ -41,6 +44,7 @@ import org.springframework.cloud.sleuth.util.ExceptionUtils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.channel.ExecutorChannel; import org.springframework.integration.core.MessagingTemplate; import org.springframework.integration.support.MessageBuilder; import org.springframework.messaging.Message; @@ -73,6 +77,10 @@ public class TraceChannelInterceptorTests implements MessageHandler { @Qualifier("tracedChannel") private DirectChannel tracedChannel; + @Autowired + @Qualifier("tracedExecutorChannel") + private ExecutorChannel executorChannel; + @Autowired @Qualifier("ignoredChannel") private DirectChannel ignoredChannel; @@ -89,9 +97,12 @@ public class TraceChannelInterceptorTests implements MessageHandler { private Message message; private Span span; + + private CountDownLatch latch = new CountDownLatch(1); @Override public void handleMessage(Message message) throws MessagingException { + this.latch.countDown(); this.message = message; this.span = TestSpanContextHolder.getCurrentSpan(); if (message.getHeaders().containsKey("THROW_EXCEPTION")) { @@ -102,6 +113,7 @@ public class TraceChannelInterceptorTests implements MessageHandler { @Before public void init() { this.tracedChannel.subscribe(this); + this.executorChannel.subscribe(this); this.ignoredChannel.subscribe(this); this.accumulator.getSpans().clear(); } @@ -111,6 +123,7 @@ public class TraceChannelInterceptorTests implements MessageHandler { then(ExceptionUtils.getLastException()).isNull(); TestSpanContextHolder.removeCurrentSpan(); this.tracedChannel.unsubscribe(this); + this.executorChannel.unsubscribe(this); this.ignoredChannel.unsubscribe(this); this.accumulator.getSpans().clear(); } @@ -127,6 +140,19 @@ public class TraceChannelInterceptorTests implements MessageHandler { then(this.span.isExportable()).isFalse(); } + @Test + public void executableSpanCreation() throws Exception { + this.executorChannel.send(MessageBuilder.withPayload("hi") + .setHeader(TraceMessageHeaders.SAMPLED_NAME, Span.SPAN_NOT_SAMPLED).build()); + this.latch.await(1, TimeUnit.SECONDS); + assertNotNull("message was null", this.message); + + String spanId = this.message.getHeaders().get(TraceMessageHeaders.SPAN_ID_NAME, String.class); + then(spanId).isNotNull(); + then(TestSpanContextHolder.getCurrentSpan()).isNull(); + then(this.span.isExportable()).isFalse(); + } + @Test public void messageHeadersStillMutable() { this.tracedChannel.send(MessageBuilder.withPayload("hi") @@ -352,6 +378,11 @@ public class TraceChannelInterceptorTests implements MessageHandler { return new ArrayListSpanAccumulator(); } + @Bean + public ExecutorChannel tracedExecutorChannel() { + return new ExecutorChannel(Executors.newSingleThreadExecutor()); + } + @Bean public DirectChannel tracedChannel() { return new DirectChannel(); diff --git a/spring-cloud-sleuth-core/src/test/resources/logback.xml b/spring-cloud-sleuth-core/src/test/resources/logback.xml index 4d584ab3f..b7f228703 100644 --- a/spring-cloud-sleuth-core/src/test/resources/logback.xml +++ b/spring-cloud-sleuth-core/src/test/resources/logback.xml @@ -2,7 +2,7 @@ - +