diff options
Diffstat (limited to 'api/src/test/java/io/opencensus/common')
7 files changed, 782 insertions, 0 deletions
diff --git a/api/src/test/java/io/opencensus/common/DurationTest.java b/api/src/test/java/io/opencensus/common/DurationTest.java new file mode 100644 index 00000000..ea636ca0 --- /dev/null +++ b/api/src/test/java/io/opencensus/common/DurationTest.java @@ -0,0 +1,152 @@ +/* + * 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.common; + +import static com.google.common.truth.Truth.assertThat; + +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 Duration}. */ +@RunWith(JUnit4.class) +public class DurationTest { + @Rule public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testDurationCreate() { + assertThat(Duration.create(24, 42).getSeconds()).isEqualTo(24); + assertThat(Duration.create(24, 42).getNanos()).isEqualTo(42); + assertThat(Duration.create(-24, -42).getSeconds()).isEqualTo(-24); + assertThat(Duration.create(-24, -42).getNanos()).isEqualTo(-42); + assertThat(Duration.create(315576000000L, 999999999).getSeconds()).isEqualTo(315576000000L); + assertThat(Duration.create(315576000000L, 999999999).getNanos()).isEqualTo(999999999); + assertThat(Duration.create(-315576000000L, -999999999).getSeconds()).isEqualTo(-315576000000L); + assertThat(Duration.create(-315576000000L, -999999999).getNanos()).isEqualTo(-999999999); + } + + @Test + public void create_SecondsTooLow() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'seconds' is less than minimum (-315576000000): -315576000001"); + Duration.create(-315576000001L, 0); + } + + @Test + public void create_SecondsTooHigh() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'seconds' is greater than maximum (315576000000): 315576000001"); + Duration.create(315576000001L, 0); + } + + @Test + public void create_NanosTooLow() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'nanos' is less than minimum (-999999999): -1000000000"); + Duration.create(0, -1000000000); + } + + @Test + public void create_NanosTooHigh() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'nanos' is greater than maximum (999999999): 1000000000"); + Duration.create(0, 1000000000); + } + + @Test + public void create_NegativeSecondsPositiveNanos() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'seconds' and 'nanos' have inconsistent sign: seconds=-1, nanos=1"); + Duration.create(-1, 1); + } + + @Test + public void create_PositiveSecondsNegativeNanos() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'seconds' and 'nanos' have inconsistent sign: seconds=1, nanos=-1"); + Duration.create(1, -1); + } + + @Test + public void testDurationFromMillis() { + assertThat(Duration.fromMillis(0)).isEqualTo(Duration.create(0, 0)); + assertThat(Duration.fromMillis(987)).isEqualTo(Duration.create(0, 987000000)); + assertThat(Duration.fromMillis(3456)).isEqualTo(Duration.create(3, 456000000)); + } + + @Test + public void testDurationFromMillisNegative() { + assertThat(Duration.fromMillis(-1)).isEqualTo(Duration.create(0, -1000000)); + assertThat(Duration.fromMillis(-999)).isEqualTo(Duration.create(0, -999000000)); + assertThat(Duration.fromMillis(-1000)).isEqualTo(Duration.create(-1, 0)); + assertThat(Duration.fromMillis(-3456)).isEqualTo(Duration.create(-3, -456000000)); + } + + @Test + public void fromMillis_TooLow() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'seconds' is less than minimum (-315576000000): -315576000001"); + Duration.fromMillis(-315576000001000L); + } + + @Test + public void fromMillis_TooHigh() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'seconds' is greater than maximum (315576000000): 315576000001"); + Duration.fromMillis(315576000001000L); + } + + @Test + public void duration_CompareLength() { + assertThat(Duration.create(0, 0).compareTo(Duration.create(0, 0))).isEqualTo(0); + assertThat(Duration.create(24, 42).compareTo(Duration.create(24, 42))).isEqualTo(0); + assertThat(Duration.create(-24, -42).compareTo(Duration.create(-24, -42))).isEqualTo(0); + assertThat(Duration.create(25, 42).compareTo(Duration.create(24, 42))).isEqualTo(1); + assertThat(Duration.create(24, 45).compareTo(Duration.create(24, 42))).isEqualTo(1); + assertThat(Duration.create(24, 42).compareTo(Duration.create(25, 42))).isEqualTo(-1); + assertThat(Duration.create(24, 42).compareTo(Duration.create(24, 45))).isEqualTo(-1); + assertThat(Duration.create(-24, -45).compareTo(Duration.create(-24, -42))).isEqualTo(-1); + assertThat(Duration.create(-24, -42).compareTo(Duration.create(-25, -42))).isEqualTo(1); + assertThat(Duration.create(24, 42).compareTo(Duration.create(-24, -42))).isEqualTo(1); + } + + @Test + public void testDurationEqual() { + // Positive tests. + assertThat(Duration.create(0, 0)).isEqualTo(Duration.create(0, 0)); + assertThat(Duration.create(24, 42)).isEqualTo(Duration.create(24, 42)); + assertThat(Duration.create(-24, -42)).isEqualTo(Duration.create(-24, -42)); + // Negative tests. + assertThat(Duration.create(25, 42)).isNotEqualTo(Duration.create(24, 42)); + assertThat(Duration.create(24, 43)).isNotEqualTo(Duration.create(24, 42)); + assertThat(Duration.create(-25, -42)).isNotEqualTo(Duration.create(-24, -42)); + assertThat(Duration.create(-24, -43)).isNotEqualTo(Duration.create(-24, -42)); + } + + @Test + public void toMillis() { + assertThat(Duration.create(10, 0).toMillis()).isEqualTo(10000L); + assertThat(Duration.create(10, 1000).toMillis()).isEqualTo(10000L); + assertThat(Duration.create(0, (int) 1e6).toMillis()).isEqualTo(1L); + assertThat(Duration.create(0, 0).toMillis()).isEqualTo(0L); + assertThat(Duration.create(-10, 0).toMillis()).isEqualTo(-10000L); + assertThat(Duration.create(-10, -1000).toMillis()).isEqualTo(-10000L); + assertThat(Duration.create(0, -(int) 1e6).toMillis()).isEqualTo(-1L); + } +} diff --git a/api/src/test/java/io/opencensus/common/FunctionsTest.java b/api/src/test/java/io/opencensus/common/FunctionsTest.java new file mode 100644 index 00000000..55d58d4d --- /dev/null +++ b/api/src/test/java/io/opencensus/common/FunctionsTest.java @@ -0,0 +1,64 @@ +/* + * 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.common; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link Functions}. */ +@RunWith(JUnit4.class) +public class FunctionsTest { + @Rule public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testReturnNull() { + assertThat(Functions.returnNull().apply("ignored")).isNull(); + } + + @Test + public void testReturnConstant() { + assertThat(Functions.returnConstant(123).apply("ignored")).isEqualTo(123); + } + + @Test + public void testReturnToString() { + assertThat(Functions.returnToString().apply("input")).isEqualTo("input"); + assertThat(Functions.returnToString().apply(Boolean.FALSE)).isEqualTo("false"); + assertThat(Functions.returnToString().apply(Double.valueOf(123.45))).isEqualTo("123.45"); + assertThat(Functions.returnToString().apply(null)).isEqualTo(null); + } + + @Test + public void testThrowIllegalArgumentException() { + Function<Object, Void> f = Functions.throwIllegalArgumentException(); + thrown.expect(IllegalArgumentException.class); + f.apply("ignored"); + } + + @Test + public void testThrowAssertionError() { + Function<Object, Void> f = Functions.throwAssertionError(); + thrown.handleAssertionErrors(); + thrown.expect(AssertionError.class); + f.apply("ignored"); + } +} diff --git a/api/src/test/java/io/opencensus/common/ServerStatsEncodingTest.java b/api/src/test/java/io/opencensus/common/ServerStatsEncodingTest.java new file mode 100644 index 00000000..6db14a79 --- /dev/null +++ b/api/src/test/java/io/opencensus/common/ServerStatsEncodingTest.java @@ -0,0 +1,155 @@ +/* + * Copyright 2018, 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.common; + +import static com.google.common.truth.Truth.assertThat; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +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 ServerStatsEncoding}. */ +@RunWith(JUnit4.class) +public class ServerStatsEncodingTest { + + @Rule public final ExpectedException thrown = ExpectedException.none(); + + @Test + public void encodeDecodeTest() throws ServerStatsDeserializationException { + ServerStats serverStatsToBeEncoded = null; + ServerStats serverStatsDecoded = null; + byte[] serialized = null; + + serverStatsToBeEncoded = ServerStats.create(31, 22, (byte) 1); + serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded); + serverStatsDecoded = ServerStatsEncoding.parseBytes(serialized); + assertThat(serverStatsDecoded).isEqualTo(serverStatsToBeEncoded); + + serverStatsToBeEncoded = ServerStats.create(0, 22, (byte) 0); + serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded); + serverStatsDecoded = ServerStatsEncoding.parseBytes(serialized); + assertThat(serverStatsDecoded).isEqualTo(serverStatsToBeEncoded); + + serverStatsToBeEncoded = ServerStats.create(450, 0, (byte) 0); + serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded); + serverStatsDecoded = ServerStatsEncoding.parseBytes(serialized); + assertThat(serverStatsDecoded).isEqualTo(serverStatsToBeEncoded); + } + + @Test + public void skipUnknownFieldTest() throws ServerStatsDeserializationException { + ServerStats serverStatsToBeEncoded = null; + ServerStats serverStatsDecoded = null; + byte[] serialized = null; + + serverStatsToBeEncoded = ServerStats.create(31, 22, (byte) 1); + serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded); + + // Add new field at the end. + byte[] serializedExpanded = new byte[serialized.length + 9]; + System.arraycopy(serialized, 0, serializedExpanded, 0, serialized.length); + final ByteBuffer bb = ByteBuffer.wrap(serializedExpanded); + bb.order(ByteOrder.LITTLE_ENDIAN); + bb.position(serialized.length); + bb.put((byte) 255); + bb.putLong(0L); + byte[] newSerialized = bb.array(); + + serverStatsDecoded = ServerStatsEncoding.parseBytes(newSerialized); + assertThat(serverStatsDecoded).isEqualTo(serverStatsToBeEncoded); + } + + @Test + public void negativeLbLatencyValueTest() throws ServerStatsDeserializationException { + ServerStats serverStatsToBeEncoded = null; + ServerStats serverStatsDecoded = null; + byte[] serialized = null; + + serverStatsToBeEncoded = ServerStats.create(31, 22, (byte) 1); + serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded); + + // update serialized byte[] with negative value for lbLatency. + final ByteBuffer bb = ByteBuffer.wrap(serialized); + bb.order(ByteOrder.LITTLE_ENDIAN); + bb.position(2); + bb.putLong(-100L); + + byte[] newSerialized = bb.array(); + thrown.expect(ServerStatsDeserializationException.class); + thrown.expectMessage("Serialized ServiceStats contains invalid values"); + ServerStatsEncoding.parseBytes(newSerialized); + } + + @Test + public void negativeServerLatencyValueTest() throws ServerStatsDeserializationException { + ServerStats serverStatsToBeEncoded = null; + ServerStats serverStatsDecoded = null; + byte[] serialized = null; + + serverStatsToBeEncoded = ServerStats.create(31, 22, (byte) 1); + serialized = ServerStatsEncoding.toBytes(serverStatsToBeEncoded); + + // update serialized byte[] with negative value for serviceLatency. + final ByteBuffer bb = ByteBuffer.wrap(serialized); + bb.order(ByteOrder.LITTLE_ENDIAN); + bb.position(11); + bb.putLong(-101L); + + byte[] newSerialized = bb.array(); + thrown.expect(ServerStatsDeserializationException.class); + thrown.expectMessage("Serialized ServiceStats contains invalid values"); + ServerStatsEncoding.parseBytes(newSerialized); + } + + @Test + public void emptySerializedBuffer() throws ServerStatsDeserializationException { + final ByteBuffer bb = ByteBuffer.allocate(0); + bb.order(ByteOrder.LITTLE_ENDIAN); + + byte[] newSerialized = bb.array(); + thrown.expect(ServerStatsDeserializationException.class); + thrown.expectMessage("Serialized ServerStats buffer is empty"); + ServerStatsEncoding.parseBytes(newSerialized); + } + + @Test + public void invalidNegativeVersion() throws ServerStatsDeserializationException { + final ByteBuffer bb = ByteBuffer.allocate(10); + bb.order(ByteOrder.LITTLE_ENDIAN); + bb.put((byte) -1); + byte[] newSerialized = bb.array(); + thrown.expect(ServerStatsDeserializationException.class); + thrown.expectMessage("Invalid ServerStats version: -1"); + ServerStatsEncoding.parseBytes(newSerialized); + } + + @Test + public void invalidCompatibleVersion() throws ServerStatsDeserializationException { + final ByteBuffer bb = ByteBuffer.allocate(10); + bb.order(ByteOrder.LITTLE_ENDIAN); + bb.put((byte) (ServerStatsEncoding.CURRENT_VERSION + 1)); + byte[] newSerialized = bb.array(); + thrown.expect(ServerStatsDeserializationException.class); + thrown.expectMessage( + "Invalid ServerStats version: " + (ServerStatsEncoding.CURRENT_VERSION + 1)); + ServerStatsEncoding.parseBytes(newSerialized); + } +} diff --git a/api/src/test/java/io/opencensus/common/ServerStatsFieldEnumsTest.java b/api/src/test/java/io/opencensus/common/ServerStatsFieldEnumsTest.java new file mode 100644 index 00000000..ed786f6c --- /dev/null +++ b/api/src/test/java/io/opencensus/common/ServerStatsFieldEnumsTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2018, 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.common; + +import static com.google.common.truth.Truth.assertThat; + +import io.opencensus.common.ServerStatsFieldEnums.Id; +import io.opencensus.common.ServerStatsFieldEnums.Size; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link ServerStatsFieldEnums}. */ +@RunWith(JUnit4.class) +public class ServerStatsFieldEnumsTest { + + @Test + public void enumIdValueOfTest() { + assertThat(Id.valueOf(0)).isEqualTo(Id.SERVER_STATS_LB_LATENCY_ID); + assertThat(Id.valueOf(1)).isEqualTo(Id.SERVER_STATS_SERVICE_LATENCY_ID); + assertThat(Id.valueOf(2)).isEqualTo(Id.SERVER_STATS_TRACE_OPTION_ID); + } + + @Test + public void enumIdInvalidValueOfTest() { + assertThat(Id.valueOf(-1)).isNull(); + assertThat(Id.valueOf(Id.values().length)).isNull(); + assertThat(Id.valueOf(Id.values().length + 1)).isNull(); + } + + @Test + public void enumSizeValueTest() { + assertThat(Size.SERVER_STATS_LB_LATENCY_SIZE.value()).isEqualTo(8); + assertThat(Size.SERVER_STATS_SERVICE_LATENCY_SIZE.value()).isEqualTo(8); + assertThat(Size.SERVER_STATS_TRACE_OPTION_SIZE.value()).isEqualTo(1); + } + + @Test + public void totalSizeTest() { + assertThat(ServerStatsFieldEnums.getTotalSize()).isEqualTo(20); + } +} diff --git a/api/src/test/java/io/opencensus/common/ServerStatsTest.java b/api/src/test/java/io/opencensus/common/ServerStatsTest.java new file mode 100644 index 00000000..620bbb4f --- /dev/null +++ b/api/src/test/java/io/opencensus/common/ServerStatsTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2018, 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.common; + +import static com.google.common.truth.Truth.assertThat; + +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 ServerStats}. */ +@RunWith(JUnit4.class) +public class ServerStatsTest { + + @Rule public ExpectedException thrown = ExpectedException.none(); + + @Test + public void serverStatsCreate() { + ServerStats serverStats = null; + + serverStats = ServerStats.create(31, 22, (byte) 0); + assertThat(serverStats.getLbLatencyNs()).isEqualTo(31); + assertThat(serverStats.getServiceLatencyNs()).isEqualTo(22); + assertThat(serverStats.getTraceOption()).isEqualTo((byte) 0); + + serverStats = ServerStats.create(1000011L, 900022L, (byte) 1); + assertThat(serverStats.getLbLatencyNs()).isEqualTo(1000011L); + assertThat(serverStats.getServiceLatencyNs()).isEqualTo(900022L); + assertThat(serverStats.getTraceOption()).isEqualTo((byte) 1); + + serverStats = ServerStats.create(0, 22, (byte) 0); + assertThat(serverStats.getLbLatencyNs()).isEqualTo(0); + assertThat(serverStats.getServiceLatencyNs()).isEqualTo(22); + assertThat(serverStats.getTraceOption()).isEqualTo((byte) 0); + + serverStats = ServerStats.create(1010, 0, (byte) 0); + assertThat(serverStats.getLbLatencyNs()).isEqualTo(1010); + assertThat(serverStats.getServiceLatencyNs()).isEqualTo(0); + assertThat(serverStats.getTraceOption()).isEqualTo((byte) 0); + } + + @Test + public void create_LbLatencyNegative() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'getLbLatencyNs' is less than zero"); + ServerStats.create(-1L, 100, (byte) 0); + } + + @Test + public void create_ServerLatencyNegative() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'getServiceLatencyNs' is less than zero"); + ServerStats.create(100L, -1L, (byte) 0); + } + + @Test + public void create_LbLatencyAndServerLatencyNegative() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'getLbLatencyNs' is less than zero"); + ServerStats.create(-100L, -1L, (byte) 0); + } +} diff --git a/api/src/test/java/io/opencensus/common/TimeUtilsTest.java b/api/src/test/java/io/opencensus/common/TimeUtilsTest.java new file mode 100644 index 00000000..d6228566 --- /dev/null +++ b/api/src/test/java/io/opencensus/common/TimeUtilsTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2018, 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.common; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link TimeUtils}. */ +@RunWith(JUnit4.class) +public final class TimeUtilsTest { + + @Rule public ExpectedException thrown = ExpectedException.none(); + + @Test + public void compareLongs() { + assertThat(TimeUtils.compareLongs(-1L, 1L)).isLessThan(0); + assertThat(TimeUtils.compareLongs(10L, 10L)).isEqualTo(0); + assertThat(TimeUtils.compareLongs(1L, 0L)).isGreaterThan(0); + } + + @Test + public void checkedAdd_TooLow() { + thrown.expect(ArithmeticException.class); + thrown.expectMessage("Long sum overflow: x=-9223372036854775807, y=-2"); + TimeUtils.checkedAdd(Long.MIN_VALUE + 1, -2); + } + + @Test + public void checkedAdd_TooHigh() { + thrown.expect(ArithmeticException.class); + thrown.expectMessage("Long sum overflow: x=9223372036854775806, y=2"); + TimeUtils.checkedAdd(Long.MAX_VALUE - 1, 2); + } + + @Test + public void checkedAdd_Valid() { + assertThat(TimeUtils.checkedAdd(1, 2)).isEqualTo(3); + assertThat(TimeUtils.checkedAdd(Integer.MAX_VALUE, Integer.MAX_VALUE)) + .isEqualTo(2L * Integer.MAX_VALUE); + } +} diff --git a/api/src/test/java/io/opencensus/common/TimestampTest.java b/api/src/test/java/io/opencensus/common/TimestampTest.java new file mode 100644 index 00000000..b193b3c8 --- /dev/null +++ b/api/src/test/java/io/opencensus/common/TimestampTest.java @@ -0,0 +1,217 @@ +/* + * 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.common; + +import static com.google.common.truth.Truth.assertThat; + +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 Timestamp}. */ +@RunWith(JUnit4.class) +public class TimestampTest { + @Rule public ExpectedException thrown = ExpectedException.none(); + + @Test + public void timestampCreate() { + assertThat(Timestamp.create(24, 42).getSeconds()).isEqualTo(24); + assertThat(Timestamp.create(24, 42).getNanos()).isEqualTo(42); + assertThat(Timestamp.create(-24, 42).getSeconds()).isEqualTo(-24); + assertThat(Timestamp.create(-24, 42).getNanos()).isEqualTo(42); + assertThat(Timestamp.create(315576000000L, 999999999).getSeconds()).isEqualTo(315576000000L); + assertThat(Timestamp.create(315576000000L, 999999999).getNanos()).isEqualTo(999999999); + assertThat(Timestamp.create(-315576000000L, 999999999).getSeconds()).isEqualTo(-315576000000L); + assertThat(Timestamp.create(-315576000000L, 999999999).getNanos()).isEqualTo(999999999); + } + + @Test + public void create_SecondsTooLow() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'seconds' is less than minimum (-315576000000): -315576000001"); + Timestamp.create(-315576000001L, 0); + } + + @Test + public void create_SecondsTooHigh() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'seconds' is greater than maximum (315576000000): 315576000001"); + Timestamp.create(315576000001L, 0); + } + + @Test + public void create_NanosTooLow_PositiveTime() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'nanos' is less than zero: -1"); + Timestamp.create(1, -1); + } + + @Test + public void create_NanosTooHigh_PositiveTime() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'nanos' is greater than maximum (999999999): 1000000000"); + Timestamp.create(1, 1000000000); + } + + @Test + public void create_NanosTooLow_NegativeTime() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'nanos' is less than zero: -1"); + Timestamp.create(-1, -1); + } + + @Test + public void create_NanosTooHigh_NegativeTime() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'nanos' is greater than maximum (999999999): 1000000000"); + Timestamp.create(-1, 1000000000); + } + + @Test + public void timestampFromMillis() { + assertThat(Timestamp.fromMillis(0)).isEqualTo(Timestamp.create(0, 0)); + assertThat(Timestamp.fromMillis(987)).isEqualTo(Timestamp.create(0, 987000000)); + assertThat(Timestamp.fromMillis(3456)).isEqualTo(Timestamp.create(3, 456000000)); + } + + @Test + public void timestampFromMillis_Negative() { + assertThat(Timestamp.fromMillis(-1)).isEqualTo(Timestamp.create(-1, 999000000)); + assertThat(Timestamp.fromMillis(-999)).isEqualTo(Timestamp.create(-1, 1000000)); + assertThat(Timestamp.fromMillis(-3456)).isEqualTo(Timestamp.create(-4, 544000000)); + } + + @Test + public void fromMillis_TooLow() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'seconds' is less than minimum (-315576000000): -315576000001"); + Timestamp.fromMillis(-315576000001000L); + } + + @Test + public void fromMillis_TooHigh() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("'seconds' is greater than maximum (315576000000): 315576000001"); + Timestamp.fromMillis(315576000001000L); + } + + @Test + public void timestampAddNanos() { + Timestamp timestamp = Timestamp.create(1234, 223); + assertThat(timestamp.addNanos(0)).isEqualTo(timestamp); + assertThat(timestamp.addNanos(999999777)).isEqualTo(Timestamp.create(1235, 0)); + assertThat(timestamp.addNanos(1300200500)).isEqualTo(Timestamp.create(1235, 300200723)); + assertThat(timestamp.addNanos(1999999777)).isEqualTo(Timestamp.create(1236, 0)); + assertThat(timestamp.addNanos(9876543789L)).isEqualTo(Timestamp.create(1243, 876544012)); + assertThat(timestamp.addNanos(Long.MAX_VALUE)) + .isEqualTo(Timestamp.create(1234L + 9223372036L, 223 + 854775807)); + } + + @Test + public void timestampAddNanos_Negative() { + Timestamp timestamp = Timestamp.create(1234, 223); + assertThat(timestamp.addNanos(-223)).isEqualTo(Timestamp.create(1234, 0)); + assertThat(timestamp.addNanos(-1000000223)).isEqualTo(Timestamp.create(1233, 0)); + assertThat(timestamp.addNanos(-1300200500)).isEqualTo(Timestamp.create(1232, 699799723)); + assertThat(timestamp.addNanos(-4123456213L)).isEqualTo(Timestamp.create(1229, 876544010)); + assertThat(timestamp.addNanos(Long.MIN_VALUE)) + .isEqualTo(Timestamp.create(1234L - 9223372036L - 1, 223 + 145224192)); + } + + @Test + public void timestampAddDuration() { + Timestamp timestamp = Timestamp.create(1234, 223); + assertThat(timestamp.addDuration(Duration.create(1, 0))).isEqualTo(Timestamp.create(1235, 223)); + assertThat(timestamp.addDuration(Duration.create(0, 1))).isEqualTo(Timestamp.create(1234, 224)); + assertThat(timestamp.addDuration(Duration.create(1, 1))).isEqualTo(Timestamp.create(1235, 224)); + assertThat(timestamp.addDuration(Duration.create(1, 999999900))) + .isEqualTo(Timestamp.create(1236, 123)); + } + + @Test + public void timestampAddDuration_Negative() { + Timestamp timestamp = Timestamp.create(1234, 223); + assertThat(timestamp.addDuration(Duration.create(-1234, -223))) + .isEqualTo(Timestamp.create(0, 0)); + assertThat(timestamp.addDuration(Duration.create(-1, 0))) + .isEqualTo(Timestamp.create(1233, 223)); + assertThat(timestamp.addDuration(Duration.create(-1, -1))) + .isEqualTo(Timestamp.create(1233, 222)); + assertThat(timestamp.addDuration(Duration.create(-1, -323))) + .isEqualTo(Timestamp.create(1232, 999999900)); + assertThat(timestamp.addDuration(Duration.create(-33, -999999999))) + .isEqualTo(Timestamp.create(1200, 224)); + } + + @Test + public void timestampSubtractTimestamp() { + Timestamp timestamp = Timestamp.create(1234, 223); + assertThat(timestamp.subtractTimestamp(Timestamp.create(0, 0))) + .isEqualTo(Duration.create(1234, 223)); + assertThat(timestamp.subtractTimestamp(Timestamp.create(1233, 223))) + .isEqualTo(Duration.create(1, 0)); + assertThat(timestamp.subtractTimestamp(Timestamp.create(1233, 222))) + .isEqualTo(Duration.create(1, 1)); + assertThat(timestamp.subtractTimestamp(Timestamp.create(1232, 999999900))) + .isEqualTo(Duration.create(1, 323)); + assertThat(timestamp.subtractTimestamp(Timestamp.create(1200, 224))) + .isEqualTo(Duration.create(33, 999999999)); + } + + @Test + public void timestampSubtractTimestamp_NegativeResult() { + Timestamp timestamp = Timestamp.create(1234, 223); + assertThat(timestamp.subtractTimestamp(Timestamp.create(1235, 223))) + .isEqualTo(Duration.create(-1, 0)); + assertThat(timestamp.subtractTimestamp(Timestamp.create(1234, 224))) + .isEqualTo(Duration.create(0, -1)); + assertThat(timestamp.subtractTimestamp(Timestamp.create(1235, 224))) + .isEqualTo(Duration.create(-1, -1)); + assertThat(timestamp.subtractTimestamp(Timestamp.create(1236, 123))) + .isEqualTo(Duration.create(-1, -999999900)); + } + + @Test + public void timestamp_CompareTo() { + assertThat(Timestamp.create(0, 0).compareTo(Timestamp.create(0, 0))).isEqualTo(0); + assertThat(Timestamp.create(24, 42).compareTo(Timestamp.create(24, 42))).isEqualTo(0); + assertThat(Timestamp.create(-24, 42).compareTo(Timestamp.create(-24, 42))).isEqualTo(0); + assertThat(Timestamp.create(25, 42).compareTo(Timestamp.create(24, 42))).isEqualTo(1); + assertThat(Timestamp.create(24, 45).compareTo(Timestamp.create(24, 42))).isEqualTo(1); + assertThat(Timestamp.create(24, 42).compareTo(Timestamp.create(25, 42))).isEqualTo(-1); + assertThat(Timestamp.create(24, 42).compareTo(Timestamp.create(24, 45))).isEqualTo(-1); + assertThat(Timestamp.create(-25, 42).compareTo(Timestamp.create(-24, 42))).isEqualTo(-1); + assertThat(Timestamp.create(-24, 45).compareTo(Timestamp.create(-24, 42))).isEqualTo(1); + assertThat(Timestamp.create(-24, 42).compareTo(Timestamp.create(-25, 42))).isEqualTo(1); + assertThat(Timestamp.create(-24, 42).compareTo(Timestamp.create(-24, 45))).isEqualTo(-1); + } + + @Test + public void timestamp_Equal() { + // Positive tests. + assertThat(Timestamp.create(0, 0)).isEqualTo(Timestamp.create(0, 0)); + assertThat(Timestamp.create(24, 42)).isEqualTo(Timestamp.create(24, 42)); + assertThat(Timestamp.create(-24, 42)).isEqualTo(Timestamp.create(-24, 42)); + // Negative tests. + assertThat(Timestamp.create(25, 42)).isNotEqualTo(Timestamp.create(24, 42)); + assertThat(Timestamp.create(24, 43)).isNotEqualTo(Timestamp.create(24, 42)); + assertThat(Timestamp.create(-25, 42)).isNotEqualTo(Timestamp.create(-24, 42)); + assertThat(Timestamp.create(-24, 43)).isNotEqualTo(Timestamp.create(-24, 42)); + } +} |