|
| 1 | +/** |
| 2 | + * Copyright (C) 2015 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.fabric8.kubernetes.client.okhttp; |
| 17 | + |
| 18 | +import io.fabric8.kubernetes.client.http.AsyncBody; |
| 19 | +import io.fabric8.kubernetes.client.http.HttpClient; |
| 20 | +import io.fabric8.kubernetes.client.http.HttpResponse; |
| 21 | +import okhttp3.ConnectionPool; |
| 22 | +import okhttp3.Protocol; |
| 23 | +import okhttp3.mockwebserver.MockResponse; |
| 24 | +import okhttp3.mockwebserver.MockWebServer; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.DisplayName; |
| 28 | +import org.junit.jupiter.params.ParameterizedTest; |
| 29 | +import org.junit.jupiter.params.provider.ValueSource; |
| 30 | + |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +class ConnectionPoolLeakageTest { |
| 38 | + |
| 39 | + private MockWebServer server; |
| 40 | + |
| 41 | + private ConnectionPool connectionPool; |
| 42 | + private OkHttpClientBuilderImpl clientBuilder; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void setUp() { |
| 46 | + server = new MockWebServer(); |
| 47 | + final char[] chars = new char[10485760]; |
| 48 | + Arrays.fill(chars, '1'); |
| 49 | + server.enqueue(new MockResponse().setResponseCode(200).setChunkedBody(new String(chars), 1024)); |
| 50 | + connectionPool = new ConnectionPool(10, 100, TimeUnit.SECONDS); |
| 51 | + clientBuilder = new OkHttpClientFactory().newBuilder(); |
| 52 | + clientBuilder.getBuilder().connectionPool(connectionPool); |
| 53 | + } |
| 54 | + |
| 55 | + @AfterEach |
| 56 | + void tearDown() throws Exception { |
| 57 | + server.shutdown(); |
| 58 | + connectionPool.evictAll(); |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest(name = "with protocol {0}") |
| 62 | + @DisplayName("consumeBytes should not leak connections") |
| 63 | + @ValueSource(strings = { "h2_prior_knowledge", "http/1.1" }) |
| 64 | + void consumeBytes(String protocol) throws Exception { |
| 65 | + final Protocol p = Protocol.get(protocol); |
| 66 | + server.setProtocols(Collections.singletonList(p)); |
| 67 | + server.start(); |
| 68 | + clientBuilder.getBuilder().protocols(Collections.singletonList(p)); |
| 69 | + try (HttpClient httpClient = clientBuilder.build()) { |
| 70 | + final HttpResponse<AsyncBody> asyncBodyResponse = httpClient.consumeBytes( |
| 71 | + httpClient.newHttpRequestBuilder().uri(server.url("/").toString()).build(), |
| 72 | + (value, asyncBody) -> { |
| 73 | + asyncBody.consume(); |
| 74 | + }) |
| 75 | + .get(10L, TimeUnit.SECONDS); |
| 76 | + assertThat(activeConnections()).isEqualTo(1); |
| 77 | + asyncBodyResponse.body().consume(); |
| 78 | + asyncBodyResponse.body().done().get(10L, TimeUnit.SECONDS); |
| 79 | + assertThat(activeConnections()).isZero(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private int activeConnections() { |
| 84 | + return connectionPool.connectionCount() - connectionPool.idleConnectionCount(); |
| 85 | + } |
| 86 | +} |
0 commit comments