Search in sources :

Example 1 with StreamResetException

use of okhttp3.internal.http2.StreamResetException in project okhttp by square.

the class DuplexTest method serverCancelsRequestBodyAndSendsResponseBody.

/**
 * OkHttp currently doesn't implement failing the request body stream independently of failing the
 * corresponding response body stream. This is necessary if we want servers to be able to stop
 * inbound data and send an early 400 before the request body completes.
 *
 * This test sends a slow request that is canceled by the server. It expects the response to still
 * be readable after the request stream is canceled.
 */
@Disabled
@Test
public void serverCancelsRequestBodyAndSendsResponseBody() throws Exception {
    client = client.newBuilder().retryOnConnectionFailure(false).build();
    BlockingQueue<String> log = new LinkedBlockingQueue<>();
    enableProtocol(Protocol.HTTP_2);
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().sendResponse("success!").exhaustResponse().cancelStream(ErrorCode.NO_ERROR));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new RequestBody() {

        @Override
        @Nullable
        public MediaType contentType() {
            return null;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            try {
                for (int i = 0; i < 10; i++) {
                    sink.writeUtf8(".");
                    sink.flush();
                    Thread.sleep(100);
                }
            } catch (IOException e) {
                log.add(e.toString());
                throw e;
            } catch (Exception e) {
                log.add(e.toString());
            }
        }
    }).build());
    try (Response response = call.execute()) {
        assertThat(response.body().string()).isEqualTo("success!");
    }
    mockDuplexResponseBody.awaitSuccess();
    assertThat(log.take()).contains("StreamResetException: stream was reset: NO_ERROR");
}
Also used : MockResponse(mockwebserver3.MockResponse) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException) MockResponse(mockwebserver3.MockResponse) MockDuplexResponseBody(mockwebserver3.internal.duplex.MockDuplexResponseBody) Nullable(org.jetbrains.annotations.Nullable) AsyncRequestBody(okhttp3.internal.duplex.AsyncRequestBody) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 2 with StreamResetException

use of okhttp3.internal.http2.StreamResetException in project okhttp by square.

the class HttpOverHttp2Test method noRecoveryFromErrorWithRetryDisabled.

private void noRecoveryFromErrorWithRetryDisabled(ErrorCode errorCode) throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.RESET_STREAM_AT_START).setHttp2ErrorCode(errorCode.httpCode));
    server.enqueue(new MockResponse().setBody("abc"));
    client = client.newBuilder().retryOnConnectionFailure(false).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    try {
        call.execute();
        fail();
    } catch (StreamResetException expected) {
        assertEquals(errorCode, expected.errorCode);
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest)

Example 3 with StreamResetException

use of okhttp3.internal.http2.StreamResetException in project okhttp by square.

the class StreamAllocation method streamFailed.

public void streamFailed(IOException e) {
    Socket socket;
    boolean noNewStreams = false;
    synchronized (connectionPool) {
        if (e instanceof StreamResetException) {
            StreamResetException streamResetException = (StreamResetException) e;
            if (streamResetException.errorCode == ErrorCode.REFUSED_STREAM) {
                refusedStreamCount++;
            }
            // other errors must be retried on a new connection.
            if (streamResetException.errorCode != ErrorCode.REFUSED_STREAM || refusedStreamCount > 1) {
                noNewStreams = true;
                route = null;
            }
        } else if (connection != null && (!connection.isMultiplexed() || e instanceof ConnectionShutdownException)) {
            noNewStreams = true;
            // If this route hasn't completed a call, avoid it for new connections.
            if (connection.successCount == 0) {
                if (route != null && e != null) {
                    routeSelector.connectFailed(route, e);
                }
                route = null;
            }
        }
        socket = deallocate(noNewStreams, false, true);
    }
    closeQuietly(socket);
}
Also used : ConnectionShutdownException(okhttp3.internal.http2.ConnectionShutdownException) StreamResetException(okhttp3.internal.http2.StreamResetException) Socket(java.net.Socket)

Aggregations

IOException (java.io.IOException)1 ProtocolException (java.net.ProtocolException)1 Socket (java.net.Socket)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 MockResponse (mockwebserver3.MockResponse)1 MockDuplexResponseBody (mockwebserver3.internal.duplex.MockDuplexResponseBody)1 Call (okhttp3.Call)1 Request (okhttp3.Request)1 AsyncRequestBody (okhttp3.internal.duplex.AsyncRequestBody)1 ConnectionShutdownException (okhttp3.internal.http2.ConnectionShutdownException)1 StreamResetException (okhttp3.internal.http2.StreamResetException)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 BufferedSink (okio.BufferedSink)1 Nullable (org.jetbrains.annotations.Nullable)1 Disabled (org.junit.jupiter.api.Disabled)1 Test (org.junit.jupiter.api.Test)1