|
23 | 23 | import static org.junit.Assert.assertTrue; |
24 | 24 |
|
25 | 25 | import com.google.cloud.storage.GrpcStorageOptions.ZeroCopyResponseMarshaller; |
| 26 | +import com.google.cloud.storage.ResponseContentLifecycleHandle.ChildRef; |
| 27 | +import com.google.cloud.storage.it.ChecksummedTestContent; |
26 | 28 | import com.google.common.collect.ImmutableList; |
27 | 29 | import com.google.common.hash.Hashing; |
28 | 30 | import com.google.protobuf.ByteString; |
| 31 | +import com.google.storage.v2.ChecksummedData; |
29 | 32 | import com.google.storage.v2.ContentRange; |
30 | 33 | import com.google.storage.v2.Object; |
31 | 34 | import com.google.storage.v2.ObjectChecksums; |
32 | 35 | import com.google.storage.v2.ReadObjectResponse; |
| 36 | +import io.grpc.Detachable; |
| 37 | +import io.grpc.HasByteBuffer; |
| 38 | +import io.grpc.KnownLength; |
33 | 39 | import io.grpc.StatusRuntimeException; |
34 | 40 | import io.grpc.internal.ReadableBuffer; |
35 | 41 | import io.grpc.internal.ReadableBuffers; |
|
41 | 47 | import java.util.Arrays; |
42 | 48 | import java.util.List; |
43 | 49 | import java.util.concurrent.atomic.AtomicBoolean; |
| 50 | +import java.util.concurrent.atomic.AtomicInteger; |
44 | 51 | import java.util.stream.Collectors; |
| 52 | +import javax.annotation.Nullable; |
45 | 53 | import org.junit.Test; |
46 | 54 |
|
47 | 55 | public class ZeroCopyMarshallerTest { |
@@ -71,10 +79,11 @@ private byte[] dropLastOneByte(byte[] bytes) { |
71 | 79 | return Arrays.copyOfRange(bytes, 0, bytes.length - 1); |
72 | 80 | } |
73 | 81 |
|
74 | | - private InputStream createInputStream(byte[] bytes, boolean isZeroCopyable) { |
| 82 | + private <IS extends InputStream & KnownLength & Detachable & HasByteBuffer> IS createInputStream( |
| 83 | + byte[] bytes, boolean isZeroCopyable) { |
75 | 84 | ReadableBuffer buffer = |
76 | 85 | isZeroCopyable ? ReadableBuffers.wrap(ByteBuffer.wrap(bytes)) : ReadableBuffers.wrap(bytes); |
77 | | - return ReadableBuffers.openStream(buffer, true); |
| 86 | + return (IS) ReadableBuffers.openStream(buffer, true); |
78 | 87 | } |
79 | 88 |
|
80 | 89 | @Test |
@@ -205,16 +214,146 @@ void onClose() throws IOException { |
205 | 214 | assertThat(messages).isEqualTo(ImmutableList.of("Kaboom stream2", "Kaboom stream3")); |
206 | 215 | } |
207 | 216 |
|
208 | | - private static class CloseAuditingInputStream extends FilterInputStream { |
| 217 | + @Test |
| 218 | + public void refCounting_closingLastBorrowedChildRefShouldCloseHandleWhenHandlePreviouslyClosed() |
| 219 | + throws IOException { |
| 220 | + try (ZeroCopyResponseMarshaller<ChecksummedData> marshaller = |
| 221 | + new ZeroCopyResponseMarshaller<>( |
| 222 | + ChecksummedData.getDefaultInstance(), |
| 223 | + cd -> cd.getContent().asReadOnlyByteBufferList())) { |
| 224 | + |
| 225 | + ChecksummedTestContent testContent = |
| 226 | + ChecksummedTestContent.of(DataGenerator.base64Characters().genBytes(17)); |
| 227 | + |
| 228 | + ChecksummedData orig = testContent.asChecksummedData(); |
| 229 | + |
| 230 | + // load our proto message into the marshaller |
| 231 | + byte[] serialized = orig.toByteArray(); |
| 232 | + CloseAuditingInputStream<?> inputStream = |
| 233 | + CloseAuditingInputStream.of(createInputStream(serialized, true)); |
| 234 | + |
| 235 | + ChecksummedData parsed = marshaller.parse(inputStream); |
| 236 | + assertThat(inputStream.closed).isFalse(); |
| 237 | + |
| 238 | + // now get the lifecycle management handle for the parsed instance |
| 239 | + ResponseContentLifecycleHandle handle = marshaller.get(parsed); |
| 240 | + assertThat(inputStream.closed).isFalse(); |
| 241 | + |
| 242 | + ChildRef ref1 = handle.borrow(); |
| 243 | + ChildRef ref2 = handle.borrow(); |
| 244 | + ChildRef ref3 = handle.borrow(); |
| 245 | + assertThat(inputStream.closed).isFalse(); |
| 246 | + handle.close(); |
| 247 | + assertThat(inputStream.closed).isFalse(); |
| 248 | + ref1.close(); |
| 249 | + assertThat(inputStream.closed).isFalse(); |
| 250 | + ref2.close(); |
| 251 | + assertThat(inputStream.closed).isFalse(); |
| 252 | + ref3.close(); |
| 253 | + assertThat(inputStream.closed).isTrue(); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + @Test |
| 258 | + public void refCounting_mustBeOpenToBorrow() throws IOException { |
| 259 | + try (ZeroCopyResponseMarshaller<ChecksummedData> marshaller = |
| 260 | + new ZeroCopyResponseMarshaller<>( |
| 261 | + ChecksummedData.getDefaultInstance(), |
| 262 | + cd -> cd.getContent().asReadOnlyByteBufferList())) { |
| 263 | + |
| 264 | + ChecksummedTestContent testContent = |
| 265 | + ChecksummedTestContent.of(DataGenerator.base64Characters().genBytes(17)); |
| 266 | + |
| 267 | + ChecksummedData orig = testContent.asChecksummedData(); |
| 268 | + |
| 269 | + // load our proto message into the marshaller |
| 270 | + byte[] serialized = orig.toByteArray(); |
| 271 | + CloseAuditingInputStream<?> inputStream = |
| 272 | + CloseAuditingInputStream.of(createInputStream(serialized, true)); |
| 273 | + |
| 274 | + ChecksummedData parsed = marshaller.parse(inputStream); |
| 275 | + assertThat(inputStream.closed).isFalse(); |
| 276 | + |
| 277 | + // now get the lifecycle management handle for the parsed instance |
| 278 | + ResponseContentLifecycleHandle handle = marshaller.get(parsed); |
| 279 | + handle.close(); |
| 280 | + assertThat(inputStream.closed).isTrue(); |
| 281 | + |
| 282 | + assertThrows(IllegalStateException.class, handle::borrow); |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 287 | + @Test |
| 288 | + public void refCounting_handleCloseOnlyHappensIfOpen() throws IOException { |
| 289 | + try (ZeroCopyResponseMarshaller<ChecksummedData> marshaller = |
| 290 | + new ZeroCopyResponseMarshaller<>( |
| 291 | + ChecksummedData.getDefaultInstance(), |
| 292 | + cd -> cd.getContent().asReadOnlyByteBufferList())) { |
| 293 | + |
| 294 | + AtomicInteger closeCount = new AtomicInteger(0); |
| 295 | + ChecksummedTestContent testContent = |
| 296 | + ChecksummedTestContent.of(DataGenerator.base64Characters().genBytes(17)); |
| 297 | + |
| 298 | + ChecksummedData orig = testContent.asChecksummedData(); |
| 299 | + |
| 300 | + // load our proto message into the marshaller |
| 301 | + byte[] serialized = orig.toByteArray(); |
| 302 | + CloseAuditingInputStream inputStream = |
| 303 | + new CloseAuditingInputStream(createInputStream(serialized, true)) { |
| 304 | + @Override |
| 305 | + public void close() throws IOException { |
| 306 | + closeCount.getAndIncrement(); |
| 307 | + super.close(); |
| 308 | + } |
| 309 | + }; |
| 310 | + |
| 311 | + ChecksummedData parsed = marshaller.parse(inputStream); |
| 312 | + assertThat(inputStream.closed).isFalse(); |
| 313 | + |
| 314 | + // now get the lifecycle management handle for the parsed instance |
| 315 | + ResponseContentLifecycleHandle handle = marshaller.get(parsed); |
| 316 | + handle.close(); |
| 317 | + assertThat(inputStream.closed).isTrue(); |
| 318 | + handle.close(); |
| 319 | + assertThat(closeCount.get()).isEqualTo(1); |
| 320 | + } |
| 321 | + } |
| 322 | + |
| 323 | + // gRPC doesn't have a public InputStream subclass that implements all of these interfaces |
| 324 | + // use generics to constrain things using multiple inheritance notation. Then, our class |
| 325 | + // implements the same interfaces to allow use within zero-copy marshaller. |
| 326 | + private static class CloseAuditingInputStream< |
| 327 | + IS extends InputStream & KnownLength & Detachable & HasByteBuffer> |
| 328 | + extends FilterInputStream implements KnownLength, Detachable, HasByteBuffer { |
209 | 329 |
|
210 | 330 | private boolean closed = false; |
| 331 | + private final IS delegate; |
211 | 332 |
|
212 | | - private CloseAuditingInputStream(InputStream in) { |
| 333 | + private CloseAuditingInputStream(IS in) { |
213 | 334 | super(in); |
| 335 | + this.delegate = in; |
| 336 | + } |
| 337 | + |
| 338 | + @Override |
| 339 | + public InputStream detach() { |
| 340 | + return this; |
| 341 | + } |
| 342 | + |
| 343 | + @Override |
| 344 | + public boolean byteBufferSupported() { |
| 345 | + return delegate.byteBufferSupported(); |
| 346 | + } |
| 347 | + |
| 348 | + @Nullable |
| 349 | + @Override |
| 350 | + public ByteBuffer getByteBuffer() { |
| 351 | + return delegate.getByteBuffer(); |
214 | 352 | } |
215 | 353 |
|
216 | | - public static CloseAuditingInputStream of(InputStream in) { |
217 | | - return new CloseAuditingInputStream(in); |
| 354 | + public static <IS extends InputStream & KnownLength & Detachable & HasByteBuffer> |
| 355 | + CloseAuditingInputStream<IS> of(IS in) { |
| 356 | + return new CloseAuditingInputStream<>(in); |
218 | 357 | } |
219 | 358 |
|
220 | 359 | @Override |
|
0 commit comments