From 25c155a752443e3b4c31197843477ecd7b9cbc6e Mon Sep 17 00:00:00 2001 From: Alexandr Gorshenin Date: Thu, 26 Sep 2024 17:37:51 +0100 Subject: [PATCH] Added tests for long scan queries --- .../ydb/jdbc/impl/YdbConnectionImplTest.java | 85 +++++++++++++++++++ .../jdbc/impl/YdbQueryConnectionImplTest.java | 85 +++++++++++++++++++ jdbc/src/test/resources/sql/select.sql | 2 +- 3 files changed, 171 insertions(+), 1 deletion(-) diff --git a/jdbc/src/test/java/tech/ydb/jdbc/impl/YdbConnectionImplTest.java b/jdbc/src/test/java/tech/ydb/jdbc/impl/YdbConnectionImplTest.java index c08ec75..66585b0 100644 --- a/jdbc/src/test/java/tech/ydb/jdbc/impl/YdbConnectionImplTest.java +++ b/jdbc/src/test/java/tech/ydb/jdbc/impl/YdbConnectionImplTest.java @@ -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; @@ -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; @@ -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()) { diff --git a/jdbc/src/test/java/tech/ydb/jdbc/impl/YdbQueryConnectionImplTest.java b/jdbc/src/test/java/tech/ydb/jdbc/impl/YdbQueryConnectionImplTest.java index 82431de..24a1a6c 100644 --- a/jdbc/src/test/java/tech/ydb/jdbc/impl/YdbQueryConnectionImplTest.java +++ b/jdbc/src/test/java/tech/ydb/jdbc/impl/YdbQueryConnectionImplTest.java @@ -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; @@ -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; @@ -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()) { diff --git a/jdbc/src/test/resources/sql/select.sql b/jdbc/src/test/resources/sql/select.sql index e3469b0..58c4f01 100644 --- a/jdbc/src/test/resources/sql/select.sql +++ b/jdbc/src/test/resources/sql/select.sql @@ -27,4 +27,4 @@ select c_Interval, c_Decimal -from #tableName +from #tableName order by key