|
| 1 | +/* |
| 2 | + * Copyright © 2021 Apple Inc. and the ServiceTalk project 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 | +package io.servicetalk.data.protobuf; |
| 17 | + |
| 18 | +import io.servicetalk.buffer.api.Buffer; |
| 19 | +import io.servicetalk.serializer.utils.VarIntLengthStreamingSerializer; |
| 20 | +import io.servicetalk.serializer.utils.test.TestProtos.DummyMessage; |
| 21 | + |
| 22 | +import com.google.protobuf.Parser; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.Arguments; |
| 25 | +import org.junit.jupiter.params.provider.MethodSource; |
| 26 | + |
| 27 | +import java.io.OutputStream; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Collection; |
| 30 | +import java.util.stream.Stream; |
| 31 | + |
| 32 | +import static io.servicetalk.buffer.api.Buffer.asInputStream; |
| 33 | +import static io.servicetalk.buffer.api.Buffer.asOutputStream; |
| 34 | +import static io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR; |
| 35 | +import static io.servicetalk.concurrent.api.Publisher.from; |
| 36 | +import static io.servicetalk.concurrent.api.Publisher.fromIterable; |
| 37 | +import static io.servicetalk.serializer.utils.test.TestProtos.DummyMessage.newBuilder; |
| 38 | +import static io.servicetalk.serializer.utils.test.TestProtos.DummyMessage.parser; |
| 39 | +import static java.util.Arrays.asList; |
| 40 | +import static java.util.Collections.singletonList; |
| 41 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 42 | +import static org.hamcrest.Matchers.contains; |
| 43 | + |
| 44 | +class VarIntLengthStreamingSerializerTest { |
| 45 | + @ParameterizedTest(name = "pojos={0}") |
| 46 | + @MethodSource("pojos") |
| 47 | + void protobufWriteDelimitedToDeserialized(Collection<DummyMessage> msgs) throws Exception { |
| 48 | + Parser<DummyMessage> parser = parser(); |
| 49 | + VarIntLengthStreamingSerializer<DummyMessage> serializer = new VarIntLengthStreamingSerializer<>( |
| 50 | + ProtobufSerializerCache.INSTANCE.serializerDeserializer(parser), DummyMessage::getSerializedSize); |
| 51 | + |
| 52 | + Buffer buffer = DEFAULT_ALLOCATOR.newBuffer(); |
| 53 | + OutputStream os = asOutputStream(buffer); |
| 54 | + for (DummyMessage msg : msgs) { |
| 55 | + msg.writeDelimitedTo(os); |
| 56 | + } |
| 57 | + |
| 58 | + assertThat(serializer.deserialize(from(buffer), DEFAULT_ALLOCATOR).toFuture().get(), contains(msgs.toArray())); |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest(name = "pojos={0}") |
| 62 | + @MethodSource("pojos") |
| 63 | + void protobufParseDelimitedFromSerialized(Collection<DummyMessage> msgs) throws Exception { |
| 64 | + Parser<DummyMessage> parser = parser(); |
| 65 | + VarIntLengthStreamingSerializer<DummyMessage> serializer = new VarIntLengthStreamingSerializer<>( |
| 66 | + ProtobufSerializerCache.INSTANCE.serializerDeserializer(parser), DummyMessage::getSerializedSize); |
| 67 | + |
| 68 | + Collection<Buffer> serialized = serializer.serialize(fromIterable(msgs), DEFAULT_ALLOCATOR) |
| 69 | + .toFuture().get(); |
| 70 | + |
| 71 | + Collection<DummyMessage> deserialized = new ArrayList<>(serialized.size()); |
| 72 | + for (Buffer buf : serialized) { |
| 73 | + deserialized.add(parser.parseDelimitedFrom(asInputStream(buf))); |
| 74 | + } |
| 75 | + assertThat(deserialized, contains(msgs.toArray())); |
| 76 | + } |
| 77 | + |
| 78 | + @SuppressWarnings("unused") |
| 79 | + private static Stream<Arguments> pojos() { |
| 80 | + return Stream.of( |
| 81 | + Arguments.of(singletonList(newMsg("hello"))), |
| 82 | + Arguments.of(asList(newMsg("hello"), newMsg("world"))), |
| 83 | + Arguments.of(asList(newMsg("hello"), newMsg("world"), newMsg("!"))) |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + private static DummyMessage newMsg(String msg) { |
| 88 | + return newBuilder().setMessage(msg).build(); |
| 89 | + } |
| 90 | +} |
0 commit comments