|
18 | 18 | import org.junit.platform.runner.JUnitPlatform;
|
19 | 19 | import org.junit.runner.RunWith;
|
20 | 20 |
|
| 21 | +import okhttp3.OkHttpClient; |
| 22 | + |
21 | 23 | import java.io.IOException;
|
22 | 24 | import java.time.Instant;
|
23 | 25 | import java.time.ZoneId;
|
|
27 | 29 | import java.util.List;
|
28 | 30 | import java.util.Set;
|
29 | 31 | import java.util.concurrent.BlockingQueue;
|
| 32 | +import java.util.concurrent.Callable; |
30 | 33 | import java.util.concurrent.CountDownLatch;
|
| 34 | +import java.util.concurrent.ExecutorService; |
31 | 35 | import java.util.concurrent.Executors;
|
32 | 36 | import java.util.concurrent.LinkedBlockingQueue;
|
33 | 37 | import java.util.concurrent.ThreadFactory;
|
@@ -891,5 +895,62 @@ public void testMessagePackOnOldDbVersion() {
|
891 | 895 | influxDB.describeDatabases();
|
892 | 896 | });
|
893 | 897 | }
|
894 |
| - |
| 898 | + |
| 899 | + /** |
| 900 | + * test for issue #445 |
| 901 | + * make sure reusing of OkHttpClient.Builder causes no error |
| 902 | + * @throws InterruptedException |
| 903 | + */ |
| 904 | + @Test |
| 905 | + public void testIssue445() throws InterruptedException { |
| 906 | + ExecutorService executor = Executors.newFixedThreadPool(100); |
| 907 | + |
| 908 | + final int maxCallables = 10_000; |
| 909 | + List<Callable<String>> callableList = new ArrayList<>(maxCallables); |
| 910 | + for (int i = 0; i < maxCallables; i++) { |
| 911 | + callableList.add(new Callable<String>() { |
| 912 | + @Override |
| 913 | + public String call() throws Exception { |
| 914 | + MyInfluxDBBean myBean = new MyInfluxDBBean(); |
| 915 | + return myBean.connectAndDoNothing1(); |
| 916 | + } |
| 917 | + }); |
| 918 | + } |
| 919 | + executor.invokeAll(callableList); |
| 920 | + executor.shutdown(); |
| 921 | + if (!executor.awaitTermination(20, TimeUnit.SECONDS)) { |
| 922 | + executor.shutdownNow(); |
| 923 | + } |
| 924 | + Assertions.assertTrue(MyInfluxDBBean.OK); |
| 925 | + //assert that MyInfluxDBBean.OKHTTP_BUILDER stays untouched (no interceptor added) |
| 926 | + Assertions.assertTrue(MyInfluxDBBean.OKHTTP_BUILDER.interceptors().isEmpty()); |
| 927 | + } |
| 928 | + |
| 929 | + private static final class MyInfluxDBBean { |
| 930 | + |
| 931 | + static final OkHttpClient.Builder OKHTTP_BUILDER = new OkHttpClient.Builder(); |
| 932 | + static Boolean OK = true; |
| 933 | + static final String URL = "http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true); |
| 934 | + |
| 935 | + InfluxDB influxClient; |
| 936 | + |
| 937 | + String connectAndDoNothing1() { |
| 938 | + synchronized (OK) { |
| 939 | + if (!OK) { |
| 940 | + return null; |
| 941 | + } |
| 942 | + } |
| 943 | + try { |
| 944 | + influxClient = InfluxDBFactory.connect(URL, "admin", "admin", OKHTTP_BUILDER); |
| 945 | + influxClient.close(); |
| 946 | + } catch (Exception e) { |
| 947 | + synchronized (OK) { |
| 948 | + if (OK) { |
| 949 | + OK = false; |
| 950 | + } |
| 951 | + } |
| 952 | + } |
| 953 | + return null; |
| 954 | + } |
| 955 | + } |
895 | 956 | }
|
0 commit comments