diff options
author | rghetia <rghetia@yahoo.com> | 2018-06-21 22:01:16 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-21 22:01:16 -0700 |
commit | bc143adc32e6db5e158485246d8d3aa6592b1cff (patch) | |
tree | 40b492607f5be764ad4f0a00163dfe61abf4d5ef /api/src/test/java/io | |
parent | 9437d455d8a6982abfaec9c7ca162693b0f23c4f (diff) | |
download | opencensus-java-bc143adc32e6db5e158485246d8d3aa6592b1cff.tar.gz |
Summary Span: Add encoder/decoder for Server Stats. (#1272)
* Summary Span: Add encoder/decoder for Server Stats.
* Fixed build errors reported by Kokoro/Travis.
* Fixed review comments.
- Added missing javadoc annotation.
- included version in encoder/decoder.
- renamed get methods for ServerStats.
* Change version from 0.15 to 0.16
- also fixed CURRENT_VERSION for encoder/decoder and added test for it.
* Make ServerStatsEncoding public.
* Add Test ServerStatsFieldEnum.Size
replace traceOption() wiht getTraceOption()
Diffstat (limited to 'api/src/test/java/io')
3 files changed, 289 insertions, 0 deletions
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); + } +} |