|
19 | 19 | import static com.google.common.truth.Truth.assertThat; |
20 | 20 | import static io.grpc.internal.GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE; |
21 | 21 | import static org.junit.Assert.assertEquals; |
22 | | -import static org.junit.Assert.assertFalse; |
23 | | -import static org.junit.Assert.assertNotNull; |
24 | 22 | import static org.junit.Assert.assertNull; |
25 | 23 | import static org.junit.Assert.assertThrows; |
26 | 24 | import static org.junit.Assert.assertTrue; |
|
37 | 35 | import com.google.common.io.ByteStreams; |
38 | 36 | import com.google.common.primitives.Bytes; |
39 | 37 | import io.grpc.Codec; |
40 | | -import io.grpc.Decompressor; |
41 | 38 | import io.grpc.InternalChannelz.TransportStats; |
42 | 39 | import io.grpc.StatusRuntimeException; |
43 | 40 | import io.grpc.StreamTracer; |
44 | | -import io.grpc.internal.MessageDeframer.LazyDecompressingInputStream; |
45 | 41 | import io.grpc.internal.MessageDeframer.Listener; |
46 | 42 | import io.grpc.internal.MessageDeframer.SizeEnforcingInputStream; |
47 | 43 | import io.grpc.internal.testing.TestStreamTracer.TestBaseStreamTracer; |
|
56 | 52 | import java.util.List; |
57 | 53 | import java.util.Locale; |
58 | 54 | import java.util.concurrent.TimeUnit; |
59 | | -import java.util.concurrent.atomic.AtomicBoolean; |
60 | 55 | import java.util.zip.GZIPOutputStream; |
61 | 56 | import org.junit.Before; |
62 | 57 | import org.junit.Test; |
@@ -318,75 +313,6 @@ public void compressed() { |
318 | 313 | verifyNoMoreInteractions(listener); |
319 | 314 | } |
320 | 315 |
|
321 | | - @Test |
322 | | - public void compressed_lazyDecompression() throws IOException { |
323 | | - final AtomicBoolean decompressCalled = new AtomicBoolean(false); |
324 | | - Decompressor countingDecompressor = new Decompressor() { |
325 | | - @Override |
326 | | - public String getMessageEncoding() { |
327 | | - return "gzip"; |
328 | | - } |
329 | | - |
330 | | - @Override |
331 | | - public InputStream decompress(InputStream is) throws IOException { |
332 | | - decompressCalled.set(true); |
333 | | - return new Codec.Gzip().decompress(is); |
334 | | - } |
335 | | - }; |
336 | | - |
337 | | - deframer = new MessageDeframer(listener, countingDecompressor, DEFAULT_MAX_MESSAGE_SIZE, |
338 | | - statsTraceCtx, transportTracer); |
339 | | - deframer.request(1); |
340 | | - |
341 | | - byte[] payload = compress(new byte[1000]); |
342 | | - byte[] header = new byte[]{1, 0, 0, 0, (byte) payload.length}; |
343 | | - deframer.deframe(buffer(Bytes.concat(header, payload))); |
344 | | - |
345 | | - verify(listener).messagesAvailable(producer.capture()); |
346 | | - InputStream stream = producer.getValue().next(); |
347 | | - assertNotNull(stream); |
348 | | - |
349 | | - // Decompressor should not be invoked before bytes are read |
350 | | - assertFalse(decompressCalled.get()); |
351 | | - |
352 | | - // Reading a byte triggers decompression |
353 | | - assertEquals(0, stream.read()); |
354 | | - assertTrue(decompressCalled.get()); |
355 | | - } |
356 | | - |
357 | | - @Test |
358 | | - public void compressed_closeWithoutReading_noDecompression() throws IOException { |
359 | | - final AtomicBoolean decompressCalled = new AtomicBoolean(false); |
360 | | - Decompressor countingDecompressor = new Decompressor() { |
361 | | - @Override |
362 | | - public String getMessageEncoding() { |
363 | | - return "gzip"; |
364 | | - } |
365 | | - |
366 | | - @Override |
367 | | - public InputStream decompress(InputStream is) throws IOException { |
368 | | - decompressCalled.set(true); |
369 | | - return new Codec.Gzip().decompress(is); |
370 | | - } |
371 | | - }; |
372 | | - |
373 | | - deframer = new MessageDeframer(listener, countingDecompressor, DEFAULT_MAX_MESSAGE_SIZE, |
374 | | - statsTraceCtx, transportTracer); |
375 | | - deframer.request(1); |
376 | | - |
377 | | - byte[] payload = compress(new byte[1000]); |
378 | | - byte[] header = new byte[]{1, 0, 0, 0, (byte) payload.length}; |
379 | | - deframer.deframe(buffer(Bytes.concat(header, payload))); |
380 | | - |
381 | | - verify(listener).messagesAvailable(producer.capture()); |
382 | | - InputStream stream = producer.getValue().next(); |
383 | | - assertNotNull(stream); |
384 | | - |
385 | | - // Closing without reading should not decompress |
386 | | - stream.close(); |
387 | | - assertFalse(decompressCalled.get()); |
388 | | - } |
389 | | - |
390 | 316 | @Test |
391 | 317 | public void deliverIsReentrantSafe() { |
392 | 318 | doAnswer( |
@@ -567,82 +493,6 @@ public void sizeEnforcingInputStream_markReset() throws IOException { |
567 | 493 | } |
568 | 494 | } |
569 | 495 |
|
570 | | - @RunWith(JUnit4.class) |
571 | | - public static class LazyDecompressingInputStreamTests { |
572 | | - private TestBaseStreamTracer tracer = new TestBaseStreamTracer(); |
573 | | - private StatsTraceContext statsTraceCtx = new StatsTraceContext(new StreamTracer[]{tracer}); |
574 | | - |
575 | | - @Test |
576 | | - public void lazyDecompressingInputStream_doesNotInitializeUntilRead() throws IOException { |
577 | | - final AtomicBoolean decompressCalled = new AtomicBoolean(false); |
578 | | - Decompressor countingDecompressor = new Decompressor() { |
579 | | - @Override |
580 | | - public String getMessageEncoding() { |
581 | | - return "gzip"; |
582 | | - } |
583 | | - |
584 | | - @Override |
585 | | - public InputStream decompress(InputStream is) throws IOException { |
586 | | - decompressCalled.set(true); |
587 | | - return new Codec.Gzip().decompress(is); |
588 | | - } |
589 | | - }; |
590 | | - |
591 | | - ByteArrayInputStream in = |
592 | | - new ByteArrayInputStream(compress("hello".getBytes(StandardCharsets.UTF_8))); |
593 | | - LazyDecompressingInputStream stream = new LazyDecompressingInputStream( |
594 | | - in, 100, statsTraceCtx, countingDecompressor); |
595 | | - |
596 | | - assertFalse(stream.markSupported()); |
597 | | - stream.mark(10); |
598 | | - assertThrows(IOException.class, () -> stream.reset()); |
599 | | - assertFalse(decompressCalled.get()); |
600 | | - byte[] buf = new byte[5]; |
601 | | - int read = stream.read(buf); |
602 | | - assertEquals(5, read); |
603 | | - assertEquals("hello", new String(buf, StandardCharsets.UTF_8)); |
604 | | - assertTrue(decompressCalled.get()); |
605 | | - stream.close(); |
606 | | - } |
607 | | - |
608 | | - @Test |
609 | | - public void lazyDecompressingInputStream_closeWithoutRead() throws IOException { |
610 | | - final AtomicBoolean decompressCalled = new AtomicBoolean(false); |
611 | | - final AtomicBoolean inClosed = new AtomicBoolean(false); |
612 | | - Decompressor countingDecompressor = new Decompressor() { |
613 | | - @Override |
614 | | - public String getMessageEncoding() { |
615 | | - return "gzip"; |
616 | | - } |
617 | | - |
618 | | - @Override |
619 | | - public InputStream decompress(InputStream is) throws IOException { |
620 | | - decompressCalled.set(true); |
621 | | - return new Codec.Gzip().decompress(is); |
622 | | - } |
623 | | - }; |
624 | | - |
625 | | - ByteArrayInputStream in = |
626 | | - new ByteArrayInputStream(compress("hello".getBytes(StandardCharsets.UTF_8))) { |
627 | | - @Override |
628 | | - public void close() throws IOException { |
629 | | - inClosed.set(true); |
630 | | - super.close(); |
631 | | - } |
632 | | - }; |
633 | | - LazyDecompressingInputStream stream = new LazyDecompressingInputStream( |
634 | | - in, 100, statsTraceCtx, countingDecompressor); |
635 | | - |
636 | | - assertFalse(decompressCalled.get()); |
637 | | - stream.close(); |
638 | | - assertTrue(inClosed.get()); |
639 | | - assertFalse(decompressCalled.get()); |
640 | | - |
641 | | - // Reading after close should throw IOException |
642 | | - assertThrows(IOException.class, () -> stream.read()); |
643 | | - } |
644 | | - } |
645 | | - |
646 | 496 | /** |
647 | 497 | * Verify stats were published through the tracer. |
648 | 498 | * |
|
0 commit comments