|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + */ |
| 22 | +package com.influxdb.rest; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.time.Duration; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.HashSet; |
| 28 | +import java.util.Set; |
| 29 | +import java.util.logging.Logger; |
| 30 | +import javax.annotation.Nonnull; |
| 31 | + |
| 32 | +import okhttp3.Call; |
| 33 | +import okhttp3.Connection; |
| 34 | +import okhttp3.EventListener; |
| 35 | +import okhttp3.OkHttpClient; |
| 36 | +import okhttp3.Protocol; |
| 37 | +import okhttp3.Request; |
| 38 | +import okhttp3.Response; |
| 39 | +import org.assertj.core.api.Assertions; |
| 40 | +import org.jetbrains.annotations.NotNull; |
| 41 | +import org.junit.jupiter.api.AfterEach; |
| 42 | +import org.junit.jupiter.api.BeforeEach; |
| 43 | +import org.junit.jupiter.api.Test; |
| 44 | + |
| 45 | +import com.influxdb.test.AbstractMockServerTest; |
| 46 | + |
| 47 | +class ITConnectionClosingInterceptor extends AbstractMockServerTest { |
| 48 | + |
| 49 | + private static final Logger LOG = Logger.getLogger(ITConnectionClosingInterceptor.class.getName()); |
| 50 | + |
| 51 | + private String url; |
| 52 | + private OkHttpClient client; |
| 53 | + private ConnectionsListener connectionsListener; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + void setUp() { |
| 57 | + connectionsListener = new ConnectionsListener(); |
| 58 | + url = startMockServer(); |
| 59 | + } |
| 60 | + |
| 61 | + @AfterEach |
| 62 | + void tearDown() { |
| 63 | + client.connectionPool().evictAll(); |
| 64 | + client.dispatcher().executorService().shutdown(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void withoutTTLonConnection() throws Exception { |
| 69 | + |
| 70 | + client = new OkHttpClient.Builder() |
| 71 | + .eventListener(connectionsListener) |
| 72 | + .build(); |
| 73 | + |
| 74 | + callApi(5, 3); |
| 75 | + |
| 76 | + Assertions.assertThat(connectionsListener.connections).hasSize(1); |
| 77 | + Assertions.assertThat(client.connectionPool().connectionCount()).isEqualTo(1); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void withTTLonConnection() throws Exception { |
| 82 | + |
| 83 | + // Use connection TTL of 2 second |
| 84 | + ConnectionClosingInterceptor interceptor = new ConnectionClosingInterceptor(Duration.ofSeconds(2)) { |
| 85 | + |
| 86 | + @Override |
| 87 | + public void connectionAcquired(@NotNull Call call, @NotNull Connection connection) { |
| 88 | + super.connectionAcquired(call, connection); |
| 89 | + |
| 90 | + // count the number of connections, the okhttp client can have only one listener => we have to use this |
| 91 | + connectionsListener.connections.add(connection); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + client = new OkHttpClient.Builder() |
| 96 | + .addNetworkInterceptor(interceptor) |
| 97 | + .eventListener(interceptor) |
| 98 | + .protocols(Collections.singletonList(Protocol.HTTP_1_1)) |
| 99 | + .build(); |
| 100 | + |
| 101 | + callApi(5, 3); |
| 102 | + |
| 103 | + Assertions.assertThat(connectionsListener.connections).hasSize(3); |
| 104 | + Assertions.assertThat(client.connectionPool().connectionCount()).isEqualTo(1); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Call API by specified times. |
| 109 | + * |
| 110 | + * @param times the number of times to call API |
| 111 | + * @param sleepSeconds the number of seconds to sleep between calls |
| 112 | + * @throws IOException if an error occurs |
| 113 | + */ |
| 114 | + private void callApi(final int times, final int sleepSeconds) throws Exception { |
| 115 | + for (int i = 0; i < times; i++) { |
| 116 | + mockServer.enqueue(createResponse("")); |
| 117 | + |
| 118 | + Request request = new Request.Builder() |
| 119 | + .url(url) |
| 120 | + .build(); |
| 121 | + |
| 122 | + LOG.info(String.format("Calling API %d", i)); |
| 123 | + try (Response response = client.newCall(request).execute()) { |
| 124 | + Assertions.assertThat(response.isSuccessful()).isTrue(); |
| 125 | + } |
| 126 | + |
| 127 | + LOG.info(String.format("Sleeping %d seconds; connection counts: %d", sleepSeconds, connectionsListener.connections.size())); |
| 128 | + Thread.sleep(sleepSeconds * 1000L); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Event listener that store acquired connections. |
| 134 | + */ |
| 135 | + private static class ConnectionsListener extends EventListener { |
| 136 | + private final Set<Connection> connections = new HashSet<>(); |
| 137 | + |
| 138 | + @Override |
| 139 | + public void connectionAcquired(@Nonnull final Call call, @Nonnull final Connection connection) { |
| 140 | + connections.add(connection); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments