diff options
author | Julien Desprez <jdesprez@google.com> | 2018-10-19 12:36:02 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2018-10-19 12:36:02 -0700 |
commit | 6fbc3cf5a1a3369fd354c1e5d9f90c86e4bce0a4 (patch) | |
tree | ede84fcf0a9687d4907ae5f8a4788271d62e0922 /api/src/test/java/io/opencensus/trace/export | |
parent | cfbefd32336596ea63784607e4106dc37ce0567f (diff) | |
parent | dd3cabeacc5c8079b0d8674230819f3c54dca590 (diff) | |
download | opencensus-java-6fbc3cf5a1a3369fd354c1e5d9f90c86e4bce0a4.tar.gz |
Merge remote-tracking branch 'aosp/upstream-master' into merge
am: dd3cabeacc
Change-Id: Ib77ae649a4106599080887b1b4aabd3b3e7cefa7
Diffstat (limited to 'api/src/test/java/io/opencensus/trace/export')
4 files changed, 516 insertions, 0 deletions
diff --git a/api/src/test/java/io/opencensus/trace/export/ExportComponentTest.java b/api/src/test/java/io/opencensus/trace/export/ExportComponentTest.java new file mode 100644 index 00000000..d7f385d0 --- /dev/null +++ b/api/src/test/java/io/opencensus/trace/export/ExportComponentTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.trace.export; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link ExportComponent}. */ +@RunWith(JUnit4.class) +public class ExportComponentTest { + private final ExportComponent exportComponent = ExportComponent.newNoopExportComponent(); + + @Test + public void implementationOfSpanExporter() { + assertThat(exportComponent.getSpanExporter()).isEqualTo(SpanExporter.getNoopSpanExporter()); + } + + @Test + public void implementationOfActiveSpans() { + assertThat(exportComponent.getRunningSpanStore()) + .isEqualTo(RunningSpanStore.getNoopRunningSpanStore()); + } + + @Test + public void implementationOfSampledSpanStore() { + assertThat(exportComponent.getSampledSpanStore()) + .isInstanceOf(SampledSpanStore.newNoopSampledSpanStore().getClass()); + } +} diff --git a/api/src/test/java/io/opencensus/trace/export/NoopRunningSpanStoreTest.java b/api/src/test/java/io/opencensus/trace/export/NoopRunningSpanStoreTest.java new file mode 100644 index 00000000..960da27c --- /dev/null +++ b/api/src/test/java/io/opencensus/trace/export/NoopRunningSpanStoreTest.java @@ -0,0 +1,55 @@ +/* + * Copyright 2017, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.trace.export; + +import static com.google.common.truth.Truth.assertThat; + +import java.util.Collection; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link NoopRunningSpanStore}. */ +@RunWith(JUnit4.class) +public final class NoopRunningSpanStoreTest { + + private final RunningSpanStore runningSpanStore = + ExportComponent.newNoopExportComponent().getRunningSpanStore(); + + @Rule public final ExpectedException thrown = ExpectedException.none(); + + @Test + public void noopRunningSpanStore_GetSummary() { + RunningSpanStore.Summary summary = runningSpanStore.getSummary(); + assertThat(summary.getPerSpanNameSummary()).isEmpty(); + } + + @Test + public void noopRunningSpanStore_GetRunningSpans_DisallowsNull() { + thrown.expect(NullPointerException.class); + runningSpanStore.getRunningSpans(null); + } + + @Test + public void noopRunningSpanStore_GetRunningSpans() { + Collection<SpanData> runningSpans = + runningSpanStore.getRunningSpans(RunningSpanStore.Filter.create("TestSpan", 0)); + assertThat(runningSpans).isEmpty(); + } +} diff --git a/api/src/test/java/io/opencensus/trace/export/NoopSampledSpanStoreTest.java b/api/src/test/java/io/opencensus/trace/export/NoopSampledSpanStoreTest.java new file mode 100644 index 00000000..6e9c7b0f --- /dev/null +++ b/api/src/test/java/io/opencensus/trace/export/NoopSampledSpanStoreTest.java @@ -0,0 +1,94 @@ +/* + * Copyright 2017, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.trace.export; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.collect.Lists; +import io.opencensus.trace.Status.CanonicalCode; +import java.util.Collection; +import java.util.Collections; +import java.util.Set; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link NoopSampledSpanStore}. */ +@RunWith(JUnit4.class) +public final class NoopSampledSpanStoreTest { + + private static final SampledSpanStore.PerSpanNameSummary EMPTY_PER_SPAN_NAME_SUMMARY = + SampledSpanStore.PerSpanNameSummary.create( + Collections.<SampledSpanStore.LatencyBucketBoundaries, Integer>emptyMap(), + Collections.<CanonicalCode, Integer>emptyMap()); + + @Test + public void noopSampledSpanStore_RegisterUnregisterAndGetSummary() { + // should return empty before register + SampledSpanStore sampledSpanStore = + ExportComponent.newNoopExportComponent().getSampledSpanStore(); + SampledSpanStore.Summary summary = sampledSpanStore.getSummary(); + assertThat(summary.getPerSpanNameSummary()).isEmpty(); + + // should return non-empty summaries with zero latency/error sampled spans after register + sampledSpanStore.registerSpanNamesForCollection( + Collections.unmodifiableList(Lists.newArrayList("TestSpan1", "TestSpan2", "TestSpan3"))); + summary = sampledSpanStore.getSummary(); + assertThat(summary.getPerSpanNameSummary()) + .containsExactly( + "TestSpan1", EMPTY_PER_SPAN_NAME_SUMMARY, + "TestSpan2", EMPTY_PER_SPAN_NAME_SUMMARY, + "TestSpan3", EMPTY_PER_SPAN_NAME_SUMMARY); + + // should unregister specific spanNames + sampledSpanStore.unregisterSpanNamesForCollection( + Collections.unmodifiableList(Lists.newArrayList("TestSpan1", "TestSpan3"))); + summary = sampledSpanStore.getSummary(); + assertThat(summary.getPerSpanNameSummary()) + .containsExactly("TestSpan2", EMPTY_PER_SPAN_NAME_SUMMARY); + } + + @Test + public void noopSampledSpanStore_GetLatencySampledSpans() { + SampledSpanStore sampledSpanStore = + ExportComponent.newNoopExportComponent().getSampledSpanStore(); + Collection<SpanData> latencySampledSpans = + sampledSpanStore.getLatencySampledSpans( + SampledSpanStore.LatencyFilter.create("TestLatencyFilter", 0, 0, 0)); + assertThat(latencySampledSpans).isEmpty(); + } + + @Test + public void noopSampledSpanStore_GetErrorSampledSpans() { + SampledSpanStore sampledSpanStore = + ExportComponent.newNoopExportComponent().getSampledSpanStore(); + Collection<SpanData> errorSampledSpans = + sampledSpanStore.getErrorSampledSpans( + SampledSpanStore.ErrorFilter.create("TestErrorFilter", null, 0)); + assertThat(errorSampledSpans).isEmpty(); + } + + @Test + public void noopSampledSpanStore_GetRegisteredSpanNamesForCollection() { + SampledSpanStore sampledSpanStore = + ExportComponent.newNoopExportComponent().getSampledSpanStore(); + sampledSpanStore.registerSpanNamesForCollection( + Collections.unmodifiableList(Lists.newArrayList("TestSpan3", "TestSpan4"))); + Set<String> registeredSpanNames = sampledSpanStore.getRegisteredSpanNamesForCollection(); + assertThat(registeredSpanNames).containsExactly("TestSpan3", "TestSpan4"); + } +} diff --git a/api/src/test/java/io/opencensus/trace/export/SpanDataTest.java b/api/src/test/java/io/opencensus/trace/export/SpanDataTest.java new file mode 100644 index 00000000..b991d145 --- /dev/null +++ b/api/src/test/java/io/opencensus/trace/export/SpanDataTest.java @@ -0,0 +1,321 @@ +/* + * Copyright 2017, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.trace.export; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.testing.EqualsTester; +import io.opencensus.common.Timestamp; +import io.opencensus.trace.Annotation; +import io.opencensus.trace.AttributeValue; +import io.opencensus.trace.Link; +import io.opencensus.trace.Link.Type; +import io.opencensus.trace.MessageEvent; +import io.opencensus.trace.NetworkEvent; +import io.opencensus.trace.Span.Kind; +import io.opencensus.trace.SpanContext; +import io.opencensus.trace.SpanId; +import io.opencensus.trace.Status; +import io.opencensus.trace.TraceId; +import io.opencensus.trace.TraceOptions; +import io.opencensus.trace.export.SpanData.Attributes; +import io.opencensus.trace.export.SpanData.Links; +import io.opencensus.trace.export.SpanData.TimedEvent; +import io.opencensus.trace.export.SpanData.TimedEvents; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link SpanData}. */ +@RunWith(JUnit4.class) +public class SpanDataTest { + private static final Timestamp startTimestamp = Timestamp.create(123, 456); + private static final Timestamp eventTimestamp1 = Timestamp.create(123, 457); + private static final Timestamp eventTimestamp2 = Timestamp.create(123, 458); + private static final Timestamp eventTimestamp3 = Timestamp.create(123, 459); + private static final Timestamp endTimestamp = Timestamp.create(123, 460); + private static final String SPAN_NAME = "MySpanName"; + private static final String ANNOTATION_TEXT = "MyAnnotationText"; + private static final Annotation annotation = Annotation.fromDescription(ANNOTATION_TEXT); + private static final NetworkEvent recvNetworkEvent = + NetworkEvent.builder(NetworkEvent.Type.RECV, 1).build(); + private static final NetworkEvent sentNetworkEvent = + NetworkEvent.builder(NetworkEvent.Type.SENT, 1).build(); + private static final MessageEvent recvMessageEvent = + MessageEvent.builder(MessageEvent.Type.RECEIVED, 1).build(); + private static final MessageEvent sentMessageEvent = + MessageEvent.builder(MessageEvent.Type.SENT, 1).build(); + private static final Status status = Status.DEADLINE_EXCEEDED.withDescription("TooSlow"); + private static final int CHILD_SPAN_COUNT = 13; + private final Random random = new Random(1234); + private final SpanContext spanContext = + SpanContext.create( + TraceId.generateRandomId(random), SpanId.generateRandomId(random), TraceOptions.DEFAULT); + private final SpanId parentSpanId = SpanId.generateRandomId(random); + private final Map<String, AttributeValue> attributesMap = new HashMap<String, AttributeValue>(); + private final List<TimedEvent<Annotation>> annotationsList = + new ArrayList<TimedEvent<Annotation>>(); + private final List<TimedEvent<NetworkEvent>> networkEventsList = + new ArrayList<SpanData.TimedEvent<NetworkEvent>>(); + private final List<TimedEvent<MessageEvent>> messageEventsList = + new ArrayList<SpanData.TimedEvent<MessageEvent>>(); + private final List<Link> linksList = new ArrayList<Link>(); + + private Attributes attributes; + private TimedEvents<Annotation> annotations; + private TimedEvents<NetworkEvent> networkEvents; + private TimedEvents<MessageEvent> messageEvents; + private Links links; + + @Before + public void setUp() { + attributesMap.put("MyAttributeKey1", AttributeValue.longAttributeValue(10)); + attributesMap.put("MyAttributeKey2", AttributeValue.booleanAttributeValue(true)); + attributes = Attributes.create(attributesMap, 1); + annotationsList.add(SpanData.TimedEvent.create(eventTimestamp1, annotation)); + annotationsList.add(SpanData.TimedEvent.create(eventTimestamp3, annotation)); + annotations = TimedEvents.create(annotationsList, 2); + networkEventsList.add(SpanData.TimedEvent.create(eventTimestamp1, recvNetworkEvent)); + networkEventsList.add(SpanData.TimedEvent.create(eventTimestamp2, sentNetworkEvent)); + networkEvents = TimedEvents.create(networkEventsList, 3); + messageEventsList.add(SpanData.TimedEvent.create(eventTimestamp1, recvMessageEvent)); + messageEventsList.add(SpanData.TimedEvent.create(eventTimestamp2, sentMessageEvent)); + messageEvents = TimedEvents.create(messageEventsList, 3); + linksList.add(Link.fromSpanContext(spanContext, Type.CHILD_LINKED_SPAN)); + links = Links.create(linksList, 0); + } + + @Test + public void spanData_AllValues() { + SpanData spanData = + SpanData.create( + spanContext, + parentSpanId, + true, + SPAN_NAME, + Kind.SERVER, + startTimestamp, + attributes, + annotations, + messageEvents, + links, + CHILD_SPAN_COUNT, + status, + endTimestamp); + assertThat(spanData.getContext()).isEqualTo(spanContext); + assertThat(spanData.getParentSpanId()).isEqualTo(parentSpanId); + assertThat(spanData.getHasRemoteParent()).isTrue(); + assertThat(spanData.getName()).isEqualTo(SPAN_NAME); + assertThat(spanData.getKind()).isEqualTo(Kind.SERVER); + assertThat(spanData.getStartTimestamp()).isEqualTo(startTimestamp); + assertThat(spanData.getAttributes()).isEqualTo(attributes); + assertThat(spanData.getAnnotations()).isEqualTo(annotations); + assertThat(spanData.getNetworkEvents()).isEqualTo(networkEvents); + assertThat(spanData.getMessageEvents()).isEqualTo(messageEvents); + assertThat(spanData.getLinks()).isEqualTo(links); + assertThat(spanData.getChildSpanCount()).isEqualTo(CHILD_SPAN_COUNT); + assertThat(spanData.getStatus()).isEqualTo(status); + assertThat(spanData.getEndTimestamp()).isEqualTo(endTimestamp); + } + + @Test + public void spanData_Create_Compatibility() { + SpanData spanData = + SpanData.create( + spanContext, + parentSpanId, + true, + SPAN_NAME, + null, + startTimestamp, + attributes, + annotations, + networkEvents, + links, + CHILD_SPAN_COUNT, + status, + endTimestamp); + assertThat(spanData.getContext()).isEqualTo(spanContext); + assertThat(spanData.getParentSpanId()).isEqualTo(parentSpanId); + assertThat(spanData.getHasRemoteParent()).isTrue(); + assertThat(spanData.getName()).isEqualTo(SPAN_NAME); + assertThat(spanData.getStartTimestamp()).isEqualTo(startTimestamp); + assertThat(spanData.getAttributes()).isEqualTo(attributes); + assertThat(spanData.getAnnotations()).isEqualTo(annotations); + assertThat(spanData.getNetworkEvents()).isEqualTo(networkEvents); + assertThat(spanData.getMessageEvents()).isEqualTo(messageEvents); + assertThat(spanData.getLinks()).isEqualTo(links); + assertThat(spanData.getChildSpanCount()).isEqualTo(CHILD_SPAN_COUNT); + assertThat(spanData.getStatus()).isEqualTo(status); + assertThat(spanData.getEndTimestamp()).isEqualTo(endTimestamp); + } + + @Test + public void spanData_RootActiveSpan() { + SpanData spanData = + SpanData.create( + spanContext, + null, + null, + SPAN_NAME, + null, + startTimestamp, + attributes, + annotations, + messageEvents, + links, + null, + null, + null); + assertThat(spanData.getContext()).isEqualTo(spanContext); + assertThat(spanData.getParentSpanId()).isNull(); + assertThat(spanData.getHasRemoteParent()).isNull(); + assertThat(spanData.getName()).isEqualTo(SPAN_NAME); + assertThat(spanData.getStartTimestamp()).isEqualTo(startTimestamp); + assertThat(spanData.getAttributes()).isEqualTo(attributes); + assertThat(spanData.getAnnotations()).isEqualTo(annotations); + assertThat(spanData.getNetworkEvents()).isEqualTo(networkEvents); + assertThat(spanData.getMessageEvents()).isEqualTo(messageEvents); + assertThat(spanData.getLinks()).isEqualTo(links); + assertThat(spanData.getChildSpanCount()).isNull(); + assertThat(spanData.getStatus()).isNull(); + assertThat(spanData.getEndTimestamp()).isNull(); + } + + @Test + public void spanData_AllDataEmpty() { + SpanData spanData = + SpanData.create( + spanContext, + parentSpanId, + false, + SPAN_NAME, + null, + startTimestamp, + Attributes.create(Collections.<String, AttributeValue>emptyMap(), 0), + TimedEvents.create(Collections.<SpanData.TimedEvent<Annotation>>emptyList(), 0), + TimedEvents.create(Collections.<SpanData.TimedEvent<MessageEvent>>emptyList(), 0), + Links.create(Collections.<Link>emptyList(), 0), + 0, + status, + endTimestamp); + assertThat(spanData.getContext()).isEqualTo(spanContext); + assertThat(spanData.getParentSpanId()).isEqualTo(parentSpanId); + assertThat(spanData.getHasRemoteParent()).isFalse(); + assertThat(spanData.getName()).isEqualTo(SPAN_NAME); + assertThat(spanData.getStartTimestamp()).isEqualTo(startTimestamp); + assertThat(spanData.getAttributes().getAttributeMap().isEmpty()).isTrue(); + assertThat(spanData.getAnnotations().getEvents().isEmpty()).isTrue(); + assertThat(spanData.getNetworkEvents().getEvents().isEmpty()).isTrue(); + assertThat(spanData.getMessageEvents().getEvents().isEmpty()).isTrue(); + assertThat(spanData.getLinks().getLinks().isEmpty()).isTrue(); + assertThat(spanData.getChildSpanCount()).isEqualTo(0); + assertThat(spanData.getStatus()).isEqualTo(status); + assertThat(spanData.getEndTimestamp()).isEqualTo(endTimestamp); + } + + @Test + public void spanDataEquals() { + SpanData allSpanData1 = + SpanData.create( + spanContext, + parentSpanId, + false, + SPAN_NAME, + Kind.CLIENT, + startTimestamp, + attributes, + annotations, + messageEvents, + links, + CHILD_SPAN_COUNT, + status, + endTimestamp); + SpanData allSpanData2 = + SpanData.create( + spanContext, + parentSpanId, + false, + SPAN_NAME, + Kind.CLIENT, + startTimestamp, + attributes, + annotations, + messageEvents, + links, + CHILD_SPAN_COUNT, + status, + endTimestamp); + SpanData emptySpanData = + SpanData.create( + spanContext, + parentSpanId, + false, + SPAN_NAME, + null, + startTimestamp, + Attributes.create(Collections.<String, AttributeValue>emptyMap(), 0), + TimedEvents.create(Collections.<SpanData.TimedEvent<Annotation>>emptyList(), 0), + TimedEvents.create(Collections.<SpanData.TimedEvent<MessageEvent>>emptyList(), 0), + Links.create(Collections.<Link>emptyList(), 0), + 0, + status, + endTimestamp); + new EqualsTester() + .addEqualityGroup(allSpanData1, allSpanData2) + .addEqualityGroup(emptySpanData) + .testEquals(); + } + + @Test + public void spanData_ToString() { + String spanDataString = + SpanData.create( + spanContext, + parentSpanId, + false, + SPAN_NAME, + Kind.CLIENT, + startTimestamp, + attributes, + annotations, + messageEvents, + links, + CHILD_SPAN_COUNT, + status, + endTimestamp) + .toString(); + assertThat(spanDataString).contains(spanContext.toString()); + assertThat(spanDataString).contains(parentSpanId.toString()); + assertThat(spanDataString).contains(SPAN_NAME); + assertThat(spanDataString).contains(Kind.CLIENT.toString()); + assertThat(spanDataString).contains(startTimestamp.toString()); + assertThat(spanDataString).contains(attributes.toString()); + assertThat(spanDataString).contains(annotations.toString()); + assertThat(spanDataString).contains(messageEvents.toString()); + assertThat(spanDataString).contains(links.toString()); + assertThat(spanDataString).contains(status.toString()); + assertThat(spanDataString).contains(endTimestamp.toString()); + } +} |