Skip to content

Commit 313c05d

Browse files
committed
add tests for varint compatability with parseDelimitedFrom and writeDelimitedTo
1 parent 129ea69 commit 313c05d

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

servicetalk-data-protobuf/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies {
4343
testImplementation project(":servicetalk-test-resources")
4444
testImplementation project(":servicetalk-buffer-netty")
4545
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5Version"
46+
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit5Version"
4647
testImplementation "org.hamcrest:hamcrest-library:$hamcrestVersion"
4748
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"
4849

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.data.protobuf.test.TestProtos.DummyMessage;
20+
import io.servicetalk.serializer.utils.VarIntLengthStreamingSerializer;
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.Arrays;
30+
import java.util.Collection;
31+
import java.util.List;
32+
import java.util.stream.Stream;
33+
34+
import static io.servicetalk.buffer.api.Buffer.asInputStream;
35+
import static io.servicetalk.buffer.api.Buffer.asOutputStream;
36+
import static io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR;
37+
import static io.servicetalk.concurrent.api.Publisher.from;
38+
import static io.servicetalk.concurrent.api.Publisher.fromIterable;
39+
import static io.servicetalk.data.protobuf.test.TestProtos.DummyMessage.parser;
40+
import static java.util.Arrays.asList;
41+
import static java.util.Collections.singletonList;
42+
import static org.hamcrest.MatcherAssert.assertThat;
43+
import static org.hamcrest.Matchers.contains;
44+
45+
class VarIntLengthStreamingSerializerTest {
46+
private static final List<Arguments> POJOS = Arrays.asList(
47+
Arguments.of(singletonList(newMsg("hello"))),
48+
Arguments.of(asList(newMsg("hello"), newMsg("world"))),
49+
Arguments.of(asList(newMsg("hello"), newMsg("world"), newMsg("!"))),
50+
Arguments.of(asList(newMsg("hello"), newMsg(1 << 7))),
51+
Arguments.of(asList(newMsg(1 << 14), newMsg("!"))),
52+
Arguments.of(singletonList(newMsg(1 << 21))),
53+
Arguments.of(singletonList(newMsg(1 << 28)))
54+
);
55+
56+
@ParameterizedTest(name = "pojos={0}")
57+
@MethodSource("pojos")
58+
void protobufWriteDelimitedToDeserialized(Collection<DummyMessage> msgs) throws Exception {
59+
Parser<DummyMessage> parser = DummyMessage.parser();
60+
VarIntLengthStreamingSerializer<DummyMessage> serializer = new VarIntLengthStreamingSerializer<>(
61+
ProtobufSerializerCache.INSTANCE.serializerDeserializer(parser), DummyMessage::getSerializedSize);
62+
63+
Buffer buffer = DEFAULT_ALLOCATOR.newBuffer();
64+
OutputStream os = asOutputStream(buffer);
65+
for (DummyMessage msg : msgs) {
66+
msg.writeDelimitedTo(os);
67+
}
68+
69+
assertThat(serializer.deserialize(from(buffer), DEFAULT_ALLOCATOR).toFuture().get(), contains(msgs.toArray()));
70+
}
71+
72+
@ParameterizedTest(name = "pojos={0}")
73+
@MethodSource("pojos")
74+
void protobufParseDelimitedFromSerialized(Collection<DummyMessage> msgs) throws Exception {
75+
Parser<DummyMessage> parser = parser();
76+
VarIntLengthStreamingSerializer<DummyMessage> serializer = new VarIntLengthStreamingSerializer<>(
77+
ProtobufSerializerCache.INSTANCE.serializerDeserializer(parser), DummyMessage::getSerializedSize);
78+
79+
Collection<Buffer> serialized = serializer.serialize(fromIterable(msgs), DEFAULT_ALLOCATOR)
80+
.toFuture().get();
81+
82+
Collection<DummyMessage> deserialized = new ArrayList<>(serialized.size());
83+
for (Buffer buf : serialized) {
84+
deserialized.add(parser.parseDelimitedFrom(asInputStream(buf)));
85+
}
86+
assertThat(deserialized, contains(msgs.toArray()));
87+
}
88+
89+
@SuppressWarnings("unused")
90+
private static Stream<Arguments> pojos() {
91+
return POJOS.stream();
92+
}
93+
94+
private static DummyMessage newMsg(String msg) {
95+
return DummyMessage.newBuilder().setMessage(msg).build();
96+
}
97+
98+
private static DummyMessage newMsg(int length) {
99+
StringBuilder sb = new StringBuilder(length);
100+
for (int i = 0; i < length; ++i) {
101+
sb.append('a');
102+
}
103+
return newMsg(sb.toString());
104+
}
105+
}

0 commit comments

Comments
 (0)