[#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

@@ -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