Fighting flakey tests

This commit is contained in:
Marcin Grzejszczak
2017-11-17 12:02:05 +01:00
parent 85517c6670
commit 2ecd77d151
4 changed files with 12 additions and 23 deletions

View File

@@ -18,10 +18,11 @@ package org.springframework.cloud.sleuth.instrument.messaging;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.assertj.core.api.BDDAssertions;
import org.awaitility.Awaitility;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -101,11 +102,8 @@ public class TraceChannelInterceptorTests implements MessageHandler {
private Span span;
private CountDownLatch latch = new CountDownLatch(1);
@Override
public void handleMessage(Message<?> message) throws MessagingException {
this.latch.countDown();
this.message = message;
this.span = TestSpanContextHolder.getCurrentSpan();
if (message.getHeaders().containsKey("THROW_EXCEPTION")) {
@@ -147,8 +145,8 @@ public class TraceChannelInterceptorTests implements MessageHandler {
public void executableSpanCreation() throws Exception {
this.executorChannel.send(MessageBuilder.withPayload("hi")
.setHeader(TraceMessageHeaders.SAMPLED_NAME, Span.SPAN_NOT_SAMPLED).build());
this.latch.await(1, TimeUnit.SECONDS);
assertNotNull("message was null", this.message);
Awaitility.await()
.untilAsserted(() -> BDDAssertions.assertThat(this.message).isNotNull());
String spanId = this.message.getHeaders().get(TraceMessageHeaders.SPAN_ID_NAME, String.class);
then(spanId).isNotNull();

View File

@@ -113,7 +113,8 @@ public class MessagingApplicationTests extends AbstractIntegrationTest {
.collect(Collectors.joining("\n")) + "\n]");
then(this.integrationTestSpanCollector.hashedSpans
.stream()
.filter(span -> !span.traceId().equals(traceIdHex))
.filter(span ->
org.springframework.cloud.sleuth.Span.hexToId(span.traceId()) != traceId)
.collect(Collectors.toList()))
.describedAs("All spans have same trace id [" + traceIdHex + "]")
.isEmpty();

View File

@@ -50,7 +50,7 @@ public abstract class AbstractIntegrationTest {
protected Runnable httpMessageWithTraceIdInHeadersIsSuccessfullySent(String endpoint, long traceId) {
return new RequestSendingRunnable(this.restTemplate, endpoint, traceId, null);
return new RequestSendingRunnable(this.restTemplate, endpoint, traceId, traceId);
}
protected Runnable httpMessageWithTraceIdInHeadersIsSuccessfullySent(String endpoint, long traceId, Long spanId) {

View File

@@ -15,22 +15,18 @@
*/
package integration;
import integration.ZipkinTests.WaitUntilZipkinIsUpConfig;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;
import integration.ZipkinTests.WaitUntilZipkinIsUpConfig;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import sample.SampleZipkinApplication;
import tools.AbstractIntegrationTest;
import java.net.URI;
import java.util.Random;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -43,6 +39,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sample.SampleZipkinApplication;
import tools.AbstractIntegrationTest;
import zipkin2.Span;
import zipkin2.codec.SpanBytesDecoder;
@@ -62,14 +60,6 @@ public class ZipkinTests extends AbstractIntegrationTest {
private String sampleAppUrl = "http://localhost:" + this.port;
@Autowired ZipkinProperties zipkinProperties;
int getZipkinServerPort() {
return getPortFromProps();
}
private int getPortFromProps() {
return URI.create(this.zipkinProperties.getBaseUrl()).getPort();
}
@Test
public void should_propagate_spans_to_zipkin() throws Exception {
zipkin.enqueue(new MockResponse());