[#89] Initial approach to the integration tests

- Fixed the tests
- Updated surefire
- Added integration tests (for the moment ignored)
- Fixed wrong surefire setup
This commit is contained in:
Marcin Grzejszczak
2016-01-08 19:08:34 +01:00
parent 7c54ad9e66
commit 9a8dc17d8c
29 changed files with 661 additions and 138 deletions

View File

@@ -62,5 +62,12 @@
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<!--
Turns on jdk8 for tests
-->
<id>ide</id>
<activation><activeByDefault>true</activeByDefault></activation>
</profile>
</profiles>
</settings>

View File

@@ -69,10 +69,11 @@ image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-sleuth/master
== Building
:jdkversion: 1.7
:testjdkversion: 1.8
=== Basic Compile and Test
To build the source you will need to install JDK {jdkversion}.
To build the source you will need to install JDK {jdkversion}. However to run the tests you need {testjdkversion}.
Spring Cloud uses Maven for most build-related activities, and you
should be able to get off the ground quite quickly by cloning the

82
pom.xml
View File

@@ -89,37 +89,6 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<useFile>false</useFile>
<includes>
<include>**/*Spec.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>signature-check</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java17</artifactId>
<version>1.0</version>
</signature>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
@@ -162,11 +131,6 @@
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
@@ -185,6 +149,12 @@
<version>1.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
@@ -194,6 +164,7 @@
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.testTarget>1.8</maven.compiler.testTarget>
<maven.compiler.testSource>1.8</maven.compiler.testSource>
<surefire.plugin.version>2.9</surefire.plugin.version>
</properties>
<profiles>
@@ -244,6 +215,45 @@
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>integration</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<useFile>false</useFile>
<groups>tools.DockerTests</groups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>!integration</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<useFile>false</useFile>
<excludes>
<exclude>**/Abstract*.java</exclude>
<exclude>**/*DockerTests.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>ide</id>
<activation>

View File

@@ -95,7 +95,6 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@@ -16,20 +16,20 @@
package org.springframework.cloud.sleuth.instrument;
import java.util.concurrent.Callable;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.cloud.sleuth.TraceManager;
import lombok.EqualsAndHashCode;
import lombok.Value;
import java.util.concurrent.Callable;
/**
* @author Spencer Gibb
*/
@Value
@EqualsAndHashCode(callSuper = false)
public class TraceCallable<V> extends TraceDelegate<Callable<V>>implements Callable<V> {
public class TraceCallable<V> extends TraceDelegate<Callable<V>> implements Callable<V> {
public TraceCallable(TraceManager traceManager, Callable<V> delegate) {
super(traceManager, delegate);

View File

@@ -27,7 +27,7 @@ import lombok.Value;
*/
@Value
@EqualsAndHashCode(callSuper = false)
public class TraceRunnable extends TraceDelegate<Runnable>implements Runnable {
public class TraceRunnable extends TraceDelegate<Runnable> implements Runnable {
public TraceRunnable(TraceManager traceManager, Runnable delegate) {
super(traceManager, delegate);

View File

@@ -16,13 +16,13 @@
package org.springframework.cloud.sleuth.instrument.hystrix;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.cloud.sleuth.TraceManager;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixThreadPoolKey;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.cloud.sleuth.TraceManager;
import org.springframework.cloud.sleuth.trace.TraceContextHolder;
/**
* Abstraction over {@code HystrixCommand} that wraps command execution with Trace setting
@@ -71,6 +71,7 @@ public abstract class TraceCommand<R> extends HystrixCommand<R> {
@Override
protected R run() throws Exception {
enforceThatHystrixThreadIsNotPolutedByPreviousTraces();
Trace trace = this.traceManager.startSpan(getCommandKey().name(), parentSpan);
try {
return doRun();
@@ -79,5 +80,10 @@ public abstract class TraceCommand<R> extends HystrixCommand<R> {
}
}
// TODO: Do more analysis why this is nor removed properly
private void enforceThatHystrixThreadIsNotPolutedByPreviousTraces() {
TraceContextHolder.removeCurrentTrace();
}
public abstract R doRun() throws Exception;
}

View File

@@ -16,17 +16,8 @@
package org.springframework.cloud.sleuth;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.cloud.sleuth.event.SpanAcquiredEvent;
@@ -34,10 +25,19 @@ import org.springframework.cloud.sleuth.event.SpanReleasedEvent;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.sampler.IsTracingSampler;
import org.springframework.cloud.sleuth.trace.DefaultTraceManager;
import org.springframework.cloud.sleuth.trace.TraceContextHolder;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.JdkIdGenerator;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.*;
/**
* @author Spencer Gibb
*/
@@ -48,6 +48,16 @@ public class DefaultTraceManagerTests {
public static final String IMPORTANT_WORK_2 = "important work 2";
public static final int NUM_SPANS = 3;
@Before
public void setup() {
TraceContextHolder.removeCurrentTrace();
}
@After
public void clean() {
TraceContextHolder.removeCurrentTrace();
}
@Test
public void tracingWorks() {
ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);

View File

@@ -1,11 +1,6 @@
package org.springframework.cloud.sleuth.instrument;
import static org.assertj.core.api.BDDAssertions.then;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
@@ -18,6 +13,11 @@ import org.springframework.cloud.sleuth.trace.TraceContextHolder;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.JdkIdGenerator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.assertj.core.api.BDDAssertions.then;
@RunWith(MockitoJUnitRunner.class)
public class TraceRunnableTest {
@@ -25,8 +25,8 @@ public class TraceRunnableTest {
TraceManager traceManager = new DefaultTraceManager(new AlwaysSampler(),
new JdkIdGenerator(), Mockito.mock(ApplicationEventPublisher.class));
@Before
public void init() {
@After
public void cleanup() {
TraceContextHolder.removeCurrentTrace();
}

View File

@@ -1,20 +1,5 @@
package org.springframework.cloud.sleuth.instrument.executor;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -28,10 +13,24 @@ import org.springframework.cloud.sleuth.event.SpanReleasedEvent;
import org.springframework.cloud.sleuth.instrument.TraceRunnable;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.sleuth.trace.DefaultTraceManager;
import org.springframework.cloud.sleuth.trace.TraceContextHolder;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.JdkIdGenerator;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.*;
public class TraceableExecutorServiceTests {
private ApplicationEventPublisher publisher;
private ExecutorService traceManagerableExecutorService;
@@ -55,6 +54,7 @@ public class TraceableExecutorServiceTests {
this.traceManager = null;
this.traceManagerableExecutorService.shutdown();
this.executorService.shutdown();
TraceContextHolder.removeCurrentTrace();
}
@Test

View File

@@ -1,9 +1,7 @@
package org.springframework.cloud.sleuth.instrument.hystrix;
import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then;
import java.util.concurrent.atomic.AtomicReference;
import com.jayway.awaitility.Awaitility;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -18,8 +16,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.jayway.awaitility.Awaitility;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import java.util.concurrent.atomic.AtomicReference;
import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {
@@ -29,6 +28,7 @@ public class SpanPassingForHystrixViaAnnotationsITest {
@Autowired HystrixCommandInvocationSpanCatcher hystrixCommandInvocationSpanCatcher;
@Autowired TraceManager traceManager;
@Test
public void should_set_span_on_an_hystrix_command_annotated_method() {
Span span = givenASpanInCurrentThread();

View File

@@ -1,10 +1,9 @@
package org.springframework.cloud.sleuth.instrument.hystrix;
import static com.netflix.hystrix.HystrixCommand.Setter.withGroupKey;
import static com.netflix.hystrix.HystrixCommandGroupKey.Factory.asKey;
import static org.assertj.core.api.BDDAssertions.then;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.cloud.sleuth.MilliSpan;
@@ -16,8 +15,9 @@ import org.springframework.cloud.sleuth.trace.TraceContextHolder;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.JdkIdGenerator;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import static com.netflix.hystrix.HystrixCommand.Setter.withGroupKey;
import static com.netflix.hystrix.HystrixCommandGroupKey.Factory.asKey;
import static org.assertj.core.api.BDDAssertions.then;
public class TraceCommandTest {
@@ -25,9 +25,20 @@ public class TraceCommandTest {
TraceManager traceManager = new DefaultTraceManager(new AlwaysSampler(),
new JdkIdGenerator(), Mockito.mock(ApplicationEventPublisher.class));
@Before
public void setup() {
TraceContextHolder.removeCurrentTrace();
}
@After
public void cleanup() {
TraceContextHolder.removeCurrentTrace();
}
@Test
public void should_remove_span_from_thread_local_after_finishing_work()
throws Exception {
TraceContextHolder.removeCurrentTrace();
Trace firstTraceFromHystrix = givenACommandWasExecuted(traceReturningCommand());
Trace secondTraceFromHystrix = whenCommandIsExecuted(traceReturningCommand());

View File

@@ -1,19 +1,7 @@
package org.springframework.cloud.sleuth.instrument.web;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static java.util.concurrent.TimeUnit.SECONDS;
import static junitparams.JUnitParamsRunner.$;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.concurrent.Callable;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
@@ -38,10 +26,17 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.async.WebAsyncTask;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import java.util.concurrent.Callable;
@SpringApplicationConfiguration(classes = {RestTemplateTraceAspectITest.CorrelationIdAspectSpecConfiguration.class})
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static java.util.concurrent.TimeUnit.SECONDS;
import static junitparams.JUnitParamsRunner.$;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringApplicationConfiguration(classes = {RestTemplateTraceAspectITest.CorrelationIdAspectTestConfiguration.class})
@RunWith(JUnitParamsRunner.class)
public class RestTemplateTraceAspectITest extends MvcWiremockITest {
@@ -92,7 +87,7 @@ public class RestTemplateTraceAspectITest extends MvcWiremockITest {
@EnableAsync
@DefaultTestAutoConfiguration
@Import(AspectTestingController.class)
public static class CorrelationIdAspectSpecConfiguration {
public static class CorrelationIdAspectTestConfiguration {
}
@RestController

View File

@@ -30,6 +30,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.JdkIdGenerator;
import org.springframework.web.bind.annotation.RequestHeader;
@@ -42,9 +43,9 @@ import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { TraceWebAutoConfiguration.class,
FeignTraceTest.TestConfiguration.class })
@SpringApplicationConfiguration(classes = { FeignTraceTest.TestConfiguration.class })
@WebIntegrationTest(value = { "spring.application.name=fooservice" }, randomPort = true)
@DirtiesContext
public class FeignTraceTest {
@Autowired
@@ -63,13 +64,13 @@ public class FeignTraceTest {
}
@Test
public void shouldWorkWhenNotTracing() {
public void shouldCreateANewSpanWhenNoPreviousTracingWasPresent() {
// when
ResponseEntity<String> response = this.testFeignInterface.getNoTrace();
// then
then(getHeader(response, Trace.TRACE_ID_NAME)).isNull();
then(this.listener.getEvents()).isEmpty();
then(getHeader(response, Trace.TRACE_ID_NAME)).isNotNull();
then(this.listener.getEvents()).isNotEmpty();
}
@Test
@@ -85,7 +86,6 @@ public class FeignTraceTest {
// then
then(getHeader(response, Trace.TRACE_ID_NAME)).isEqualTo(currentTraceId);
then(getHeader(response, Trace.PARENT_ID_NAME)).isEqualTo(currentParentId);
then(this.listener.getEvents().size()).isEqualTo(2);
}
@@ -149,7 +149,7 @@ public class FeignTraceTest {
@RequestMapping(value = "/notrace", method = RequestMethod.GET)
public String notrace(
@RequestHeader(name = Trace.TRACE_ID_NAME, required = false) String traceId) {
then(traceId).isNull();
then(traceId).isNotNull();
return "OK";
}

View File

@@ -66,6 +66,7 @@ public class TraceRestTemplateInterceptorTests {
new JdkIdGenerator(), this.publisher);
this.template.setInterceptors(Arrays.<ClientHttpRequestInterceptor>asList(
new TraceRestTemplateInterceptor(this.traces)));
TraceContextHolder.removeCurrentTrace();
}
@After

View File

@@ -18,6 +18,7 @@
<modules>
<module>spring-cloud-sleuth-sample</module>
<module>spring-cloud-sleuth-sample-test-core</module>
<module>spring-cloud-sleuth-sample-messaging</module>
<module>spring-cloud-sleuth-sample-ribbon</module>
<module>spring-cloud-sleuth-sample-zipkin</module>
@@ -26,17 +27,27 @@
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<!--skip deploy (this is just a test module) -->
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<pluginManagement>
<plugins>
<plugin>
<!--skip deploy (this is just a test module) -->
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-sample-test-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@@ -39,6 +39,10 @@
</plugins>
</build>
<properties>
<testcontainers.jackson.version>2.1.0</testcontainers.jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
@@ -73,6 +77,21 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-sample-test-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -16,15 +16,14 @@
package sample;
import java.util.Random;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.TraceManager;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.Random;
/**
* @author Spencer Gibb
*/

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.cloud.sleuth" level="DEBUG"/>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>

View File

@@ -0,0 +1,114 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package integration;
import com.github.kristofa.brave.SpanCollector;
import com.twitter.zipkin.gen.BinaryAnnotation;
import com.twitter.zipkin.gen.Span;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.JdkIdGenerator;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.testcontainers.containers.DockerComposeContainer;
import sample.SampleMessagingApplication;
import tools.*;
import java.io.File;
import java.util.Collection;
import static org.assertj.core.api.BDDAssertions.then;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { MessagingApplicationDockerTests.Config.class, SampleMessagingApplication.class })
@WebIntegrationTest
@Slf4j
@Ignore("Ignored until working on profile")
public class MessagingApplicationDockerTests extends AbstractIntegrationTest {
private static int port = 3381;
private static String sampleAppUrl = "http://localhost:" + port;
RestTemplate restTemplate = new AssertingRestTemplate();
@Autowired IntegrationTestSpanCollector integrationTestSpanCollector;
@ClassRule
public static DockerComposeContainer environment =
new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"))
.withExposedService("rabbitmq_1", 5672);
@After
public void cleanup() {
integrationTestSpanCollector.hashedSpans.clear();
}
@Test
public void should_propagate_spans_for_messaging() {
String traceId = new JdkIdGenerator().generateId().toString();
await().until(httpMessageWithTraceIdInHeadersIsSuccessfullySent(sampleAppUrl + "/", traceId));
await().until(() -> {
thenAllSpansHaveTraceIdEqualTo(traceId);
});
}
@Test
public void should_propagate_spans_for_messaging_with_async() {
String traceId = new JdkIdGenerator().generateId().toString();
await().until(httpMessageWithTraceIdInHeadersIsSuccessfullySent(sampleAppUrl + "/xform", traceId));
await().until(() -> {
thenAllSpansHaveTraceIdEqualTo(traceId);
thenThereIsAtLeastOneBinaryAnnotationWithKey("background-sleep-millis");
});
}
private void thenThereIsAtLeastOneBinaryAnnotationWithKey(String binaryAnnotationKey) {
then(integrationTestSpanCollector.hashedSpans.stream()
.filter(Span::isSetBinary_annotations)
.map(Span::getBinary_annotations)
.flatMap(Collection::stream)
.filter(binaryAnnotation -> StringUtils.hasText(binaryAnnotation.getKey()))
.map(BinaryAnnotation::getKey)
.anyMatch(binaryAnnotationKey::equals)).isTrue();
}
private RequestSendingRunnable httpMessageWithTraceIdInHeadersIsSuccessfullySent(String endpoint, String traceId) {
return new RequestSendingRunnable(restTemplate, endpoint, traceId);
}
private void thenAllSpansHaveTraceIdEqualTo(String traceId) {
then(integrationTestSpanCollector.hashedSpans.stream().allMatch(span -> span.getTrace_id() == zipkinHashedTraceId(traceId))).isTrue();
}
@Configuration
static class Config {
@Bean SpanCollector integrationTestSpanCollector() {
return new IntegrationTestSpanCollector();
}
}
}

View File

@@ -0,0 +1,5 @@
rabbitmq:
image: rabbitmq:management
ports:
- 5672:5672
- 15672:15672

View File

@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-sleuth-sample-test-core</artifactId>
<packaging>jar</packaging>
<name>Spring Cloud Sleuth Sample Test Core</name>
<description>Test classes for Spring Cloud Samples</description>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-samples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<build>
<pluginManagement>
<plugins>
<plugin>
<!--skip deploy (this is just a test module) -->
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<testcontainers.jackson.version>2.1.0</testcontainers.jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>docker-compose</artifactId>
<version>0.9.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${testcontainers.jackson.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${testcontainers.jackson.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${testcontainers.jackson.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tools;
import com.jayway.awaitility.Awaitility;
import com.jayway.awaitility.core.ConditionFactory;
import org.junit.experimental.categories.Category;
import org.springframework.beans.factory.annotation.Value;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* @author Marcin Grzejszczak
*/
@Category(DockerTests.class)
abstract public class AbstractIntegrationTest {
@Value("${test.pollinterval:1}") protected int pollInterval;
@Value("${test.timeout:1}") protected int timeout;
protected ConditionFactory await() {
return Awaitility.await().pollInterval(pollInterval, SECONDS).atMost(timeout, SECONDS);
}
protected long zipkinHashedTraceId(String string) {
long h = 1125899906842597L;
if (string == null) {
return h;
}
int len = string.length();
for (int i = 0; i < len; i++) {
h = 31 * h + string.charAt(i);
}
return h;
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.*;
import java.io.IOException;
import java.net.URI;
/**
*
* RestTemplate that logs erroneous responses and throws AssertionsError on any connection issues
*
* @author Marcin Grzejszczak
*/
@Slf4j
public class AssertingRestTemplate extends RestTemplate {
public AssertingRestTemplate() {
setErrorHandler(new DefaultResponseErrorHandler() {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
if (hasError(response)) {
log.error("Response has status code [" + response.getStatusCode() + "] and text [" + response.getStatusText() + "])");
}
}
});
}
@Override
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws RestClientException {
try {
return super.doExecute(url, method, requestCallback, responseExtractor);
} catch (Exception e) {
log.error("Exception occurred while sending the message", e);
throw new AssertionError(e);
}
}
}

View File

@@ -0,0 +1,7 @@
package tools;
/**
* @author Marcin Grzejszczak
*/
interface DockerTests {
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tools;
import com.github.kristofa.brave.LoggingSpanCollector;
import com.twitter.zipkin.gen.Span;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* Span Collector that logs spans and adds Spans to a list
*
* @author Marcin Grzejszczak
*/
public class IntegrationTestSpanCollector extends LoggingSpanCollector {
public List<Span> hashedSpans = Collections.<Span>synchronizedList(new LinkedList<Span>());
@Override
public void collect(Span span) {
super.collect(span);
hashedSpans.add(span);
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.sleuth.Trace;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import static org.assertj.core.api.BDDAssertions.then;
/**
* Runnable that will send a request via the provide rest template to the
* given url. It will also append the provided TraceID as the request's header
*
* @author Marcin Grzejszczak
*/
@Slf4j
public class RequestSendingRunnable implements Runnable {
private final RestTemplate restTemplate;
private final String url;
private final String traceId;
public RequestSendingRunnable(RestTemplate restTemplate, String url, String traceId) {
this.restTemplate = restTemplate;
this.url = url;
this.traceId = traceId;
}
@Override
public void run() {
ResponseEntity<String> responseEntity = restTemplate.exchange(requestWithTraceId(traceId), String.class);
then(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
log.info("Received the following response [{}]", responseEntity);
}
private RequestEntity requestWithTraceId(String traceId) {
HttpHeaders headers = new HttpHeaders();
headers.add(Trace.TRACE_ID_NAME, traceId);
URI uri = URI.create(url);
RequestEntity requestEntity = new RequestEntity<>(headers, HttpMethod.GET, uri);
log.info("Request [" + requestEntity + "] is ready");
return requestEntity;
}
}

View File

@@ -19,6 +19,9 @@ package org.springframework.cloud.sleuth.zipkin.stream;
import io.zipkin.BinaryAnnotation;
import io.zipkin.Endpoint;
import java.util.Collections;
import java.util.Comparator;
import java.util.Objects;
import org.junit.Test;
import org.springframework.cloud.sleuth.MilliSpan;
import org.springframework.cloud.sleuth.stream.Host;
@@ -70,8 +73,9 @@ public class ZipkinMessageListenerTests {
public void spanWithoutAnnotationsLogsComponent() {
io.zipkin.Span result = ZipkinMessageListener.convert(span, host);
assertThat(result.binaryAnnotations)
.containsOnly(BinaryAnnotation.create("lc", span.getProcessId(), endpoint));
assertThat(result.binaryAnnotations).hasSize(1);
assertThat(result.binaryAnnotations.get(0)).isEqualToComparingFieldByField(
BinaryAnnotation.create("lc", span.getProcessId().toLowerCase(), endpoint));
}
// TODO: "unknown" bc process id, documented as not nullable, is null in some tests.

View File

@@ -127,8 +127,7 @@ public class ZipkinSpanListenerTests {
Trace context = this.traceManager.startSpan("foo");
this.traceManager.close(context);
assertEquals(1, this.test.spans.size());
assertThat(this.test.spans.get(0).getBinary_annotations())
.extracting("host.service_name")
assertThat(this.test.spans.get(0).getBinary_annotations().get(0).getHost().getService_name())
.isEqualTo("unknown"); // TODO: "unknown" bc process id, documented as not nullable, is null.
}