Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit d1e5e9f

Browse files
authored
Merge pull request #758 from sebright/serialization-exception
Make TagContextBinarySerializer.toByteArray throw a checked exception.
2 parents ff43823 + 6f5b661 commit d1e5e9f

File tree

6 files changed

+103
-8
lines changed

6 files changed

+103
-8
lines changed

api/src/main/java/io/opencensus/tags/propagation/TagContextBinarySerializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ public abstract class TagContextBinarySerializer {
3434
*
3535
* @param tags the {@code TagContext} to serialize.
3636
* @return the on-the-wire representation of a {@code TagContext}.
37+
* @throws TagContextSerializationException if the result would be larger than 8192 bytes.
3738
*/
38-
public abstract byte[] toByteArray(TagContext tags);
39+
public abstract byte[] toByteArray(TagContext tags) throws TagContextSerializationException;
3940

4041
/**
4142
* Creates a {@code TagContext} from the given on-the-wire encoded representation.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2017, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.tags.propagation;
18+
19+
import io.opencensus.tags.TagContext;
20+
21+
/** Exception thrown when a {@link TagContext} cannot be serialized. */
22+
public final class TagContextSerializationException extends Exception {
23+
private static final long serialVersionUID = 0L;
24+
25+
/**
26+
* Constructs a new {@code TagContextSerializationException} with the given message.
27+
*
28+
* @param message a message describing the error.
29+
*/
30+
public TagContextSerializationException(String message) {
31+
super(message);
32+
}
33+
34+
/**
35+
* Constructs a new {@code TagContextSerializationException} with the given message and cause.
36+
*
37+
* @param message a message describing the error.
38+
* @param cause the cause of the error.
39+
*/
40+
public TagContextSerializationException(String message, Throwable cause) {
41+
super(message, cause);
42+
}
43+
}

api/src/test/java/io/opencensus/tags/NoopTagsTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.opencensus.internal.NoopScope;
2323
import io.opencensus.tags.propagation.TagContextBinarySerializer;
2424
import io.opencensus.tags.propagation.TagContextParseException;
25+
import io.opencensus.tags.propagation.TagContextSerializationException;
2526
import java.util.Arrays;
2627
import java.util.Iterator;
2728
import org.junit.Rule;
@@ -129,15 +130,17 @@ public void noopTagPropagationComponent() {
129130
}
130131

131132
@Test
132-
public void noopTagContextBinarySerializer() throws TagContextParseException {
133+
public void noopTagContextBinarySerializer()
134+
throws TagContextParseException, TagContextSerializationException {
133135
assertThat(NoopTags.getNoopTagContextBinarySerializer().toByteArray(TAG_CONTEXT))
134136
.isEqualTo(new byte[0]);
135137
assertThat(NoopTags.getNoopTagContextBinarySerializer().fromByteArray(new byte[5]))
136138
.isEqualTo(NoopTags.getNoopTagContext());
137139
}
138140

139141
@Test
140-
public void noopTagContextBinarySerializer_ToByteArray_DisallowsNull() {
142+
public void noopTagContextBinarySerializer_ToByteArray_DisallowsNull()
143+
throws TagContextSerializationException {
141144
TagContextBinarySerializer noopSerializer = NoopTags.getNoopTagContextBinarySerializer();
142145
thrown.expect(NullPointerException.class);
143146
noopSerializer.toByteArray(null);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2017, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.tags.propagation;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.IOException;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.JUnit4;
25+
26+
/** Unit tests for {@link TagContextSerializationException}. */
27+
@RunWith(JUnit4.class)
28+
public final class TagContextSerializationExceptionTest {
29+
30+
@Test
31+
public void createWithMessage() {
32+
assertThat(new TagContextSerializationException("my message").getMessage())
33+
.isEqualTo("my message");
34+
}
35+
36+
@Test
37+
public void createWithMessageAndCause() {
38+
IOException cause = new IOException();
39+
TagContextSerializationException exception =
40+
new TagContextSerializationException("my message", cause);
41+
assertThat(exception.getMessage()).isEqualTo("my message");
42+
assertThat(exception.getCause()).isEqualTo(cause);
43+
}
44+
}

core_impl/src/test/java/io/opencensus/implcore/tags/propagation/TagContextBinarySerializerImplTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.opencensus.tags.TagsComponent;
3030
import io.opencensus.tags.propagation.TagContextBinarySerializer;
3131
import io.opencensus.tags.propagation.TagContextParseException;
32+
import io.opencensus.tags.propagation.TagContextSerializationException;
3233
import java.util.Iterator;
3334
import org.junit.Test;
3435
import org.junit.runner.RunWith;
@@ -56,13 +57,13 @@ public Iterator<Tag> getIterator() {
5657
};
5758

5859
@Test
59-
public void toByteArray_TaggingDisabled() {
60+
public void toByteArray_TaggingDisabled() throws TagContextSerializationException {
6061
tagsComponent.setState(TaggingState.DISABLED);
6162
assertThat(serializer.toByteArray(tagContext)).isEmpty();
6263
}
6364

6465
@Test
65-
public void toByteArray_TaggingReenabled() {
66+
public void toByteArray_TaggingReenabled() throws TagContextSerializationException {
6667
final byte[] serialized = serializer.toByteArray(tagContext);
6768
tagsComponent.setState(TaggingState.DISABLED);
6869
assertThat(serializer.toByteArray(tagContext)).isEmpty();
@@ -71,14 +72,16 @@ public void toByteArray_TaggingReenabled() {
7172
}
7273

7374
@Test
74-
public void fromByteArray_TaggingDisabled() throws TagContextParseException {
75+
public void fromByteArray_TaggingDisabled()
76+
throws TagContextParseException, TagContextSerializationException {
7577
byte[] serialized = serializer.toByteArray(tagContext);
7678
tagsComponent.setState(TaggingState.DISABLED);
7779
assertThat(TagsTestUtil.tagContextToList(serializer.fromByteArray(serialized))).isEmpty();
7880
}
7981

8082
@Test
81-
public void fromByteArray_TaggingReenabled() throws TagContextParseException {
83+
public void fromByteArray_TaggingReenabled()
84+
throws TagContextParseException, TagContextSerializationException {
8285
byte[] serialized = serializer.toByteArray(tagContext);
8386
tagsComponent.setState(TaggingState.DISABLED);
8487
assertThat(TagsTestUtil.tagContextToList(serializer.fromByteArray(serialized))).isEmpty();

core_impl/src/test/java/io/opencensus/implcore/tags/propagation/TagContextSerializationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.opencensus.tags.Tagger;
3030
import io.opencensus.tags.TagsComponent;
3131
import io.opencensus.tags.propagation.TagContextBinarySerializer;
32+
import io.opencensus.tags.propagation.TagContextSerializationException;
3233
import java.io.ByteArrayOutputStream;
3334
import java.io.IOException;
3435
import java.util.Arrays;
@@ -85,7 +86,7 @@ public void testSerializeWithMultipleTags() throws Exception {
8586
testSerialize(T1, T2, T3, T4);
8687
}
8788

88-
private void testSerialize(Tag... tags) throws IOException {
89+
private void testSerialize(Tag... tags) throws IOException, TagContextSerializationException {
8990
TagContextBuilder builder = tagger.emptyBuilder();
9091
for (Tag tag : tags) {
9192
builder.put(tag.getKey(), tag.getValue());

0 commit comments

Comments
 (0)