|
| 1 | +/* |
| 2 | + * Copyright © 2022 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.encoding.netty; |
| 17 | + |
| 18 | +import io.servicetalk.buffer.api.Buffer; |
| 19 | +import io.servicetalk.buffer.api.BufferAllocator; |
| 20 | +import io.servicetalk.buffer.api.CompositeBuffer; |
| 21 | +import io.servicetalk.concurrent.api.Publisher; |
| 22 | +import io.servicetalk.serializer.api.SerializerDeserializer; |
| 23 | +import io.servicetalk.serializer.api.StreamingSerializerDeserializer; |
| 24 | + |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.EnumSource; |
| 27 | + |
| 28 | +import java.util.concurrent.ThreadLocalRandom; |
| 29 | + |
| 30 | +import static io.servicetalk.buffer.api.ReadOnlyBufferAllocators.DEFAULT_RO_ALLOCATOR; |
| 31 | +import static io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR; |
| 32 | +import static io.servicetalk.concurrent.api.Publisher.from; |
| 33 | +import static io.servicetalk.encoding.netty.NettyCompression.deflateDefault; |
| 34 | +import static io.servicetalk.encoding.netty.NettyCompression.deflateDefaultStreaming; |
| 35 | +import static io.servicetalk.encoding.netty.NettyCompression.gzipDefault; |
| 36 | +import static io.servicetalk.encoding.netty.NettyCompression.gzipDefaultStreaming; |
| 37 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 38 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | +import static org.hamcrest.Matchers.equalTo; |
| 40 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 41 | + |
| 42 | +class NettyCompressionStreamingSerializerTest { |
| 43 | + enum StreamingType { |
| 44 | + GZIP(gzipDefaultStreaming(), DEFAULT_ALLOCATOR), |
| 45 | + DEFLATE(deflateDefaultStreaming(), DEFAULT_ALLOCATOR), |
| 46 | + GZIP_RO(gzipDefaultStreaming(), DEFAULT_RO_ALLOCATOR), |
| 47 | + DEFLATE_RO(deflateDefaultStreaming(), DEFAULT_RO_ALLOCATOR); |
| 48 | + |
| 49 | + final StreamingSerializerDeserializer<Buffer> serializer; |
| 50 | + final BufferAllocator allocator; |
| 51 | + |
| 52 | + StreamingType(StreamingSerializerDeserializer<Buffer> serializer, BufferAllocator allocator) { |
| 53 | + this.serializer = serializer; |
| 54 | + this.allocator = allocator; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + enum AggType { |
| 59 | + GZIP(gzipDefault(), DEFAULT_ALLOCATOR), |
| 60 | + DEFLATE(deflateDefault(), DEFAULT_ALLOCATOR), |
| 61 | + GZIP_RO(gzipDefault(), DEFAULT_RO_ALLOCATOR), |
| 62 | + DEFLATE_RO(deflateDefault(), DEFAULT_RO_ALLOCATOR); |
| 63 | + |
| 64 | + final SerializerDeserializer<Buffer> serializer; |
| 65 | + final BufferAllocator allocator; |
| 66 | + |
| 67 | + AggType(SerializerDeserializer<Buffer> serializer, BufferAllocator allocator) { |
| 68 | + this.serializer = serializer; |
| 69 | + this.allocator = allocator; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @ParameterizedTest(name = "{displayName} [{index}] type = {0}") |
| 74 | + @EnumSource(StreamingType.class) |
| 75 | + void streamingOverMultipleFrames(StreamingType type) throws Exception { |
| 76 | + BufferAllocator allocator = type.allocator; |
| 77 | + StreamingSerializerDeserializer<Buffer> serializer = type.serializer; |
| 78 | + String rawString = "hello"; |
| 79 | + byte[] rawBytes = new byte[1024]; |
| 80 | + ThreadLocalRandom.current().nextBytes(rawBytes); |
| 81 | + Buffer[] clearText = new Buffer[] {allocator.fromAscii(rawString), allocator.wrap(rawBytes)}; |
| 82 | + Publisher<Buffer> serializedPub = serializer.serialize(from(clearText), DEFAULT_ALLOCATOR); |
| 83 | + |
| 84 | + CompositeBuffer serializedBuf = serializedPub |
| 85 | + .collect(DEFAULT_ALLOCATOR::newCompositeBuffer, CompositeBuffer::addBuffer).toFuture().get(); |
| 86 | + |
| 87 | + assertThat(serializedBuf.readableBytes(), greaterThanOrEqualTo(4)); |
| 88 | + CompositeBuffer result = serializer.deserialize(from( |
| 89 | + // Copy so we feed in data from the allocator of choice. |
| 90 | + allocator.wrap(readAndCopy(serializedBuf, 4)), |
| 91 | + allocator.wrap(readAndCopy(serializedBuf, serializedBuf.readableBytes())) |
| 92 | + ), DEFAULT_ALLOCATOR) |
| 93 | + .collect(DEFAULT_ALLOCATOR::newCompositeBuffer, CompositeBuffer::addBuffer).toFuture().get(); |
| 94 | + |
| 95 | + assertThat(result.readableBytes(), equalTo(rawString.length() + rawBytes.length)); |
| 96 | + assertThat(result.readBytes(rawString.length()).toString(UTF_8), equalTo(rawString)); |
| 97 | + assertThat(result, equalTo(allocator.wrap(rawBytes))); |
| 98 | + } |
| 99 | + |
| 100 | + @ParameterizedTest(name = "{displayName} [{index}] type = {0}") |
| 101 | + @EnumSource(AggType.class) |
| 102 | + void aggregatedRoundTrip(AggType type) { |
| 103 | + BufferAllocator allocator = type.allocator; |
| 104 | + SerializerDeserializer<Buffer> serializer = type.serializer; |
| 105 | + byte[] rawBytes = new byte[1024]; |
| 106 | + ThreadLocalRandom.current().nextBytes(rawBytes); |
| 107 | + Buffer serializedBuf = serializer.serialize(allocator.wrap(rawBytes), DEFAULT_ALLOCATOR); |
| 108 | + Buffer result = serializer.deserialize( |
| 109 | + allocator.wrap(readAndCopy(serializedBuf, serializedBuf.readableBytes())), |
| 110 | + DEFAULT_ALLOCATOR); |
| 111 | + |
| 112 | + assertThat(readAndCopy(result, result.readableBytes()), equalTo(rawBytes)); |
| 113 | + } |
| 114 | + |
| 115 | + private static byte[] readAndCopy(Buffer buffer, int byteCount) { |
| 116 | + byte[] bytes = new byte[byteCount]; |
| 117 | + buffer.readBytes(bytes); |
| 118 | + return bytes; |
| 119 | + } |
| 120 | +} |
0 commit comments