Skip to content

Commit

Permalink
try to fix flaky test (#4733)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8255a9b)
  • Loading branch information
olavloite authored and sduskis committed Mar 25, 2019
1 parent 554c109 commit bab445b
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import static com.google.cloud.pubsub.v1.OpenCensusUtil.TRACE_CONTEXT_KEY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;

import com.google.common.base.Stopwatch;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import io.opencensus.common.Scope;
Expand All @@ -40,6 +42,7 @@
import io.opencensus.trace.export.SpanData;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.Test;

/** Tests for {@link OpenCensusUtil}. */
Expand Down Expand Up @@ -123,8 +126,8 @@ private TestMessageReceiver(SpanContext parentLinkedSpan, TagContext originalTag
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
assertEquals(originalTagContext, tagger.getCurrentTagContext());
assertSpanCount(1);
Collection<SpanData> spanDatas = runningSpanStore.getRunningSpans(RECEIVER_FILTER);
assertEquals(spanDatas.size(), 1);
for (SpanData spanData : spanDatas) {
List<Link> links = spanData.getLinks().getLinks();
assertEquals(links.size(), 1);
Expand All @@ -135,5 +138,19 @@ public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
}
consumer.ack();
}

private void assertSpanCount(int expected) {
Stopwatch watch = Stopwatch.createStarted();
while (true) {
Collection<SpanData> spanDatas = runningSpanStore.getRunningSpans(RECEIVER_FILTER);
if (spanDatas.size() == 1) {
break;
}
Thread.yield();
if (watch.elapsed(TimeUnit.SECONDS) >= 5) {
fail();
}
}
}
}
}

0 comments on commit bab445b

Please sign in to comment.