aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Schmidt <ubschmidt2@users.noreply.github.com>2017-11-03 08:05:54 +0100
committerGitHub <noreply@github.com>2017-11-03 08:05:54 +0100
commit8a077450a97c700de33b9aeaa81c43787ebf2b02 (patch)
tree40209a745e0e857298bd9e51f00663fa93950f79
parent6cb4dd2cc949a2602783321da179cb11afb7fc7a (diff)
downloadopencensus-java-8a077450a97c700de33b9aeaa81c43787ebf2b02.tar.gz
Move FakeServiceHandler to -testing so that it can be used in other places, too. (#772)
-rw-r--r--impl_core/src/test/java/io/opencensus/implcore/trace/export/SpanExporterImplTest.java51
-rw-r--r--testing/src/main/java/io/opencensus/testing/export/TestHandler.java68
2 files changed, 71 insertions, 48 deletions
diff --git a/impl_core/src/test/java/io/opencensus/implcore/trace/export/SpanExporterImplTest.java b/impl_core/src/test/java/io/opencensus/implcore/trace/export/SpanExporterImplTest.java
index 3368c95a..e1f4cb8e 100644
--- a/impl_core/src/test/java/io/opencensus/implcore/trace/export/SpanExporterImplTest.java
+++ b/impl_core/src/test/java/io/opencensus/implcore/trace/export/SpanExporterImplTest.java
@@ -25,6 +25,7 @@ import io.opencensus.implcore.internal.SimpleEventQueue;
import io.opencensus.implcore.trace.SpanImpl;
import io.opencensus.implcore.trace.SpanImpl.StartEndHandler;
import io.opencensus.implcore.trace.StartEndHandlerImpl;
+import io.opencensus.testing.export.TestHandler;
import io.opencensus.trace.Span.Options;
import io.opencensus.trace.SpanContext;
import io.opencensus.trace.SpanId;
@@ -33,13 +34,9 @@ import io.opencensus.trace.TraceOptions;
import io.opencensus.trace.config.TraceParams;
import io.opencensus.trace.export.SpanData;
import io.opencensus.trace.export.SpanExporter.Handler;
-import java.util.ArrayList;
-import java.util.Collection;
import java.util.EnumSet;
-import java.util.LinkedList;
import java.util.List;
import java.util.Random;
-import javax.annotation.concurrent.GuardedBy;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -66,7 +63,7 @@ public class SpanExporterImplTest {
private final StartEndHandler startEndHandler =
new StartEndHandlerImpl(spanExporter, runningSpanStore, null, new SimpleEventQueue());
private EnumSet<Options> recordSpanOptions = EnumSet.of(Options.RECORD_EVENTS);
- private final FakeServiceHandler serviceHandler = new FakeServiceHandler();
+ private final TestHandler serviceHandler = new TestHandler();
@Mock private Handler mockServiceHandler;
@Before
@@ -160,7 +157,7 @@ public class SpanExporterImplTest {
@Test
public void exportSpansToMultipleServices() {
- FakeServiceHandler serviceHandler2 = new FakeServiceHandler();
+ TestHandler serviceHandler2 = new TestHandler();
spanExporter.registerHandler("test.service2", serviceHandler2);
SpanImpl span1 = createSampledEndedSpan(SPAN_NAME_1);
SpanImpl span2 = createSampledEndedSpan(SPAN_NAME_2);
@@ -184,46 +181,4 @@ public class SpanExporterImplTest {
assertThat(exported).doesNotContain(span1.toSpanData());
assertThat(exported).containsExactly(span2.toSpanData());
}
-
- /** Fake {@link Handler} for testing only. */
- private static final class FakeServiceHandler extends Handler {
- private final Object monitor = new Object();
-
- @GuardedBy("monitor")
- private final List<SpanData> spanDataList = new LinkedList<SpanData>();
-
- @Override
- public void export(Collection<SpanData> spanDataList) {
- synchronized (monitor) {
- this.spanDataList.addAll(spanDataList);
- monitor.notifyAll();
- }
- }
-
- /**
- * Waits until we received numberOfSpans spans to export. Returns the list of exported {@link
- * SpanData} objects, otherwise {@code null} if the current thread is interrupted.
- *
- * @param numberOfSpans the number of minimum spans to be collected.
- * @return the list of exported {@link SpanData} objects, otherwise {@code null} if the current
- * thread is interrupted.
- */
- private List<SpanData> waitForExport(int numberOfSpans) {
- List<SpanData> ret;
- synchronized (monitor) {
- while (spanDataList.size() < numberOfSpans) {
- try {
- monitor.wait();
- } catch (InterruptedException e) {
- // Preserve the interruption status as per guidance.
- Thread.currentThread().interrupt();
- return null;
- }
- }
- ret = new ArrayList<SpanData>(spanDataList);
- spanDataList.clear();
- }
- return ret;
- }
- }
}
diff --git a/testing/src/main/java/io/opencensus/testing/export/TestHandler.java b/testing/src/main/java/io/opencensus/testing/export/TestHandler.java
new file mode 100644
index 00000000..4d745230
--- /dev/null
+++ b/testing/src/main/java/io/opencensus/testing/export/TestHandler.java
@@ -0,0 +1,68 @@
+/*
+ * 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.testing.export;
+
+import io.opencensus.trace.export.SpanData;
+import io.opencensus.trace.export.SpanExporter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import javax.annotation.concurrent.GuardedBy;
+
+/** A {@link SpanExporter.Handler} for testing only. */
+public final class TestHandler extends SpanExporter.Handler {
+
+ private final Object monitor = new Object();
+
+ @GuardedBy("monitor")
+ private final List<SpanData> spanDataList = new LinkedList<SpanData>();
+
+ @Override
+ public void export(Collection<SpanData> spanDataList) {
+ synchronized (monitor) {
+ this.spanDataList.addAll(spanDataList);
+ monitor.notifyAll();
+ }
+ }
+
+ /**
+ * Waits until we received numberOfSpans spans to export. Returns the list of exported {@link
+ * SpanData} objects, otherwise {@code null} if the current thread is interrupted.
+ *
+ * @param numberOfSpans the number of minimum spans to be collected.
+ * @return the list of exported {@link SpanData} objects, otherwise {@code null} if the current
+ * thread is interrupted.
+ */
+ public List<SpanData> waitForExport(int numberOfSpans) {
+ List<SpanData> ret;
+ synchronized (monitor) {
+ while (spanDataList.size() < numberOfSpans) {
+ try {
+ monitor.wait();
+ } catch (InterruptedException e) {
+ // Preserve the interruption status as per guidance.
+ Thread.currentThread().interrupt();
+ return null;
+ }
+ }
+ ret = new ArrayList<SpanData>(spanDataList);
+ spanDataList.clear();
+ }
+ return ret;
+ }
+}