[#128] Added an integration test

This commit is contained in:
Marcin Grzejszczak
2016-01-22 13:24:42 +01:00
parent 301c6b230a
commit 8258289550
6 changed files with 90 additions and 19 deletions

View File

@@ -60,6 +60,11 @@ public class SampleMessagingApplication {
return msg;
}
@RequestMapping("/foo")
public String foo() {
return "foo";
}
@RequestMapping("/xform")
public String xform() {
String msg = "Hello";

View File

@@ -18,9 +18,13 @@ package sample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.web.client.RestTemplate;
/**
* @author Dave Syer
@@ -28,11 +32,19 @@ import org.springframework.messaging.Message;
*/
@MessageEndpoint
@Slf4j
public class SampleService {
public class SampleService implements
ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@Autowired private RestTemplate restTemplate;
private int port;
@ServiceActivator(inputChannel="messages")
public void log(Message<?> message) {
log.info("Received: " + message);
this.restTemplate.getForObject("http://localhost:" + this.port + "/foo", String.class);
}
@Override public void onApplicationEvent(
EmbeddedServletContainerInitializedEvent event) {
this.port = event.getEmbeddedServletContainer().getPort();
}
}

View File

@@ -29,8 +29,10 @@ import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sample.SampleMessagingApplication;
import tools.AbstractIntegrationTest;
import zipkin.Span;
import java.util.Collection;
import java.util.Optional;
import java.util.Random;
import static org.assertj.core.api.BDDAssertions.then;
@@ -51,7 +53,7 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
}
@Test
public void should_propagate_spans_for_messaging() {
public void should_have_passed_trace_id_when_message_is_about_to_be_sent() {
long traceId = new Random().nextLong();
await().until(httpMessageWithTraceIdInHeadersIsSuccessfullySent(sampleAppUrl + "/", traceId));
@@ -62,7 +64,20 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
}
@Test
public void should_propagate_spans_for_messaging_with_async() {
public void should_have_passed_trace_id_and_generate_new_span_id_when_message_is_about_to_be_sent() {
long traceId = new Random().nextLong();
long spanId = new Random().nextLong();
await().until(httpMessageWithTraceIdInHeadersIsSuccessfullySent(sampleAppUrl + "/", traceId, spanId));
await().until(() -> {
thenAllSpansHaveTraceIdEqualTo(traceId);
thenTheLastSpansParentHasIdEqualToFirstSpansId();
});
}
@Test
public void should_have_passed_trace_id_with_annotations_in_async_thread_when_message_is_about_to_be_sent() {
long traceId = new Random().nextLong();
await().until(httpMessageWithTraceIdInHeadersIsSuccessfullySent(sampleAppUrl + "/xform", traceId));
@@ -84,6 +99,16 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
then(this.integrationTestSpanCollector.hashedSpans.stream().allMatch(span -> span.traceId == traceId)).isTrue();
}
private void thenTheLastSpansParentHasIdEqualToFirstSpansId() {
Optional<Span> firstSpan = this.integrationTestSpanCollector.hashedSpans.stream()
.filter(span -> "http/".equals(span.name) && span.parentId != null).findFirst();
Optional<Span> lastSpan = this.integrationTestSpanCollector.hashedSpans.stream()
.filter(span -> "http/foo".equals(span.name)).findFirst();
then(firstSpan.isPresent()).isTrue();
then(lastSpan.isPresent()).isTrue();
then(lastSpan.get().parentId).isEqualTo(firstSpan.get().id);
}
@Configuration
public static class IntegrationSpanCollectorConfig {
@Bean