|
66 | 66 | import java.util.Properties; |
67 | 67 | import java.util.Random; |
68 | 68 | import java.util.UUID; |
| 69 | +import java.util.concurrent.CountDownLatch; |
69 | 70 | import java.util.concurrent.ExecutionException; |
| 71 | +import java.util.concurrent.ExecutorService; |
| 72 | +import java.util.concurrent.Executors; |
70 | 73 | import java.util.concurrent.Future; |
71 | 74 | import java.util.concurrent.TimeUnit; |
72 | 75 | import java.util.function.Consumer; |
73 | 76 | import java.util.function.Function; |
74 | 77 | import java.util.function.Supplier; |
75 | 78 | import java.util.stream.BaseStream; |
| 79 | +import java.util.stream.IntStream; |
76 | 80 |
|
77 | 81 | public class QueryTests extends BaseIntegrationTest { |
78 | 82 |
|
@@ -1281,32 +1285,62 @@ public void testClientUseOwnTimeZone() { |
1281 | 1285 |
|
1282 | 1286 | @Test |
1283 | 1287 | public void testAsyncQuery() { |
1284 | | - try (Client client = newClient().useAsyncRequests(true).build(); |
1285 | | - QueryResponse response = |
| 1288 | + try (Client client = newClient().useAsyncRequests(true).build()){ |
| 1289 | + simpleRequest(client); |
| 1290 | + } catch (Exception e) { |
| 1291 | + Assert.fail("Failed to get server time zone from header", e); |
| 1292 | + } |
| 1293 | + } |
| 1294 | + |
| 1295 | + protected void simpleRequest(Client client) throws Exception { |
| 1296 | + try (QueryResponse response = |
1286 | 1297 | client.query("SELECT number FROM system.numbers LIMIT 1000_000").get(1, TimeUnit.SECONDS)) { |
1287 | | - ClickHouseBinaryFormatReader reader = |
1288 | | - new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream(), response.getSettings()); |
| 1298 | + ClickHouseBinaryFormatReader reader = |
| 1299 | + new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream(), response.getSettings()); |
1289 | 1300 |
|
1290 | | - int count = 0; |
1291 | | - while (reader.hasNext()) { |
1292 | | - reader.next(); |
1293 | | - count++; |
1294 | | - } |
| 1301 | + int count = 0; |
| 1302 | + while (reader.hasNext()) { |
| 1303 | + reader.next(); |
| 1304 | + count++; |
| 1305 | + } |
1295 | 1306 |
|
1296 | | - Assert.assertEquals(count, 1000_000); |
1297 | | - } catch (Exception e) { |
1298 | | - Assert.fail("Failed to get server time zone from header", e); |
| 1307 | + Assert.assertEquals(count, 1000_000); |
1299 | 1308 | } |
1300 | 1309 | } |
1301 | 1310 |
|
1302 | | - private Client.Builder newClient() { |
| 1311 | + @Test |
| 1312 | + public void testConcurrentQueries() throws Exception{ |
| 1313 | + final Client client = newClient().build(); |
| 1314 | + final int concurrency = 10; |
| 1315 | + CountDownLatch latch = new CountDownLatch(concurrency); |
| 1316 | + Runnable task = () -> { |
| 1317 | + try { |
| 1318 | + simpleRequest(client); |
| 1319 | + } catch (Exception e) { |
| 1320 | + e.printStackTrace(); |
| 1321 | + Assert.fail("Failed", e); |
| 1322 | + } finally { |
| 1323 | + latch.countDown(); |
| 1324 | + } |
| 1325 | + }; |
| 1326 | + |
| 1327 | + ExecutorService executor = Executors.newFixedThreadPool(concurrency); |
| 1328 | + IntStream.range(0,concurrency).forEach(i -> executor.submit(task)); |
| 1329 | + executor.shutdown(); |
| 1330 | + executor.awaitTermination(10, TimeUnit.SECONDS); |
| 1331 | + latch.await(); |
| 1332 | + Assert.assertEquals(latch.getCount(), 0); |
| 1333 | + } |
| 1334 | + |
| 1335 | + protected Client.Builder newClient() { |
1303 | 1336 | ClickHouseNode node = getServer(ClickHouseProtocol.HTTP); |
1304 | 1337 | return new Client.Builder() |
1305 | 1338 | .addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), false) |
1306 | 1339 | .setUsername("default") |
1307 | 1340 | .setPassword("") |
1308 | 1341 | .compressClientRequest(false) |
1309 | | - .compressServerResponse(false) |
| 1342 | + .compressServerResponse(true) |
| 1343 | + .useHttpCompression(useHttpCompression) |
1310 | 1344 | .useNewImplementation(System.getProperty("client.tests.useNewImplementation", "true").equals("true")); |
1311 | 1345 | } |
1312 | 1346 | } |
0 commit comments