From 4aa6e80c8efac571e3017b70d599eb52db23a6f3 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 16 Jun 2017 14:33:42 -0700 Subject: Move TestClock in opencensus-testing package. (#368) --- api/build.gradle | 3 +- .../java/io/opencensus/internal/TestClock.java | 99 ---------------------- .../java/io/opencensus/internal/TestClockTest.java | 62 -------------- core/build.gradle | 4 + core_impl/build.gradle | 5 +- .../stats/MeasurementDescriptorToViewMapTest.java | 2 +- .../stats/StatsContextFactoryTest.java | 2 +- .../instrumentation/stats/StatsContextTest.java | 2 +- .../stats/StatsManagerImplTest.java | 2 +- core_impl_android/build.gradle | 6 ++ core_impl_java/build.gradle | 6 ++ examples/build.gradle | 4 +- impl/build.gradle | 3 + impl_core/build.gradle | 3 + .../opencensus/trace/config/TraceConfigImpl.java | 4 +- .../io/opencensus/trace/SpanFactoryImplTest.java | 2 +- .../java/io/opencensus/trace/SpanImplTest.java | 2 +- .../propagation/PropagationComponentImplTest.java | 3 +- impl_lite/build.gradle | 3 + settings.gradle | 2 + testing/README.md | 5 ++ testing/build.gradle | 9 ++ .../io/opencensus/testing/common/TestClock.java | 99 ++++++++++++++++++++++ .../opencensus/testing/common/TestClockTest.java | 62 ++++++++++++++ 24 files changed, 219 insertions(+), 175 deletions(-) delete mode 100644 api/src/main/java/io/opencensus/internal/TestClock.java delete mode 100644 api/src/test/java/io/opencensus/internal/TestClockTest.java create mode 100644 testing/README.md create mode 100644 testing/build.gradle create mode 100644 testing/src/main/java/io/opencensus/testing/common/TestClock.java create mode 100644 testing/src/test/java/io/opencensus/testing/common/TestClockTest.java 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)); - } -} diff --git a/core/build.gradle b/core/build.gradle index 333dea0a..1d2266c1 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -8,8 +8,12 @@ dependencies { compile project(':opencensus-api'), libraries.grpc_context, libraries.guava + compileOnly libraries.auto_value + testCompile project(':opencensus-api'), + project(':opencensus-testing') + signature "org.codehaus.mojo.signature:java16:+@signature" } diff --git a/core_impl/build.gradle b/core_impl/build.gradle index 73593aa2..44e02423 100644 --- a/core_impl/build.gradle +++ b/core_impl/build.gradle @@ -6,7 +6,10 @@ dependencies { project(':opencensus-impl-core'), libraries.guava - testCompile project(':core') + testCompile project(':core'), + project(':opencensus-api'), + project(':opencensus-impl-core'), + project(':opencensus-testing') signature "org.codehaus.mojo.signature:java16:+@signature" } diff --git a/core_impl/src/test/java/com/google/instrumentation/stats/MeasurementDescriptorToViewMapTest.java b/core_impl/src/test/java/com/google/instrumentation/stats/MeasurementDescriptorToViewMapTest.java index 1b4bdf7b..86f344be 100644 --- a/core_impl/src/test/java/com/google/instrumentation/stats/MeasurementDescriptorToViewMapTest.java +++ b/core_impl/src/test/java/com/google/instrumentation/stats/MeasurementDescriptorToViewMapTest.java @@ -18,7 +18,7 @@ import static org.junit.Assert.fail; import io.opencensus.common.Function; import io.opencensus.common.Timestamp; -import io.opencensus.internal.TestClock; +import io.opencensus.testing.common.TestClock; import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; import com.google.instrumentation.stats.View.DistributionView; diff --git a/core_impl/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java b/core_impl/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java index 1bc7cfd3..e2ca9beb 100644 --- a/core_impl/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java +++ b/core_impl/src/test/java/com/google/instrumentation/stats/StatsContextFactoryTest.java @@ -17,7 +17,7 @@ import static com.google.common.truth.Truth.assertThat; import io.opencensus.common.NonThrowingCloseable; import io.opencensus.internal.SimpleEventQueue; -import io.opencensus.internal.TestClock; +import io.opencensus.testing.common.TestClock; import io.opencensus.internal.VarInt; import io.grpc.Context; import java.io.ByteArrayInputStream; diff --git a/core_impl/src/test/java/com/google/instrumentation/stats/StatsContextTest.java b/core_impl/src/test/java/com/google/instrumentation/stats/StatsContextTest.java index 783506ba..c30750c9 100644 --- a/core_impl/src/test/java/com/google/instrumentation/stats/StatsContextTest.java +++ b/core_impl/src/test/java/com/google/instrumentation/stats/StatsContextTest.java @@ -20,7 +20,7 @@ import com.google.common.collect.Collections2; import com.google.common.testing.EqualsTester; import io.opencensus.common.Function; import io.opencensus.internal.SimpleEventQueue; -import io.opencensus.internal.TestClock; +import io.opencensus.testing.common.TestClock; import io.opencensus.internal.VarInt; import com.google.instrumentation.stats.View.DistributionView; import com.google.instrumentation.stats.View.IntervalView; diff --git a/core_impl/src/test/java/com/google/instrumentation/stats/StatsManagerImplTest.java b/core_impl/src/test/java/com/google/instrumentation/stats/StatsManagerImplTest.java index a5f74211..7297540b 100644 --- a/core_impl/src/test/java/com/google/instrumentation/stats/StatsManagerImplTest.java +++ b/core_impl/src/test/java/com/google/instrumentation/stats/StatsManagerImplTest.java @@ -19,7 +19,7 @@ import static com.google.instrumentation.stats.StatsTestUtil.createContext; import io.opencensus.common.Duration; import io.opencensus.internal.SimpleEventQueue; import io.opencensus.common.Timestamp; -import io.opencensus.internal.TestClock; +import io.opencensus.testing.common.TestClock; import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit; import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit; import com.google.instrumentation.stats.View.DistributionView; diff --git a/core_impl_android/build.gradle b/core_impl_android/build.gradle index 4d830586..d5dc088a 100644 --- a/core_impl_android/build.gradle +++ b/core_impl_android/build.gradle @@ -4,5 +4,11 @@ dependencies { compile project(':core'), project(':core_impl') + testCompile project(':core'), + project(':core_impl'), + project(':opencensus-api'), + project(':opencensus-impl-core'), + project(':opencensus-impl-lite') + signature "net.sf.androidscents.signature:android-api-level-14:+@signature" } diff --git a/core_impl_java/build.gradle b/core_impl_java/build.gradle index 4c17d2b5..de9b0637 100644 --- a/core_impl_java/build.gradle +++ b/core_impl_java/build.gradle @@ -12,5 +12,11 @@ dependencies { project(':core_impl'), project(':opencensus-impl') + testCompile project(':core'), + project(':core_impl'), + project(':opencensus-api'), + project(':opencensus-impl-core'), + project(':opencensus-impl') + signature "org.codehaus.mojo.signature:java17:+@signature" } \ No newline at end of file diff --git a/examples/build.gradle b/examples/build.gradle index 480374a1..9439bba9 100644 --- a/examples/build.gradle +++ b/examples/build.gradle @@ -8,7 +8,9 @@ tasks.withType(JavaCompile) { dependencies { compile project(':core'), project(':core_impl'), - project(':core_impl_java') + project(':core_impl_java'), + project(':opencensus-api'), + project(':opencensus-impl') } // Provide convenience executables for trying out the examples. diff --git a/impl/build.gradle b/impl/build.gradle index be4e959c..e8852049 100644 --- a/impl/build.gradle +++ b/impl/build.gradle @@ -12,6 +12,9 @@ dependencies { project(':opencensus-impl-core'), libraries.disruptor + testCompile project(':opencensus-api'), + project(':opencensus-impl-core') + signature "org.codehaus.mojo.signature:java17:+@signature" } diff --git a/impl_core/build.gradle b/impl_core/build.gradle index 70de8aa6..4458d537 100644 --- a/impl_core/build.gradle +++ b/impl_core/build.gradle @@ -6,6 +6,9 @@ dependencies { compileOnly libraries.auto_value + testCompile project(':opencensus-api'), + project(':opencensus-testing') + signature "org.codehaus.mojo.signature:java16:+@signature" } diff --git a/impl_core/src/main/java/io/opencensus/trace/config/TraceConfigImpl.java b/impl_core/src/main/java/io/opencensus/trace/config/TraceConfigImpl.java index cf3dc59e..3b42c2b0 100644 --- a/impl_core/src/main/java/io/opencensus/trace/config/TraceConfigImpl.java +++ b/impl_core/src/main/java/io/opencensus/trace/config/TraceConfigImpl.java @@ -22,9 +22,7 @@ public final class TraceConfigImpl extends TraceConfig { // operations are visible on other CPUs as well. private volatile TraceParams activeTraceParams = TraceParams.DEFAULT; - /** - * Constructs a new {@code TraceConfigImpl}. - */ + /** Constructs a new {@code TraceConfigImpl}. */ public TraceConfigImpl() {} @Override diff --git a/impl_core/src/test/java/io/opencensus/trace/SpanFactoryImplTest.java b/impl_core/src/test/java/io/opencensus/trace/SpanFactoryImplTest.java index 00d051a6..1d5cf3ac 100644 --- a/impl_core/src/test/java/io/opencensus/trace/SpanFactoryImplTest.java +++ b/impl_core/src/test/java/io/opencensus/trace/SpanFactoryImplTest.java @@ -16,7 +16,7 @@ package io.opencensus.trace; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.when; -import io.opencensus.internal.TestClock; +import io.opencensus.testing.common.TestClock; import io.opencensus.trace.Span.Options; import io.opencensus.trace.SpanImpl.StartEndHandler; import io.opencensus.trace.base.SpanId; diff --git a/impl_core/src/test/java/io/opencensus/trace/SpanImplTest.java b/impl_core/src/test/java/io/opencensus/trace/SpanImplTest.java index 62052dd2..ed4aae74 100644 --- a/impl_core/src/test/java/io/opencensus/trace/SpanImplTest.java +++ b/impl_core/src/test/java/io/opencensus/trace/SpanImplTest.java @@ -17,8 +17,8 @@ import static com.google.common.truth.Truth.assertThat; import io.opencensus.common.Duration; import io.opencensus.common.Timestamp; -import io.opencensus.internal.TestClock; import io.opencensus.internal.TimestampConverter; +import io.opencensus.testing.common.TestClock; import io.opencensus.trace.Span.Options; import io.opencensus.trace.SpanImpl.StartEndHandler; import io.opencensus.trace.base.Annotation; diff --git a/impl_core/src/test/java/io/opencensus/trace/propagation/PropagationComponentImplTest.java b/impl_core/src/test/java/io/opencensus/trace/propagation/PropagationComponentImplTest.java index dd7812ab..9c1228ac 100644 --- a/impl_core/src/test/java/io/opencensus/trace/propagation/PropagationComponentImplTest.java +++ b/impl_core/src/test/java/io/opencensus/trace/propagation/PropagationComponentImplTest.java @@ -26,7 +26,6 @@ public class PropagationComponentImplTest { @Test public void implementationOfBinary() { - assertThat(propagationComponent.getBinaryFormat()) - .isInstanceOf(BinaryFormatImpl.class); + assertThat(propagationComponent.getBinaryFormat()).isInstanceOf(BinaryFormatImpl.class); } } diff --git a/impl_lite/build.gradle b/impl_lite/build.gradle index 40cce82a..30ba7873 100644 --- a/impl_lite/build.gradle +++ b/impl_lite/build.gradle @@ -4,5 +4,8 @@ dependencies { compile project(':opencensus-api'), project(':opencensus-impl-core') + testCompile project(':opencensus-api'), + project(':opencensus-impl-core') + signature "net.sf.androidscents.signature:android-api-level-14:+@signature" } diff --git a/settings.gradle b/settings.gradle index e7a8c4c4..41395b1d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -4,6 +4,7 @@ include ":opencensus-api" include ":opencensus-impl-core" include ":opencensus-impl-lite" include ":opencensus-impl" +include ":opencensus-testing" include ":all" include ":core" include ":core_impl" @@ -14,6 +15,7 @@ project(':opencensus-api').projectDir = "$rootDir/api" as File project(':opencensus-impl-core').projectDir = "$rootDir/impl_core" as File project(':opencensus-impl-lite').projectDir = "$rootDir/impl_lite" as File project(':opencensus-impl').projectDir = "$rootDir/impl" as File +project(':opencensus-testing').projectDir = "$rootDir/testing" as File // Java8 projects only if (JavaVersion.current().isJava8Compatible()) { 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..179ab71b --- /dev/null +++ b/testing/build.gradle @@ -0,0 +1,9 @@ +description = 'OpenCensus Testing' + +dependencies { + compile project(':opencensus-api') + + testCompile project(':opencensus-api') + + signature "org.codehaus.mojo.signature:java16:+@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..2a604f2a --- /dev/null +++ b/testing/src/main/java/io/opencensus/testing/common/TestClock.java @@ -0,0 +1,99 @@ +/* + * 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.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. */ +@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/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..f67080c7 --- /dev/null +++ b/testing/src/test/java/io/opencensus/testing/common/TestClockTest.java @@ -0,0 +1,62 @@ +/* + * 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.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)); + } +} -- cgit v1.2.3