|
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 |
|
@@ -1264,32 +1268,62 @@ public void testClientUseOwnTimeZone() { |
1264 | 1268 |
|
1265 | 1269 | @Test |
1266 | 1270 | public void testAsyncQuery() { |
1267 | | - try (Client client = newClient().useAsyncRequests(true).build(); |
1268 | | - QueryResponse response = |
| 1271 | + try (Client client = newClient().useAsyncRequests(true).build()){ |
| 1272 | + simpleRequest(client); |
| 1273 | + } catch (Exception e) { |
| 1274 | + Assert.fail("Failed to get server time zone from header", e); |
| 1275 | + } |
| 1276 | + } |
| 1277 | + |
| 1278 | + protected void simpleRequest(Client client) throws Exception { |
| 1279 | + try (QueryResponse response = |
1269 | 1280 | client.query("SELECT number FROM system.numbers LIMIT 1000_000").get(1, TimeUnit.SECONDS)) { |
1270 | | - ClickHouseBinaryFormatReader reader = |
1271 | | - new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream(), response.getSettings()); |
| 1281 | + ClickHouseBinaryFormatReader reader = |
| 1282 | + new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream(), response.getSettings()); |
1272 | 1283 |
|
1273 | | - int count = 0; |
1274 | | - while (reader.hasNext()) { |
1275 | | - reader.next(); |
1276 | | - count++; |
1277 | | - } |
| 1284 | + int count = 0; |
| 1285 | + while (reader.hasNext()) { |
| 1286 | + reader.next(); |
| 1287 | + count++; |
| 1288 | + } |
1278 | 1289 |
|
1279 | | - Assert.assertEquals(count, 1000_000); |
1280 | | - } catch (Exception e) { |
1281 | | - Assert.fail("Failed to get server time zone from header", e); |
| 1290 | + Assert.assertEquals(count, 1000_000); |
1282 | 1291 | } |
1283 | 1292 | } |
1284 | 1293 |
|
1285 | | - private Client.Builder newClient() { |
| 1294 | + @Test |
| 1295 | + public void testConcurrentQueries() throws Exception{ |
| 1296 | + final Client client = newClient().build(); |
| 1297 | + final int concurrency = 10; |
| 1298 | + CountDownLatch latch = new CountDownLatch(concurrency); |
| 1299 | + Runnable task = () -> { |
| 1300 | + try { |
| 1301 | + simpleRequest(client); |
| 1302 | + } catch (Exception e) { |
| 1303 | + e.printStackTrace(); |
| 1304 | + Assert.fail("Failed", e); |
| 1305 | + } finally { |
| 1306 | + latch.countDown(); |
| 1307 | + } |
| 1308 | + }; |
| 1309 | + |
| 1310 | + ExecutorService executor = Executors.newFixedThreadPool(concurrency); |
| 1311 | + IntStream.range(0,concurrency).forEach(i -> executor.submit(task)); |
| 1312 | + executor.shutdown(); |
| 1313 | + executor.awaitTermination(10, TimeUnit.SECONDS); |
| 1314 | + latch.await(); |
| 1315 | + Assert.assertEquals(latch.getCount(), 0); |
| 1316 | + } |
| 1317 | + |
| 1318 | + protected Client.Builder newClient() { |
1286 | 1319 | ClickHouseNode node = getServer(ClickHouseProtocol.HTTP); |
1287 | 1320 | return new Client.Builder() |
1288 | 1321 | .addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), false) |
1289 | 1322 | .setUsername("default") |
1290 | 1323 | .setPassword("") |
1291 | 1324 | .compressClientRequest(false) |
1292 | | - .compressServerResponse(false) |
| 1325 | + .compressServerResponse(true) |
| 1326 | + .useHttpCompression(useHttpCompression) |
1293 | 1327 | .useNewImplementation(System.getProperty("client.tests.useNewImplementation", "true").equals("true")); |
1294 | 1328 | } |
1295 | 1329 | } |
0 commit comments