Skip to content

Commit

Permalink
Formatting and polish
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Cole committed Jan 9, 2017
1 parent d49a9a1 commit 6e998f7
Showing 1 changed file with 72 additions and 55 deletions.
127 changes: 72 additions & 55 deletions zipkin/src/test/java/zipkin/internal/CorrectForClockSkewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
package zipkin.internal;

import java.net.Inet6Address;
import java.net.UnknownHostException;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Test;
import zipkin.Annotation;
import zipkin.BinaryAnnotation;
Expand All @@ -21,15 +25,13 @@
import zipkin.Span;
import zipkin.TestObjects;

import java.util.Arrays;
import java.util.List;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static zipkin.Constants.CLIENT_RECV;
import static zipkin.Constants.CLIENT_SEND;
import static zipkin.Constants.LOCAL_COMPONENT;
import static zipkin.Constants.SERVER_RECV;
import static zipkin.Constants.SERVER_SEND;
import static zipkin.TestObjects.APP_ENDPOINT;
Expand All @@ -39,8 +41,8 @@
import static zipkin.internal.CorrectForClockSkew.isLocalSpan;

public class CorrectForClockSkewTest {
private static final long networkLatency = 10L;
private static final long now = System.currentTimeMillis();
static final long networkLatency = 10L;
static final long now = System.currentTimeMillis();

Endpoint ipv6 = Endpoint.builder()
.serviceName("web")
Expand Down Expand Up @@ -73,9 +75,9 @@ public void ipsMatch_falseWhenIpv4Different() {
}

@Test
public void ipsMatch_falseWhenIpv6Different() {
public void ipsMatch_falseWhenIpv6Different() throws UnknownHostException {
Endpoint different = ipv6.toBuilder()
.ipv6(sun.net.util.IPAddressUtil.textToNumericFormatV6("2001:db8::c002")).build();
.ipv6(Inet6Address.getByName("2001:db8::c002").getAddress()).build();
assertFalse(ipsMatch(different, ipv6));
assertFalse(ipsMatch(ipv6, different));
}
Expand All @@ -101,7 +103,7 @@ public void spanWithSameEndPointIsLocalSpan() {

@Test
public void spanWithLCAnnotationIsLocalSpan() {
Span localSpan = createLocalSpan(TestObjects.TRACE.get(0), WEB_ENDPOINT, 0, 0);
Span localSpan = localSpan(TestObjects.TRACE.get(0), WEB_ENDPOINT, 0, 0);
assertTrue(isLocalSpan(localSpan));
}

Expand All @@ -120,20 +122,22 @@ public void clockSkewIsPropagatedToLocalSpans() {
long networkLatency = 10L;
Span rootSpan = createRootSpan(WEB_ENDPOINT, now, 2000L);
long skew = -50000L;
Span rpcSpan = createChildSpan(rootSpan, WEB_ENDPOINT, APP_ENDPOINT, now + networkLatency, 1000L, skew);
Span localSpan = createLocalSpan(rpcSpan, APP_ENDPOINT, rpcSpan.timestamp + 5, 200L);
Span embeddedLocalSpan = createLocalSpan(localSpan, APP_ENDPOINT, localSpan.timestamp + 10, 100L);
Span rpcSpan = childSpan(rootSpan, APP_ENDPOINT, now + networkLatency, 1000L, skew);
Span local = localSpan(rpcSpan, APP_ENDPOINT, rpcSpan.timestamp + 5, 200L);
Span local2 = localSpan(local, APP_ENDPOINT, local.timestamp + 10, 100L);

List<Span> adjustedSpans = CorrectForClockSkew.apply(Arrays.asList(rpcSpan, rootSpan, localSpan, embeddedLocalSpan));
List<Span> adjustedSpans = CorrectForClockSkew.apply(asList(rpcSpan, rootSpan, local, local2));

Span adjustedLocalSpan = adjustedSpans.stream().filter(s -> s.id == localSpan.id).findFirst().get();
assertEquals(localSpan.timestamp - skew, adjustedLocalSpan.timestamp.longValue());
Span adjustedLocal = getById(adjustedSpans, local.id);
assertThat(local.timestamp - skew)
.isEqualTo(adjustedLocal.timestamp.longValue());

Span adjustedEmbeddedLocalSpan = adjustedSpans.stream().filter(s -> s.id == embeddedLocalSpan.id).findFirst().get();
assertEquals(embeddedLocalSpan.timestamp - skew, adjustedEmbeddedLocalSpan.timestamp.longValue());
Span adjustedLocal2 = getById(adjustedSpans, local2.id);
assertThat(local2.timestamp - skew)
.isEqualTo(adjustedLocal2.timestamp.longValue());
}

private static void assertClockSkewIsCorrectlyApplied(long skew) {
static void assertClockSkewIsCorrectlyApplied(long skew) {
long rpcClientSendTs = now + 50L;
long dbClientSendTimestamp = now + 60 + skew;

Expand All @@ -142,51 +146,64 @@ private static void assertClockSkewIsCorrectlyApplied(long skew) {
long dbDuration = 40L;

Span rootSpan = createRootSpan(WEB_ENDPOINT, now, rootDuration);
Span rpcSpan = createChildSpan(rootSpan, WEB_ENDPOINT, APP_ENDPOINT, rpcClientSendTs, rpcDuration, skew);
Span tierSpan = createChildSpan(rpcSpan, APP_ENDPOINT, DB_ENDPOINT, dbClientSendTimestamp, dbDuration, skew);
Span rpcSpan = childSpan(rootSpan, APP_ENDPOINT, rpcClientSendTs, rpcDuration, skew);
Span tierSpan = childSpan(rpcSpan, DB_ENDPOINT, dbClientSendTimestamp, dbDuration, skew);

List<Span> adjustedSpans = CorrectForClockSkew.apply(Arrays.asList(rpcSpan, rootSpan, tierSpan));
List<Span> adjustedSpans = CorrectForClockSkew.apply(asList(rpcSpan, rootSpan, tierSpan));

Span adjustedRpcSpan = adjustedSpans.stream().filter(s -> s.id == rpcSpan.id).findFirst().get();
assertAnnotationTimestampEquals(rpcClientSendTs + networkLatency, adjustedRpcSpan, Constants.SERVER_RECV);
assertAnnotationTimestampEquals(adjustedRpcSpan.timestamp, adjustedRpcSpan, Constants.CLIENT_SEND);
long id = rpcSpan.id;
Span adjustedRpcSpan = getById(adjustedSpans, id);
assertThat(annotationTimestamps(adjustedRpcSpan, Constants.SERVER_RECV))
.containsExactly(rpcClientSendTs + networkLatency);

Span adjustedTierSpan = adjustedSpans.stream().filter(s -> s.id == tierSpan.id).findFirst().get();
assertAnnotationTimestampEquals(adjustedTierSpan.timestamp, adjustedTierSpan, Constants.CLIENT_SEND);
assertThat(annotationTimestamps(adjustedRpcSpan, Constants.CLIENT_SEND))
.containsExactly(adjustedRpcSpan.timestamp);

Span adjustedTierSpan =
getById(adjustedSpans, tierSpan.id);

assertThat(annotationTimestamps(adjustedTierSpan, Constants.CLIENT_SEND))
.containsExactly(adjustedTierSpan.timestamp);
}

private static Span createRootSpan(Endpoint endPoint, long beginTs, long duration) {
static Span createRootSpan(Endpoint endPoint, long begin, long duration) {
return Span.builder()
.traceId(1L).id(1L).name("root").timestamp(beginTs)
.addAnnotation(Annotation.create(beginTs, SERVER_RECV, endPoint))
.addAnnotation(Annotation.create(beginTs + duration, SERVER_SEND, endPoint))
.build();
.traceId(1L).id(1L).name("root").timestamp(begin)
.addAnnotation(Annotation.create(begin, SERVER_RECV, endPoint))
.addAnnotation(Annotation.create(begin + duration, SERVER_SEND, endPoint))
.build();
}
private static Span createChildSpan(Span parentSpan, Endpoint from, Endpoint to, long beginTs, long duration, long skew) {
long spanId = parentSpan.id + 1;

static Span childSpan(Span parent, Endpoint to, long begin, long duration, long skew) {
long spanId = parent.id + 1;
Endpoint from = parent.annotations.get(0).endpoint;
long networkLatency = 10L;
return Span.builder()
.traceId(parentSpan.traceId).id(spanId).parentId(parentSpan.id).name("span" + spanId).timestamp(beginTs)
.addAnnotation(Annotation.create(beginTs, CLIENT_SEND, from))
.addAnnotation(Annotation.create(beginTs + skew + networkLatency, SERVER_RECV, to))
.addAnnotation(Annotation.create(beginTs + skew + duration - networkLatency, SERVER_SEND, to))
.addAnnotation(Annotation.create(beginTs + duration, CLIENT_RECV, from))
.build();
}

private static Span createLocalSpan(Span parentSpan, Endpoint endPoint, long beginTs, long duration) {
long spanId = parentSpan.id + 1;
return Span.builder().traceId(parentSpan.traceId).id(spanId).parentId(parentSpan.id).name("localcomponent" + spanId)
.timestamp(beginTs).duration(duration)
.addBinaryAnnotation(BinaryAnnotation.create(Constants.LOCAL_COMPONENT, "localComponent" + spanId, endPoint))
.build();
}

private static void assertAnnotationTimestampEquals(long expectedTimestamp, Span span, String annotation) {
assertThat(span.annotations)
.filteredOn(a -> a.value.equals(annotation))
.extracting(a -> a.timestamp)
.first()
.isEqualTo(expectedTimestamp);
.traceId(parent.traceId).id(spanId).parentId(parent.id)
.name("span" + spanId).timestamp(begin)
.addAnnotation(Annotation.create(begin, CLIENT_SEND, from))
.addAnnotation(Annotation.create(begin + skew + networkLatency, SERVER_RECV, to))
.addAnnotation(Annotation.create(begin + skew + duration - networkLatency, SERVER_SEND, to))
.addAnnotation(Annotation.create(begin + duration, CLIENT_RECV, from))
.build();
}

static Span localSpan(Span parent, Endpoint endpoint, long begin, long duration) {
long spanId = parent.id + 1;
return Span.builder().traceId(parent.traceId).parentId(parent.id).id(spanId)
.name("lc" + spanId)
.timestamp(begin).duration(duration)
.addBinaryAnnotation(BinaryAnnotation.create(LOCAL_COMPONENT, "lc" + spanId, endpoint))
.build();
}

static Stream<Long> annotationTimestamps(Span span, String annotation) {
return span.annotations.stream()
.filter(a -> a.value.equals(annotation))
.map(a -> a.timestamp);
}

static Span getById(List<Span> adjustedSpans, long id) {
return adjustedSpans.stream().filter(s -> s.id == id).findFirst().get();
}
}

0 comments on commit 6e998f7

Please sign in to comment.