@Override public Timeout timeout() { return new Timeout().timeout(executeTimeoutMillis, TimeUnit.MILLISECONDS); }
b0 = source.readByte() & 0xff; } finally { source.timeout().timeout(timeoutBefore, TimeUnit.NANOSECONDS);
public HttpCodec newCodec(OkHttpClient client, Interceptor.Chain chain, StreamAllocation streamAllocation) throws SocketException { if (http2Connection != null) { return new Http2Codec(client, chain, streamAllocation, http2Connection); } else { socket.setSoTimeout(chain.readTimeoutMillis()); source.timeout().timeout(chain.readTimeoutMillis(), MILLISECONDS); sink.timeout().timeout(chain.writeTimeoutMillis(), MILLISECONDS); return new Http1Codec(client, streamAllocation, source, sink); } }
b0 = source.readByte() & 0xff; } finally { source.timeout().timeout(timeoutBefore, TimeUnit.NANOSECONDS);
@Override public void writeRequestHeaders(Request request) throws IOException { if (stream != null) return; boolean hasRequestBody = request.body() != null; List<Header> requestHeaders = http2HeadersList(request); stream = connection.newStream(requestHeaders, hasRequestBody); // We may have been asked to cancel while creating the new stream and sending the request // headers, but there was still no stream to close. if (canceled) { stream.closeLater(ErrorCode.CANCEL); throw new IOException("Canceled"); } stream.readTimeout().timeout(chain.readTimeoutMillis(), TimeUnit.MILLISECONDS); stream.writeTimeout().timeout(chain.writeTimeoutMillis(), TimeUnit.MILLISECONDS); }
public HttpCodec newCodec(OkHttpClient client, Interceptor.Chain chain, StreamAllocation streamAllocation) throws SocketException { if (http2Connection != null) { return new Http2Codec(client, chain, streamAllocation, http2Connection); } else { socket.setSoTimeout(chain.readTimeoutMillis()); source.timeout().timeout(chain.readTimeoutMillis(), MILLISECONDS); sink.timeout().timeout(chain.writeTimeoutMillis(), MILLISECONDS); return new Http1Codec(client, streamAllocation, source, sink); } }
@Override public void writeRequestHeaders(Request request) throws IOException { if (stream != null) return; boolean hasRequestBody = request.body() != null; List<Header> requestHeaders = http2HeadersList(request); stream = connection.newStream(requestHeaders, hasRequestBody); // We may have been asked to cancel while creating the new stream and sending the request // headers, but there was still no stream to close. if (canceled) { stream.closeLater(ErrorCode.CANCEL); throw new IOException("Canceled"); } stream.readTimeout().timeout(chain.readTimeoutMillis(), TimeUnit.MILLISECONDS); stream.writeTimeout().timeout(chain.writeTimeoutMillis(), TimeUnit.MILLISECONDS); }
? new StreamedRequestBody(contentLength) : new BufferedRequestBody(contentLength); requestBody.timeout().timeout(client.writeTimeoutMillis(), TimeUnit.MILLISECONDS);
@Test public void readWithoutTimeout() throws Exception { Socket socket = socket(ONE_MB, 0); BufferedSource source = Okio.buffer(Okio.source(socket)); source.timeout().timeout(5000, TimeUnit.MILLISECONDS); source.require(ONE_MB); socket.close(); }
@Test public void readWithTimeout() throws Exception { Socket socket = socket(0, 0); BufferedSource source = Okio.buffer(Okio.source(socket)); source.timeout().timeout(250, TimeUnit.MILLISECONDS); try { source.require(ONE_MB); fail(); } catch (SocketTimeoutException expected) { } socket.close(); }
@Test public void writeWithoutTimeout() throws Exception { Socket socket = socket(0, ONE_MB); Sink sink = Okio.buffer(Okio.sink(socket)); sink.timeout().timeout(500, TimeUnit.MILLISECONDS); byte[] data = new byte[ONE_MB]; sink.write(new Buffer().write(data), data.length); sink.flush(); socket.close(); }
@Test public synchronized void notified() throws InterruptedIOException { Timeout timeout = new Timeout(); timeout.timeout(5000, TimeUnit.MILLISECONDS); double start = now(); executorService.schedule(new Runnable() { @Override public void run() { synchronized (WaitUntilNotifiedTest.this) { WaitUntilNotifiedTest.this.notify(); } } }, 1000, TimeUnit.MILLISECONDS); timeout.waitUntilNotified(this); assertElapsed(1000.0, start); }
@Test public void writeWithTimeout() throws Exception { Socket socket = socket(0, 0); Sink sink = Okio.sink(socket); sink.timeout().timeout(500, TimeUnit.MILLISECONDS); byte[] data = new byte[ONE_MB]; long start = System.nanoTime(); try { sink.write(new Buffer().write(data), data.length); sink.flush(); fail(); } catch (SocketTimeoutException expected) { } long elapsed = System.nanoTime() - start; socket.close(); assertTrue("elapsed: " + elapsed, TimeUnit.NANOSECONDS.toMillis(elapsed) >= 500); assertTrue("elapsed: " + elapsed, TimeUnit.NANOSECONDS.toMillis(elapsed) <= 750); }
@Test public synchronized void timeout() { Timeout timeout = new Timeout(); timeout.timeout(1000, TimeUnit.MILLISECONDS); double start = now(); try { timeout.waitUntilNotified(this); fail(); } catch (InterruptedIOException expected) { assertEquals("timeout", expected.getMessage()); } assertElapsed(1000.0, start); }
@Test public synchronized void deadlineBeforeTimeout() { Timeout timeout = new Timeout(); timeout.timeout(5000, TimeUnit.MILLISECONDS); timeout.deadline(1000, TimeUnit.MILLISECONDS); double start = now(); try { timeout.waitUntilNotified(this); fail(); } catch (InterruptedIOException expected) { assertEquals("timeout", expected.getMessage()); } assertElapsed(1000.0, start); }
@Test public synchronized void timeoutBeforeDeadline() { Timeout timeout = new Timeout(); timeout.timeout(1000, TimeUnit.MILLISECONDS); timeout.deadline(5000, TimeUnit.MILLISECONDS); double start = now(); try { timeout.waitUntilNotified(this); fail(); } catch (InterruptedIOException expected) { assertEquals("timeout", expected.getMessage()); } assertElapsed(1000.0, start); }
@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()); }