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

More tests to cover socket timeout and error during query/insert #983

Merged
merged 6 commits into from
Jul 8, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public void testCustomLoad() throws Exception {
throw new SkipException("Skip due to time out error");
}

@Test(groups = { "integration" })
@Override
public void testErrorDuringQuery() throws Exception {
throw new SkipException(
"Skip due to incomplete implementation(needs to consider ErrorOutputStream in deserialization as well)");
}

@Test(groups = { "integration" })
@Override
public void testLoadRawData() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Wraper class of long.
*/
public class ClickHouseLongValue implements ClickHouseValue {
private static final BigInteger MASK = BigInteger.ONE.shiftLeft(Long.SIZE).subtract(BigInteger.ONE);
public static final BigInteger MASK = BigInteger.ONE.shiftLeft(Long.SIZE).subtract(BigInteger.ONE);

/**
* Create a new instance representing null value of long.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.ClickHouseValue;
import com.clickhouse.client.ClickHouseValues;
import com.clickhouse.client.data.ClickHouseLongValue;
import com.clickhouse.client.data.ClickHouseObjectValue;

/**
Expand Down Expand Up @@ -91,11 +92,23 @@ public Object[] asArray() {
public <E> E[] asArray(Class<E> clazz) {
long[] v = getValue();
int len = v.length;
E[] array = ClickHouseValues.createObjectArray(clazz, len, 1);
for (int i = 0; i < len; i++) {
array[i] = clazz.cast(v[i]);
if (clazz == BigInteger.class) {
BigInteger[] array = new BigInteger[len];
for (int i = 0; i < len; i++) {
long value = v[i];
array[i] = BigInteger.valueOf(value);
if (value < 0L) {
array[i] = array[i].and(ClickHouseLongValue.MASK);
}
}
return (E[]) array;
} else {
E[] array = ClickHouseValues.createObjectArray(clazz, len, 1);
for (int i = 0; i < len; i++) {
array[i] = clazz.cast(v[i]);
}
return array;
}
return array;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.Inet4Address;
Expand Down Expand Up @@ -44,6 +45,7 @@
import com.clickhouse.client.data.ClickHouseStringValue;

import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -1256,4 +1258,56 @@ public void testTempTable() throws Exception {
Assert.assertEquals(count, 2);
}
}

@Test(groups = "integration")
public void testErrorDuringInsert() throws Exception {
ClickHouseNode server = getServer();
if (server.getProtocol() != ClickHouseProtocol.HTTP) {
throw new SkipException("Skip as only http implementation works well");
}
ClickHouseClient.send(server, "drop table if exists error_during_insert",
"create table error_during_insert(n UInt64, flag UInt8)engine=Null").get();
boolean success = true;
try (ClickHouseClient client = getClient();
ClickHouseResponse resp = client.connect(getServer()).write().format(ClickHouseFormat.RowBinary)
.query("insert into error_during_insert select number, throwIf(number>=100000000) from numbers(500000000)")
.executeAndWait()) {
for (ClickHouseRecord r : resp.records()) {
Assert.fail("Should have no record");
}
Assert.fail("Insert should be aborted");
} catch (ClickHouseException e) {
Assert.assertEquals(e.getErrorCode(), 395);
Assert.assertTrue(e.getCause() instanceof IOException, "Should end up with IOException");
success = false;
}

Assert.assertFalse(success, "Should fail due insert error");
}

@Test(groups = "integration")
public void testErrorDuringQuery() throws Exception {
ClickHouseNode server = getServer();
if (server.getProtocol() != ClickHouseProtocol.HTTP) {
throw new SkipException("Skip as only http implementation works well");
}
String query = "select number, throwIf(number>=100000000) from numbers(500000000)";
long count = 0L;
try (ClickHouseClient client = getClient();
ClickHouseResponse resp = client.connect(server).format(ClickHouseFormat.RowBinaryWithNamesAndTypes)
.query(query).executeAndWait()) {
for (ClickHouseRecord r : resp.records()) {
// does not work which may relate to deserialization failure
// java.lang.AssertionError: expected [99764115] but found [4121673519155408707]
// Assert.assertEquals(r.getValue(0).asLong(), count++);
Assert.assertTrue((count = r.getValue(0).asLong()) >= 0);
}
Assert.fail("Query should be terminated before all rows returned");
} catch (UncheckedIOException e) {
Assert.assertTrue(e.getCause() instanceof IOException,
"Should end up with IOException due to deserialization failure");
}

Assert.assertNotEquals(count, 0L, "Should have read at least one record");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.clickhouse.client.data.array;

import java.math.BigInteger;

import org.junit.Assert;
import org.testng.annotations.Test;

public class ClickHouseLongArrayValueTest {
@Test(groups = { "unit" })
public void testConvertToBigInteger() throws Exception {
ClickHouseLongArrayValue v = ClickHouseLongArrayValue
.of(new long[] { 1L, new BigInteger("9223372036854775808").longValue() });
Assert.assertArrayEquals(v.getValue(), new long[] { 1L, -9223372036854775808L });
Assert.assertArrayEquals(v.asArray(BigInteger.class),
new BigInteger[] { BigInteger.ONE, new BigInteger("9223372036854775808") });
}

@Test(groups = { "unit" })
public void testConvertFromBigInteger() throws Exception {
ClickHouseLongArrayValue v = ClickHouseLongArrayValue.ofEmpty();
Assert.assertArrayEquals(v.getValue(), new long[0]);
v.update(new BigInteger[] { BigInteger.ONE, new BigInteger("9223372036854775808") });
Assert.assertArrayEquals(v.getValue(), new long[] { 1L, -9223372036854775808L });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
import com.clickhouse.client.ClickHouseDataType;
import com.clickhouse.client.ClickHouseParameterizedQuery;
import com.clickhouse.client.ClickHouseProtocol;
import com.clickhouse.client.ClickHouseRequest;
import com.clickhouse.client.ClickHouseValues;
import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.client.data.ClickHouseDateTimeValue;
import com.clickhouse.client.http.config.ClickHouseHttpOption;

import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

Expand All @@ -65,6 +67,25 @@ public void testJdbcEscapeSyntax() throws SQLException {
}
}

@Test(groups = "integration")
public void testSocketTimeout() throws SQLException {
Properties props = new Properties();
props.setProperty("connect_timeout", "500");
props.setProperty("socket_timeout", "1000");
props.setProperty("database", "system");
try (ClickHouseConnection conn = newConnection(props);
ClickHouseStatement stmt = conn.createStatement()) {
if (stmt.unwrap(ClickHouseRequest.class).getServer().getProtocol() != ClickHouseProtocol.HTTP) {
throw new SkipException("Skip as only http implementation works well");
}
stmt.executeQuery("select sleep(3)");
Assert.fail("Should throw timeout exception");
} catch (SQLException e) {
Assert.assertTrue(e.getCause() instanceof java.net.SocketTimeoutException,
"Should throw SocketTimeoutException");
}
}

@Test(groups = "integration")
public void testSwitchSchema() throws SQLException {
Properties props = new Properties();
Expand Down