public DuplexRequestBody(@Nullable MediaType contentType, long pipeMaxBufferSize) { this.pipe = new Pipe(pipeMaxBufferSize); this.contentType = contentType; }
final Pipe pipe = new Pipe(1000L); // An awkward size to force producer/consumer exchange. final long totalBytes = 16L * 1024L * 1024L; ByteString expectedHash = ByteString.decodeHex("7c3b224bea749086babe079360cf29f98d88262d");
@Test public void sourceClose() throws Exception { Pipe pipe = new Pipe(100L); pipe.source().close(); try { pipe.source().read(new Buffer(), 3); fail(); } catch (IllegalStateException expected) { assertEquals("closed", expected.getMessage()); } }
@Test public void sinkFlushDoesntWaitForReader() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.sink().flush(); BufferedSource bufferedSource = Okio.buffer(pipe.source()); assertEquals("abc", bufferedSource.readUtf8(3)); }
@Test public void sourceMultipleClose() throws Exception { Pipe pipe = new Pipe(100L); pipe.source().close(); pipe.source().close(); }
@Test public void sinkWriteFailsByClosedReader() throws Exception { final Pipe pipe = new Pipe(3L); executorService.schedule(new Runnable() { @Override public void run() { try { pipe.source().close(); } catch (IOException e) { throw new AssertionError(); } } }, 1000, TimeUnit.MILLISECONDS); double start = now(); try { pipe.sink().write(new Buffer().writeUtf8("abcdef"), 6); fail(); } catch (IOException expected) { assertEquals("source is closed", expected.getMessage()); assertElapsed(1000.0, start); } }
@Test public void sinkMultipleClose() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().close(); pipe.sink().close(); }
@Test public void sourceReadUnblockedByClosedSink() throws Exception { final Pipe pipe = new Pipe(3L); executorService.schedule(new Runnable() { @Override public void run() { try { pipe.sink().close(); } catch (IOException e) { throw new AssertionError(); } } }, 1000, TimeUnit.MILLISECONDS); double start = now(); Buffer readBuffer = new Buffer(); assertEquals(-1, pipe.source().read(readBuffer, Long.MAX_VALUE)); assertEquals(0, readBuffer.size()); assertElapsed(1000.0, start); }
@Test public void sinkFlushFailsIfReaderIsClosedBeforeAllDataIsRead() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.source().close(); try { pipe.sink().flush(); fail(); } catch (IOException expected) { assertEquals("source is closed", expected.getMessage()); } }
@Test public void sinkCloseFailsIfReaderIsClosedBeforeAllDataIsRead() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.source().close(); try { pipe.sink().close(); fail(); } catch (IOException expected) { assertEquals("source is closed", expected.getMessage()); } }
@Test public void sinkCloseDoesntWaitForSourceRead() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.sink().close(); BufferedSource bufferedSource = Okio.buffer(pipe.source()); assertEquals("abc", bufferedSource.readUtf8()); assertTrue(bufferedSource.exhausted()); }
@Test public void sinkClose() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().close(); try { pipe.sink().write(new Buffer().writeUtf8("abc"), 3); fail(); } catch (IllegalStateException expected) { assertEquals("closed", expected.getMessage()); } try { pipe.sink().flush(); fail(); } catch (IllegalStateException expected) { assertEquals("closed", expected.getMessage()); } }
final Pipe pipe = new Pipe(3L); executorService.execute(new Runnable() { @Override public void run() {
@Test public void gzipSink() throws Exception { Pipe pipe = new Pipe(1024 * 1024); GzipSink gzipSink = new GzipSink(pipe.sink()); // Disable compression to speed up a slow test. Improved from 141s to 35s on one machine. gzipSink.deflater().setLevel(Deflater.NO_COMPRESSION); Future<Long> future = readAllAndCloseAsync(randomSource(FOUR_GIB_PLUS_ONE), gzipSink); HashingSink hashingSink = HashingSink.sha256(Okio.blackhole()); GZIPInputStream gzipIn = new GZIPInputStream(Okio.buffer(pipe.source()).inputStream()); readAllAndClose(Okio.source(gzipIn), hashingSink); assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get()); assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash()); }
@Test public void test() throws Exception { Pipe pipe = new Pipe(6); pipe.sink().write(new Buffer().writeUtf8("abc"), 3L); Source source = pipe.source(); Buffer readBuffer = new Buffer(); assertEquals(3L, source.read(readBuffer, 6L)); assertEquals("abc", readBuffer.readUtf8()); pipe.sink().close(); assertEquals(-1L, source.read(readBuffer, 6L)); source.close(); }
@Test public void test() throws Exception { Pipe pipe = new Pipe(1024 * 1024); Future<Long> future = readAllAndCloseAsync(randomSource(FOUR_GIB_PLUS_ONE), pipe.sink()); HashingSink hashingSink = HashingSink.sha256(Okio.blackhole()); readAllAndClose(pipe.source(), hashingSink); assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get()); assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash()); }
@Test public void gzipSource() throws Exception { Pipe pipe = new Pipe(1024 * 1024); OutputStream gzipOut = new GZIPOutputStream(Okio.buffer(pipe.sink()).outputStream()) { { // Disable compression to speed up a slow test. Improved from 141s to 33s on one machine. def.setLevel(Deflater.NO_COMPRESSION); } }; Future<Long> future = readAllAndCloseAsync( randomSource(FOUR_GIB_PLUS_ONE), Okio.sink(gzipOut)); HashingSink hashingSink = HashingSink.sha256(Okio.blackhole()); GzipSource gzipSource = new GzipSource(pipe.source()); readAllAndClose(gzipSource, hashingSink); assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get()); assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash()); }
@Test public void sourceTimeout() throws Exception { Pipe pipe = new Pipe(3L); pipe.source().timeout().timeout(1000, TimeUnit.MILLISECONDS); double start = now(); Buffer readBuffer = new Buffer(); try { pipe.source().read(readBuffer, 6L); fail(); } catch (InterruptedIOException expected) { assertEquals("timeout", expected.getMessage()); } assertElapsed(1000.0, start); assertEquals(0, readBuffer.size()); }
@Test public void sinkTimeout() throws Exception { Pipe pipe = new Pipe(3); pipe.sink().timeout().timeout(1000, TimeUnit.MILLISECONDS); pipe.sink().write(new Buffer().writeUtf8("abc"), 3L); double start = now(); try { pipe.sink().write(new Buffer().writeUtf8("def"), 3L); fail(); } catch (InterruptedIOException expected) { assertEquals("timeout", expected.getMessage()); } assertElapsed(1000.0, start); Buffer readBuffer = new Buffer(); assertEquals(3L, pipe.source().read(readBuffer, 6L)); assertEquals("abc", readBuffer.readUtf8()); }
final Pipe pipe = new Pipe(100L); executorService.execute(new Runnable() { @Override public void run() {