diff options
| author | Bogdan Drutu <bdrutu@google.com> | 2017-06-16 14:33:42 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-06-16 14:33:42 -0700 |
| commit | 4aa6e80c8efac571e3017b70d599eb52db23a6f3 (patch) | |
| tree | 479e2448d3c30a1a6ec5eda2294892c65eb7ed3c /api | |
| parent | a7e1b0cb733a5c6b7cbe5b90a2ab57507be0c5bd (diff) | |
| download | platform_external_opencensus-java-4aa6e80c8efac571e3017b70d599eb52db23a6f3.tar.gz platform_external_opencensus-java-4aa6e80c8efac571e3017b70d599eb52db23a6f3.tar.bz2 platform_external_opencensus-java-4aa6e80c8efac571e3017b70d599eb52db23a6f3.zip | |
Move TestClock in opencensus-testing package. (#368)
Diffstat (limited to 'api')
| -rw-r--r-- | api/build.gradle | 3 | ||||
| -rw-r--r-- | api/src/main/java/io/opencensus/internal/TestClock.java | 99 | ||||
| -rw-r--r-- | api/src/test/java/io/opencensus/internal/TestClockTest.java | 62 |
3 files changed, 2 insertions, 162 deletions
diff --git a/api/build.gradle b/api/build.gradle index 8d2967bc..3834f051 100644 --- a/api/build.gradle +++ b/api/build.gradle @@ -1,8 +1,9 @@ -description = 'OpenCensus: API' +description = 'OpenCensus API' dependencies { compile libraries.grpc_context, libraries.guava + compileOnly libraries.auto_value signature "org.codehaus.mojo.signature:java16:+@signature" diff --git a/api/src/main/java/io/opencensus/internal/TestClock.java b/api/src/main/java/io/opencensus/internal/TestClock.java deleted file mode 100644 index d181da3a..00000000 --- a/api/src/main/java/io/opencensus/internal/TestClock.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2017, Google Inc. - * 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.internal; - -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. */ -@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. - */ - 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. - */ - public static TestClock create(Timestamp time) { - TestClock clock = new TestClock(); - clock.setTime(time); - return clock; - } - - /** - * Sets the time. - * - * @param time the new time. - */ - public synchronized void setTime(Timestamp time) { - currentTime = validateNanos(time); - } - - /** - * Advances the time by a duration. - * - * @param duration the increase in time. - */ - // TODO(sebright): Consider adding an 'addDuration' method to Timestamp. - public synchronized void advanceTime(Duration duration) { - currentTime = - validateNanos( - Timestamp.create( - LongMath.checkedAdd(currentTime.getSeconds(), duration.getSeconds()), - currentTime.getNanos()) - .addNanos(duration.getNanos())); - } - - @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/api/src/test/java/io/opencensus/internal/TestClockTest.java b/api/src/test/java/io/opencensus/internal/TestClockTest.java deleted file mode 100644 index fe3f80cb..00000000 --- a/api/src/test/java/io/opencensus/internal/TestClockTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2017, Google Inc. - * 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.internal; - -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)); - } -} |
