diff options
author | Julien Desprez <jdesprez@google.com> | 2018-10-22 11:42:32 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2018-10-22 11:42:32 -0700 |
commit | 108d816db7cf4559963e6474a679af7a25d892ad (patch) | |
tree | ede84fcf0a9687d4907ae5f8a4788271d62e0922 /testing | |
parent | cfbefd32336596ea63784607e4106dc37ce0567f (diff) | |
parent | 13217871fefa43f6d16fbb31b04e9904996d87d5 (diff) | |
download | opencensus-java-108d816db7cf4559963e6474a679af7a25d892ad.tar.gz |
Merge remote-tracking branch 'aosp/upstream-master' into merge am: dd3cabeacc am: 6fbc3cf5a1
am: 13217871fe
Change-Id: Icfe12d6db2f81a6f5c65241919b1d63fb665ffd6
Diffstat (limited to 'testing')
-rw-r--r-- | testing/README.md | 5 | ||||
-rw-r--r-- | testing/build.gradle | 11 | ||||
-rw-r--r-- | testing/src/main/java/io/opencensus/testing/common/TestClock.java | 104 | ||||
-rw-r--r-- | testing/src/main/java/io/opencensus/testing/export/TestHandler.java | 77 | ||||
-rw-r--r-- | testing/src/test/java/io/opencensus/testing/common/TestClockTest.java | 65 |
5 files changed, 262 insertions, 0 deletions
diff --git a/testing/README.md b/testing/README.md new file mode 100644 index 00000000..268bbf3d --- /dev/null +++ b/testing/README.md @@ -0,0 +1,5 @@ +OpenCensus Testing Package +====================================================== + +* Java 6 and Android compatible. +* The classes in this directory can be used to test the API integration. diff --git a/testing/build.gradle b/testing/build.gradle new file mode 100644 index 00000000..811b0597 --- /dev/null +++ b/testing/build.gradle @@ -0,0 +1,11 @@ +description = 'OpenCensus Testing' + +dependencies { + compile project(':opencensus-api'), + libraries.guava + + testCompile project(':opencensus-api') + + signature "org.codehaus.mojo.signature:java17:1.0@signature" + signature "net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature" +} diff --git a/testing/src/main/java/io/opencensus/testing/common/TestClock.java b/testing/src/main/java/io/opencensus/testing/common/TestClock.java new file mode 100644 index 00000000..b670cb7f --- /dev/null +++ b/testing/src/main/java/io/opencensus/testing/common/TestClock.java @@ -0,0 +1,104 @@ +/* + * 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.common; + +import com.google.common.math.LongMath; +import io.opencensus.common.Clock; +import io.opencensus.common.Duration; +import io.opencensus.common.Timestamp; +import javax.annotation.concurrent.GuardedBy; +import javax.annotation.concurrent.ThreadSafe; + +/** + * A {@link Clock} that allows the time to be set for testing. + * + * @since 0.5 + */ +@ThreadSafe +public final class TestClock extends Clock { + private static final int NUM_NANOS_PER_SECOND = 1000 * 1000 * 1000; + + @GuardedBy("this") + private Timestamp currentTime = validateNanos(Timestamp.create(1493419949, 223123456)); + + private TestClock() {} + + /** + * Creates a clock initialized to a constant non-zero time. {@code Timestamp.create(0, 0)} is not + * a good default, because it represents an invalid time. + * + * @return a clock initialized to a constant non-zero time. + * @since 0.5 + */ + public static TestClock create() { + return new TestClock(); + } + + /** + * Creates a clock with the given time. + * + * @param time the initial time. + * @return a new {@code TestClock} with the given time. + * @since 0.5 + */ + public static TestClock create(Timestamp time) { + TestClock clock = new TestClock(); + clock.setTime(time); + return clock; + } + + /** + * Sets the time. + * + * @param time the new time. + * @since 0.5 + */ + public synchronized void setTime(Timestamp time) { + currentTime = validateNanos(time); + } + + /** + * Advances the time by a duration. + * + * @param duration the increase in time. + * @since 0.5 + */ + public synchronized void advanceTime(Duration duration) { + currentTime = validateNanos(currentTime.addDuration(duration)); + } + + @Override + public synchronized Timestamp now() { + return currentTime; + } + + @Override + public synchronized long nowNanos() { + return getNanos(currentTime); + } + + private static Timestamp validateNanos(Timestamp time) { + getNanos(time); + return time; + } + + // Converts Timestamp into nanoseconds since time 0 and throws an exception if it overflows. + private static long getNanos(Timestamp time) { + return LongMath.checkedAdd( + LongMath.checkedMultiply(time.getSeconds(), NUM_NANOS_PER_SECOND), time.getNanos()); + } +} 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..6d73aff1 --- /dev/null +++ b/testing/src/main/java/io/opencensus/testing/export/TestHandler.java @@ -0,0 +1,77 @@ +/* + * 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.Nullable; +import javax.annotation.concurrent.GuardedBy; + +/** + * A {@link SpanExporter.Handler} for testing only. + * + * @since 0.9 + */ +public final class TestHandler extends SpanExporter.Handler { + + private final Object monitor = new Object(); + + // TODO: Decide whether to use a different class instead of LinkedList. + @GuardedBy("monitor") + @SuppressWarnings("JdkObsolete") + 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. + * @since 0.9 + */ + @Nullable + 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; + } +} diff --git a/testing/src/test/java/io/opencensus/testing/common/TestClockTest.java b/testing/src/test/java/io/opencensus/testing/common/TestClockTest.java new file mode 100644 index 00000000..24cd7fcd --- /dev/null +++ b/testing/src/test/java/io/opencensus/testing/common/TestClockTest.java @@ -0,0 +1,65 @@ +/* + * 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.common; + +import static com.google.common.truth.Truth.assertThat; + +import io.opencensus.common.Duration; +import io.opencensus.common.Timestamp; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link TestClock}. */ +@RunWith(JUnit4.class) +public final class TestClockTest { + private static final int NUM_NANOS_PER_SECOND = 1000 * 1000 * 1000; + + @Test + public void setAndGetTime() { + TestClock clock = TestClock.create(Timestamp.create(1, 2)); + assertThat(clock.now()).isEqualTo(Timestamp.create(1, 2)); + clock.setTime(Timestamp.create(3, 4)); + assertThat(clock.now()).isEqualTo(Timestamp.create(3, 4)); + } + + @Test + public void advanceTime() { + TestClock clock = TestClock.create(Timestamp.create(1, 500 * 1000 * 1000)); + clock.advanceTime(Duration.create(2, 600 * 1000 * 1000)); + assertThat(clock.now()).isEqualTo(Timestamp.create(4, 100 * 1000 * 1000)); + } + + @Test + public void measureElapsedTime() { + TestClock clock = TestClock.create(Timestamp.create(10, 1)); + long nanos1 = clock.nowNanos(); + clock.setTime(Timestamp.create(11, 5)); + long nanos2 = clock.nowNanos(); + assertThat(nanos2 - nanos1).isEqualTo(1000 * 1000 * 1000 + 4); + } + + @Test(expected = ArithmeticException.class) + public void catchOverflow() { + TestClock.create(Timestamp.create(Long.MAX_VALUE / NUM_NANOS_PER_SECOND + 1, 0)); + } + + @Test(expected = ArithmeticException.class) + public void catchNegativeOverflow() { + TestClock.create(Timestamp.create(Long.MIN_VALUE / NUM_NANOS_PER_SECOND - 1, 0)); + } +} |