Skip to content

Commit 9f9ef67

Browse files
committed
More tests to cover socket timeout and error during query/insert
1 parent d0284d2 commit 9f9ef67

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

clickhouse-client/src/test/java/com/clickhouse/client/ClientIntegrationTest.java

+47
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.io.InputStreamReader;
8+
import java.io.UncheckedIOException;
89
import java.math.BigDecimal;
910
import java.math.BigInteger;
1011
import java.net.Inet4Address;
@@ -1256,4 +1257,50 @@ public void testTempTable() throws Exception {
12561257
Assert.assertEquals(count, 2);
12571258
}
12581259
}
1260+
1261+
@Test(groups = "integration")
1262+
public void testErrorDuringInsert() throws Exception {
1263+
ClickHouseNode server = getServer();
1264+
ClickHouseClient.send(server, "drop table if exists error_during_insert",
1265+
"create table error_during_insert(n UInt64, flag UInt8)engine=Null").get();
1266+
boolean success = true;
1267+
try (ClickHouseClient client = getClient();
1268+
ClickHouseResponse resp = client.connect(getServer()).write()
1269+
.format(ClickHouseFormat.RowBinary)
1270+
.query("insert into error_during_insert select number, throwIf(number>=100000000) from numbers(500000000)")
1271+
.executeAndWait()) {
1272+
for (ClickHouseRecord r : resp.records()) {
1273+
Assert.fail("Should have no record");
1274+
}
1275+
Assert.fail("Insert should be aborted");
1276+
} catch (ClickHouseException e) {
1277+
Assert.assertEquals(e.getErrorCode(), 395);
1278+
Assert.assertTrue(e.getCause() instanceof IOException, "Should end up with IOException");
1279+
success = false;
1280+
}
1281+
1282+
Assert.assertFalse(success, "Should fail due insert error");
1283+
}
1284+
1285+
@Test(groups = "integration")
1286+
public void testErrorDuringQuery() throws Exception {
1287+
String query = "select number, throwIf(number>=100000000) from numbers(500000000)";
1288+
long count = 0L;
1289+
try (ClickHouseClient client = getClient();
1290+
ClickHouseResponse resp = client.connect(getServer())
1291+
.format(ClickHouseFormat.RowBinaryWithNamesAndTypes).query(query).executeAndWait()) {
1292+
for (ClickHouseRecord r : resp.records()) {
1293+
// does not work which may relate to deserialization failure
1294+
// java.lang.AssertionError: expected [99764115] but found [4121673519155408707]
1295+
// Assert.assertEquals(r.getValue(0).asLong(), count++);
1296+
Assert.assertTrue((count = r.getValue(0).asLong()) >= 0);
1297+
}
1298+
Assert.fail("Query should be terminated before all rows returned");
1299+
} catch (UncheckedIOException e) {
1300+
Assert.assertTrue(e.getCause() instanceof IOException,
1301+
"Should end up with IOException due to deserialization failure");
1302+
}
1303+
1304+
Assert.assertNotEquals(count, 0L, "Should have read at least one record");
1305+
}
12591306
}

clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseStatementTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ public void testJdbcEscapeSyntax() throws SQLException {
6565
}
6666
}
6767

68+
@Test(groups = "integration")
69+
public void testSocketTimeout() throws SQLException {
70+
Properties props = new Properties();
71+
props.setProperty("connect_timeout", "500");
72+
props.setProperty("socket_timeout", "1000");
73+
props.setProperty("database", "system");
74+
try (ClickHouseConnection conn = newConnection(props);
75+
ClickHouseStatement stmt = conn.createStatement()) {
76+
stmt.executeQuery("select sleep(3)");
77+
Assert.fail("Should throw timeout exception");
78+
} catch (SQLException e) {
79+
Assert.assertTrue(e.getCause() instanceof java.net.SocketTimeoutException,
80+
"Should throw SocketTimeoutException");
81+
}
82+
}
83+
6884
@Test(groups = "integration")
6985
public void testSwitchSchema() throws SQLException {
7086
Properties props = new Properties();

0 commit comments

Comments
 (0)