|
11 | 11 | import static org.mockito.Mockito.doThrow; |
12 | 12 | import static org.mockito.Mockito.mock; |
13 | 13 |
|
| 14 | +import io.opentelemetry.api.common.AttributeKey; |
| 15 | +import io.opentelemetry.api.common.Attributes; |
| 16 | +import java.io.ByteArrayOutputStream; |
14 | 17 | import java.io.IOException; |
15 | 18 | import java.io.OutputStream; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.stream.Stream; |
16 | 21 | import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.Arguments; |
| 24 | +import org.junit.jupiter.params.provider.MethodSource; |
17 | 25 |
|
18 | 26 | class MarshalerTest { |
19 | 27 |
|
@@ -47,4 +55,123 @@ protected void writeTo(Serializer output) throws IOException { |
47 | 55 | assertThatThrownBy(() -> marshaler.writeBinaryTo(os)).isInstanceOf(IOException.class); |
48 | 56 | assertThatThrownBy(() -> marshaler.writeJsonTo(os)).isInstanceOf(IOException.class); |
49 | 57 | } |
| 58 | + |
| 59 | + /** |
| 60 | + * This test ensures that instances where serializer produces runtime exceptions are properly |
| 61 | + * converted back to checked {@link IOException}. |
| 62 | + * |
| 63 | + * <p>At various points in {@link Serializer}, we use {@code .forEach}-style methods which accept |
| 64 | + * {@link java.util.function.Consumer} and {@link java.util.function.BiConsumer}. These consumers |
| 65 | + * that any checked exceptions are caught and rethrown as runtime exceptions. Its essential that |
| 66 | + * these runtime exceptions are re-converted back to checked {@link IOException} because calling |
| 67 | + * code's error handling is built on top of {@link IOException}. Failure to convert to {@link |
| 68 | + * IOException} leads to issues like this: <a |
| 69 | + * href="https://github.com/open-telemetry/opentelemetry-java/issues/6946">#6946</a>. |
| 70 | + */ |
| 71 | + @ParameterizedTest |
| 72 | + @MethodSource("writeToArgs") |
| 73 | + void writeTo_NoRuntimeExceptions(Writer writer) throws IOException { |
| 74 | + Marshaler marshaler = |
| 75 | + new Marshaler() { |
| 76 | + @Override |
| 77 | + public int getBinarySerializedSize() { |
| 78 | + return 0; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected void writeTo(Serializer output) throws IOException { |
| 83 | + writer.writeTo(output); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 88 | + assertThatThrownBy(() -> marshaler.writeBinaryTo(baos)).isInstanceOf(IOException.class); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static Stream<Arguments> writeToArgs() { |
| 93 | + ProtoFieldInfo fieldInfo = ProtoFieldInfo.create(0, 0, "name"); |
| 94 | + MarshalerContext context = new MarshalerContext(); |
| 95 | + ThrowingMarshaler<String> throwingMarshaler = new ThrowingMarshaler<>(new IOException("error")); |
| 96 | + ThrowingMarshaler2<String, String> throwingMarshaler2 = |
| 97 | + new ThrowingMarshaler2<>(new IOException("error")); |
| 98 | + ThrowingMarshaler2<AttributeKey<?>, Object> throwingMarshaler2Attributes = |
| 99 | + new ThrowingMarshaler2<>(new IOException("error")); |
| 100 | + |
| 101 | + return Stream.of( |
| 102 | + Arguments.of( |
| 103 | + asWriter( |
| 104 | + output -> |
| 105 | + output.serializeRepeatedMessageWithContext( |
| 106 | + fieldInfo, |
| 107 | + Collections.singleton("value"), |
| 108 | + throwingMarshaler, |
| 109 | + context, |
| 110 | + MarshalerContext.key()))), |
| 111 | + Arguments.of( |
| 112 | + asWriter( |
| 113 | + output -> |
| 114 | + output.serializeRepeatedMessageWithContext( |
| 115 | + fieldInfo, |
| 116 | + Collections.singletonMap("key", "value"), |
| 117 | + throwingMarshaler2, |
| 118 | + context, |
| 119 | + MarshalerContext.key()))), |
| 120 | + Arguments.of( |
| 121 | + asWriter( |
| 122 | + output -> |
| 123 | + output.serializeRepeatedMessageWithContext( |
| 124 | + fieldInfo, |
| 125 | + Attributes.builder().put("key", "value").build(), |
| 126 | + throwingMarshaler2Attributes, |
| 127 | + context)))); |
| 128 | + } |
| 129 | + |
| 130 | + private static Writer asWriter(Writer writer) { |
| 131 | + return writer; |
| 132 | + } |
| 133 | + |
| 134 | + @FunctionalInterface |
| 135 | + private interface Writer { |
| 136 | + void writeTo(Serializer output) throws IOException; |
| 137 | + } |
| 138 | + |
| 139 | + private static class ThrowingMarshaler<T> implements StatelessMarshaler<T> { |
| 140 | + |
| 141 | + private final IOException exception; |
| 142 | + |
| 143 | + private ThrowingMarshaler(IOException exception) { |
| 144 | + this.exception = exception; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public int getBinarySerializedSize(T value, MarshalerContext context) { |
| 149 | + return 0; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void writeTo(Serializer output, T value, MarshalerContext context) throws IOException { |
| 154 | + throw exception; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private static class ThrowingMarshaler2<K, V> implements StatelessMarshaler2<K, V> { |
| 159 | + |
| 160 | + private final IOException exception; |
| 161 | + |
| 162 | + private ThrowingMarshaler2(IOException exception) { |
| 163 | + this.exception = exception; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public int getBinarySerializedSize(K key, V value, MarshalerContext context) { |
| 168 | + return 0; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void writeTo(Serializer output, K key, V value, MarshalerContext context) |
| 173 | + throws IOException { |
| 174 | + throw exception; |
| 175 | + } |
| 176 | + } |
50 | 177 | } |
0 commit comments