Skip to content

Commit

Permalink
Added tests for long scan queries
Browse files Browse the repository at this point in the history
  • Loading branch information
alex268 committed Sep 26, 2024
1 parent df4525e commit 25c155a
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 1 deletion.
85 changes: 85 additions & 0 deletions jdbc/src/test/java/tech/ydb/jdbc/impl/YdbConnectionImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -18,6 +20,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -861,6 +864,88 @@ public void testLiteralQuery() throws SQLException {
}
}

private String createPayload(Random rnd, int length) {
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
sb.append((char)('0' + rnd.nextInt(75)));
}
return sb.toString();
}

@Test
@Timeout(value = 30, unit = TimeUnit.SECONDS, threadMode = Timeout.ThreadMode.SAME_THREAD)
public void testBigBulkAndScan() throws SQLException {
String bulkUpsert = QUERIES.upsertOne(SqlQueries.JdbcQuery.BULK, "c_Text", "Text?");
String scanSelectAll = QUERIES.scanSelectSQL();
String selectOne = QUERIES.selectAllByKey("?");

Random rnd = new Random(0x234567);
int payloadLength = 1000;

try {
// BULK UPSERT
try (PreparedStatement ps = jdbc.connection().prepareStatement(bulkUpsert)) {
for (int idx = 1; idx <= 10000; idx++) {
ps.setInt(1, idx);
String payload = createPayload(rnd, payloadLength);
ps.setString(2, payload);
ps.addBatch();
if (idx % 1000 == 0) {
ps.executeBatch();
}
}
ps.executeBatch();
}

// SCAN all table
try (PreparedStatement ps = jdbc.connection().prepareStatement(scanSelectAll)) {
int readed = 0;
Assertions.assertTrue(ps.execute());
try (ResultSet rs = ps.getResultSet()) {
while (rs.next()) {
readed++;
Assertions.assertEquals(readed, rs.getInt("key"));
Assertions.assertEquals(payloadLength, rs.getString("c_Text").length());
}
}
Assertions.assertEquals(10000, readed);
}

// Canceled scan
try (PreparedStatement ps = jdbc.connection().prepareStatement(scanSelectAll)) {
Assertions.assertTrue(ps.execute());
ps.getResultSet().next();
ps.getResultSet().close();

SQLWarning w = ps.getWarnings();
Assertions.assertNotNull(w);
Assertions.assertEquals("gRPC error: (CANCELLED) Cancelled on user request (S_ERROR)", w.getMessage());

w = w.getNextWarning();
Assertions.assertNotNull(w);
Assertions.assertEquals("java.util.concurrent.CancellationException (S_ERROR)", w.getMessage());

w = w.getNextWarning();
Assertions.assertNull(w);
}

// Scan was cancelled, but connection still work
try (PreparedStatement ps = jdbc.connection().prepareStatement(selectOne)) {
ps.setInt(1, 1234);

Assertions.assertTrue(ps.execute());
try (ResultSet rs = ps.getResultSet()) {
Assertions.assertTrue(rs.next());
Assertions.assertEquals(1234, rs.getInt("key"));
Assertions.assertEquals(payloadLength, rs.getString("c_Text").length());
Assertions.assertFalse(rs.next());
}
}
} finally {
cleanTable();
}
}

@Test
public void testAnsiLexer() throws SQLException {
try (Statement statement = jdbc.connection().createStatement()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -19,6 +21,7 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -876,6 +879,88 @@ public void testLiteralQuery() throws SQLException {
}
}

private String createPayload(Random rnd, int length) {
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
sb.append((char)('0' + rnd.nextInt(75)));
}
return sb.toString();
}

@Test
@Timeout(value = 30, unit = TimeUnit.SECONDS, threadMode = Timeout.ThreadMode.SAME_THREAD)
public void testBigBulkAndScan() throws SQLException {
String bulkUpsert = QUERIES.upsertOne(SqlQueries.JdbcQuery.BULK, "c_Text", "Text?");
String scanSelectAll = QUERIES.scanSelectSQL();
String selectOne = QUERIES.selectAllByKey("?");

Random rnd = new Random(0x234567);
int payloadLength = 1000;

try {
// BULK UPSERT
try (PreparedStatement ps = jdbc.connection().prepareStatement(bulkUpsert)) {
for (int idx = 1; idx <= 10000; idx++) {
ps.setInt(1, idx);
String payload = createPayload(rnd, payloadLength);
ps.setString(2, payload);
ps.addBatch();
if (idx % 1000 == 0) {
ps.executeBatch();
}
}
ps.executeBatch();
}

// SCAN all table
try (PreparedStatement ps = jdbc.connection().prepareStatement(scanSelectAll)) {
int readed = 0;
Assertions.assertTrue(ps.execute());
try (ResultSet rs = ps.getResultSet()) {
while (rs.next()) {
readed++;
Assertions.assertEquals(readed, rs.getInt("key"));
Assertions.assertEquals(payloadLength, rs.getString("c_Text").length());
}
}
Assertions.assertEquals(10000, readed);
}

// Canceled scan
try (PreparedStatement ps = jdbc.connection().prepareStatement(scanSelectAll)) {
Assertions.assertTrue(ps.execute());
ps.getResultSet().next();
ps.getResultSet().close();

SQLWarning w = ps.getWarnings();
Assertions.assertNotNull(w);
Assertions.assertEquals("gRPC error: (CANCELLED) Cancelled on user request (S_ERROR)", w.getMessage());

w = w.getNextWarning();
Assertions.assertNotNull(w);
Assertions.assertEquals("java.util.concurrent.CancellationException (S_ERROR)", w.getMessage());

w = w.getNextWarning();
Assertions.assertNull(w);
}

// Scan was cancelled, but connection still work
try (PreparedStatement ps = jdbc.connection().prepareStatement(selectOne)) {
ps.setInt(1, 1234);

Assertions.assertTrue(ps.execute());
try (ResultSet rs = ps.getResultSet()) {
Assertions.assertTrue(rs.next());
Assertions.assertEquals(1234, rs.getInt("key"));
Assertions.assertEquals(payloadLength, rs.getString("c_Text").length());
Assertions.assertFalse(rs.next());
}
}
} finally {
cleanTable();
}
}

@Test
public void testAnsiLexer() throws SQLException {
try (Statement statement = jdbc.connection().createStatement()) {
Expand Down
2 changes: 1 addition & 1 deletion jdbc/src/test/resources/sql/select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ select
c_Interval,

c_Decimal
from #tableName
from #tableName order by key

0 comments on commit 25c155a

Please sign in to comment.