Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #445 #477

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ public InfluxDBImpl(final String url, final String username, final String passwo
setLogLevel(LOG_LEVEL);

this.gzipRequestInterceptor = new GzipRequestInterceptor();
OkHttpClient.Builder clonedBuilder = client.build().newBuilder();
this.retrofit = new Retrofit.Builder()
.baseUrl(url)
.client(client.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor).build())
.client(clonedBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor).build())
.addConverterFactory(MoshiConverterFactory.create())
.build();
this.influxDBService = this.retrofit.create(InfluxDBService.class);
Expand All @@ -120,9 +121,10 @@ public InfluxDBImpl(final String url, final String username, final String passwo
setLogLevel(LOG_LEVEL);

this.gzipRequestInterceptor = new GzipRequestInterceptor();
OkHttpClient.Builder clonedBuilder = client.build().newBuilder();
this.retrofit = new Retrofit.Builder()
.baseUrl(url)
.client(client.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor).build())
.client(clonedBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor).build())
.addConverterFactory(MoshiConverterFactory.create())
.build();
this.influxDBService = influxDBService;
Expand Down
86 changes: 71 additions & 15 deletions src/test/java/org/influxdb/InfluxDBTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package org.influxdb;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

import org.influxdb.InfluxDB.LogLevel;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.BoundParameterQuery.QueryBuilder;
Expand All @@ -16,21 +35,7 @@
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import okhttp3.OkHttpClient;

/**
* Test the InfluxDB API.
Expand Down Expand Up @@ -882,4 +887,55 @@ public void testIsBatchEnabledWithConsistency() {
}, InfluxDB.ConsistencyLevel.ALL);
Assertions.assertTrue(this.influxDB.isBatchEnabled());
}

/**
* test for issue #445
* make sure reusing of OkHttpClient.Builder causes no error
* @throws InterruptedException
*/
@Test
public void testIssue445() throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(100);

final int maxCallables = 10_000;
List<Callable<String>> callableList = new ArrayList<>(maxCallables);
for (int i = 0; i < maxCallables; i++) {
callableList.add(new Callable<String>() {
@Override
public String call() throws Exception {
MyInfluxDBBean myBean = new MyInfluxDBBean();
return myBean.connectAndDoNothing1();
}
});
}
System.out.println("Invoking all callableList (size()=" + callableList.size() + ")");
executor.invokeAll(callableList);
System.out.println("Shutting down...");
executor.shutdown();
System.out.println("Shutdown requested and waiting for termination...");
if (!executor.awaitTermination(20, TimeUnit.SECONDS)) {
executor.shutdownNow();
}

//assert that MyInfluxDBBean.OKHTTP_BUILDER stay untouched (no interceptor added)
Assertions.assertTrue(MyInfluxDBBean.OKHTTP_BUILDER.interceptors().isEmpty());
}

private static final class MyInfluxDBBean {

static final OkHttpClient.Builder OKHTTP_BUILDER = new OkHttpClient.Builder();

InfluxDB influxClient;

String connectAndDoNothing1() {
try {
influxClient = InfluxDBFactory.connect("http://127.0.0.1:8086", "admin", "supersecretpassword", OKHTTP_BUILDER);
} catch (Exception e) {
e.printStackTrace();
System.exit(1); // OPS!
}
return null;
}

}
}