Reverts JmsTracingConfigurationTest test configuration

JmsTracingConfigurationTest flakes in Jenkins for some reason that appears
like either the test rule not being honored or tests run concurrently.
This commit is contained in:
Adrian Cole
2020-05-20 08:30:01 +08:00
parent 03be784678
commit d02493ca40

View File

@@ -18,6 +18,10 @@ package org.springframework.cloud.sleuth.instrument.messaging;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
@@ -29,17 +33,16 @@ import javax.jms.XAConnection;
import javax.jms.XAConnectionFactory;
import javax.resource.spi.ResourceAdapter;
import brave.Span.Kind;
import brave.Tracing;
import brave.handler.MutableSpan;
import brave.handler.SpanHandler;
import brave.propagation.CurrentTraceContext;
import brave.propagation.TraceContext;
import brave.test.IntegrationTestSpanHandler;
import org.apache.activemq.ra.ActiveMQActivationSpec;
import org.apache.activemq.ra.ActiveMQResourceAdapter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -72,15 +75,16 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class JmsTracingConfigurationTest {
@ClassRule
public static IntegrationTestSpanHandler spanHandler = new IntegrationTestSpanHandler();
final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(JmsTestTracingConfiguration.class,
AnnotationJmsListenerConfiguration.class, XAConfiguration.class,
SimpleJmsListenerConfiguration.class,
JcaJmsListenerConfiguration.class));
static void clearSpans(AssertableApplicationContext ctx) throws JMSException {
ctx.getBean(JmsTestTracingConfiguration.class).clearSpan();
}
static void checkConnection(AssertableApplicationContext ctx) throws JMSException {
// Not using try-with-resources as that doesn't exist in JMS 1.1
Connection con = ctx.getBean(ConnectionFactory.class).createConnection();
@@ -133,6 +137,7 @@ public class JmsTracingConfigurationTest {
@Test
public void tracesXAConnectionFactories() {
this.contextRunner.withUserConfiguration(XAConfiguration.class).run(ctx -> {
clearSpans(ctx);
checkConnection(ctx);
checkXAConnection(ctx);
});
@@ -141,6 +146,7 @@ public class JmsTracingConfigurationTest {
@Test
public void tracesTopicConnectionFactories() {
this.contextRunner.withUserConfiguration(XAConfiguration.class).run(ctx -> {
clearSpans(ctx);
checkConnection(ctx);
checkTopicConnection(ctx);
});
@@ -150,13 +156,13 @@ public class JmsTracingConfigurationTest {
public void tracesListener_jmsMessageListener() {
this.contextRunner.withUserConfiguration(SimpleJmsListenerConfiguration.class)
.run(ctx -> {
clearSpans(ctx);
ctx.getBean(JmsTemplate.class).convertAndSend("myQueue", "foo");
MutableSpan producer = spanHandler.takeRemoteSpan(Kind.PRODUCER);
MutableSpan consumer = spanHandler.takeRemoteSpan(Kind.CONSUMER);
MutableSpan listener = spanHandler.takeLocalSpan();
List<MutableSpan> trace = Arrays.asList(producer, consumer, listener);
Callable<MutableSpan> takeSpan = ctx.getBean("takeSpan",
Callable.class);
List<MutableSpan> trace = Arrays.asList(takeSpan.call(),
takeSpan.call(), takeSpan.call());
assertThat(trace).allSatisfy(s -> assertThat(s.traceId())
.isEqualTo(trace.get(0).traceId()));
@@ -166,16 +172,17 @@ public class JmsTracingConfigurationTest {
}
@Test
@Ignore("flakey")
public void tracesListener_annotationMessageListener() {
this.contextRunner.withUserConfiguration(AnnotationJmsListenerConfiguration.class)
.run(ctx -> {
clearSpans(ctx);
ctx.getBean(JmsTemplate.class).convertAndSend("myQueue", "foo");
MutableSpan producer = spanHandler.takeRemoteSpan(Kind.PRODUCER);
MutableSpan consumer = spanHandler.takeRemoteSpan(Kind.CONSUMER);
MutableSpan listener = spanHandler.takeLocalSpan();
List<MutableSpan> trace = Arrays.asList(producer, consumer, listener);
Callable<MutableSpan> takeSpan = ctx.getBean("takeSpan",
Callable.class);
List<MutableSpan> trace = Arrays.asList(takeSpan.call(),
takeSpan.call(), takeSpan.call());
assertThat(trace).allSatisfy(s -> assertThat(s.traceId())
.isEqualTo(trace.get(0).traceId()));
@@ -188,13 +195,13 @@ public class JmsTracingConfigurationTest {
public void tracesListener_jcaMessageListener() {
this.contextRunner.withUserConfiguration(JcaJmsListenerConfiguration.class)
.run(ctx -> {
clearSpans(ctx);
ctx.getBean(JmsTemplate.class).convertAndSend("myQueue", "foo");
MutableSpan producer = spanHandler.takeRemoteSpan(Kind.PRODUCER);
MutableSpan consumer = spanHandler.takeRemoteSpan(Kind.CONSUMER);
MutableSpan listener = spanHandler.takeLocalSpan();
List<MutableSpan> trace = Arrays.asList(producer, consumer, listener);
Callable<MutableSpan> takeSpan = ctx.getBean("takeSpan",
Callable.class);
List<MutableSpan> trace = Arrays.asList(takeSpan.call(),
takeSpan.call(), takeSpan.call());
assertThat(trace).allSatisfy(s -> assertThat(s.traceId())
.isEqualTo(trace.get(0).traceId()));
@@ -306,16 +313,49 @@ public class JmsTracingConfigurationTest {
}
@Configuration
@EnableAutoConfiguration(exclude = KafkaAutoConfiguration.class)
static class JmsTestTracingConfiguration {
}
@Bean
Tracing tracing(CurrentTraceContext currentTraceContext) {
return Tracing.newBuilder().addSpanHandler(spanHandler)
.currentTraceContext(currentTraceContext).build();
}
@Configuration
@EnableAutoConfiguration(exclude = KafkaAutoConfiguration.class)
// this should be able to use IntegrationSpanReporter, but Jenkins fails due to what
// appears as
// out of order tests..
class JmsTestTracingConfiguration {
/**
* When testing servers or asynchronous clients, spans are reported on a worker
* thread. In order to read them on the main thread, we use a concurrent queue. As
* some implementations report after a response is sent, we use a blocking queue to
* prevent race conditions in tests.
*/
BlockingQueue<MutableSpan> spans = new LinkedBlockingQueue<>();
void clearSpan() {
this.spans.clear();
}
/**
* Call this to block until a span was reported.
* @return span from queue
*/
@Bean
Callable<MutableSpan> takeSpan() {
return () -> {
MutableSpan result = this.spans.poll(3, TimeUnit.SECONDS);
assertThat(result).withFailMessage("MutableSpan was not reported")
.isNotNull();
return result;
};
}
@Bean
Tracing tracing(CurrentTraceContext currentTraceContext) {
return Tracing.newBuilder().addSpanHandler(new SpanHandler() {
@Override
public boolean end(TraceContext context, MutableSpan span, Cause cause) {
return spans.add(span);
}
}).currentTraceContext(currentTraceContext).build();
}
}