Working Zipkin Stream tests

This commit is contained in:
Marcin Grzejszczak
2016-01-12 15:30:23 +01:00
parent e9f307a0be
commit 091a4fc218
15 changed files with 597 additions and 261 deletions

View File

@@ -15,7 +15,6 @@
*/
package integration;
import io.zipkin.Codec;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
@@ -24,28 +23,21 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.JdkIdGenerator;
import org.testcontainers.containers.DockerComposeContainer;
import sample.SampleZipkinApplication;
import tools.AbstractIntegrationTest;
import tools.RequestSendingRunnable;
import tools.AbstractDockerIntegrationTest;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
import static org.assertj.core.api.BDDAssertions.then;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { AbstractIntegrationTest.ZipkinConfig.class, SampleZipkinApplication.class })
@SpringApplicationConfiguration(classes = { AbstractDockerIntegrationTest.ZipkinConfig.class, SampleZipkinApplication.class })
@WebIntegrationTest
@TestPropertySource(properties="sample.zipkin.enabled=true")
@Slf4j
public class ZipkinDockerTests extends AbstractIntegrationTest {
public class ZipkinDockerTests extends AbstractDockerIntegrationTest {
private static final String APP_NAME = "testsleuthzipkin";
private static int port = 3380;
@@ -68,65 +60,13 @@ public class ZipkinDockerTests extends AbstractIntegrationTest {
public void should_propagate_spans_to_zipkin() {
String traceId = new JdkIdGenerator().generateId().toString();
httpMessageWithTraceIdInHeadersIsSuccessfullySent(sampleAppUrl + "/hi2", traceId);
await().until(httpMessageWithTraceIdInHeadersIsSuccessfullySent(sampleAppUrl + "/hi2", traceId));
await().until(() -> {
allSpansWereRegisteredInZipkinWithTraceIdEqualTo(traceId);
});
await().until(allSpansWereRegisteredInZipkinWithTraceIdEqualTo(traceId));
}
private void allSpansWereRegisteredInZipkinWithTraceIdEqualTo(String traceId) {
ResponseEntity<String> response = checkStateOfTheTraceId(traceId);
log.info("Response from the Zipkin query service about the trace id [{}] for trace with id [{}]", response, traceId);
then(response.getStatusCode()).isEqualTo(HttpStatus.OK);
then(response.hasBody()).isTrue();
List<io.zipkin.Span> spans = Codec.JSON.readSpans(response.getBody().getBytes());
List<String> serviceNamesNotFoundInZipkin = serviceNamesNotFoundInZipkin(spans);
List<String> spanNamesNotFoundInZipkin = annotationsNotFoundInZipkin(spans);
log.info("The following services were not found in Zipkin {}", serviceNamesNotFoundInZipkin);
log.info("The following spans were not found in Zipkin {}", spanNamesNotFoundInZipkin);
then(serviceNamesNotFoundInZipkin).isEmpty();
then(spanNamesNotFoundInZipkin).isEmpty();
log.info("Zipkin tracing is working! Sleuth is working! Let's be happy!");
@Override
protected String getAppName() {
return APP_NAME;
}
private List<String> serviceNamesNotFoundInZipkin(List<io.zipkin.Span> spans) {
List<String> serviceNamesFoundInAnnotations = spans.stream()
.map(span -> span.annotations)
.flatMap(Collection::stream)
.filter(span -> span.endpoint != null)
.map(annotation -> annotation.endpoint)
.map(endpoint -> endpoint.serviceName)
.distinct()
.collect(Collectors.toList());
List<String> serviceNamesFoundInBinaryAnnotations = spans.stream()
.map(span -> span.binaryAnnotations)
.flatMap(Collection::stream)
.filter(span -> span.endpoint != null)
.map(annotation -> annotation.endpoint)
.map(endpoint -> endpoint.serviceName)
.distinct()
.collect(Collectors.toList());
List<String> names = new ArrayList<>();
names.addAll(serviceNamesFoundInAnnotations);
names.addAll(serviceNamesFoundInBinaryAnnotations);
return names.contains(APP_NAME) ? Collections.EMPTY_LIST : names;
}
private List<String> annotationsNotFoundInZipkin(List<io.zipkin.Span> spans) {
String binaryAnnotationName = "random-sleep-millis";
Optional<String> names = spans.stream()
.map(span -> span.binaryAnnotations)
.flatMap(Collection::stream)
.filter(span -> span.endpoint != null)
.map(annotation -> annotation.key)
.filter(binaryAnnotationName::equals)
.findFirst();
return names.isPresent() ? Collections.EMPTY_LIST : Collections.singletonList(binaryAnnotationName);
}
private void httpMessageWithTraceIdInHeadersIsSuccessfullySent(String endpoint, String traceId) {
new RequestSendingRunnable(restTemplate, endpoint, traceId).run();
}
}