Skip to content

Commit 65e40fb

Browse files
SNOW-1003125: Fix flaky test DatabaseMetaDataIT.testGetSchemas (#1614)
1 parent 15b3aca commit 65e40fb

File tree

4 files changed

+89
-40
lines changed

4 files changed

+89
-40
lines changed

src/test/java/net/snowflake/client/TestUtil.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ public class TestUtil {
2222
private static final Pattern QUERY_ID_REGEX =
2323
Pattern.compile("[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}");
2424

25+
public static final String GENERATED_SCHEMA_PREFIX = "GENERATED_";
26+
public static final String ESCAPED_GENERATED_SCHEMA_PREFIX =
27+
GENERATED_SCHEMA_PREFIX.replaceAll("_", "\\\\_");
28+
private static final String GITHUB_SCHEMA_PREFIX =
29+
"GITHUB_"; // created by JDBC CI jobs before tests
30+
private static final String GITHUB_JOB_SCHEMA_PREFIX =
31+
"GH_JOB_"; // created by other drivers e.g. Python driver
32+
33+
public static boolean isSchemaGeneratedInTests(String schema) {
34+
return schema.startsWith(TestUtil.GITHUB_SCHEMA_PREFIX)
35+
|| schema.startsWith(TestUtil.GITHUB_JOB_SCHEMA_PREFIX)
36+
|| schema.startsWith(TestUtil.GENERATED_SCHEMA_PREFIX);
37+
}
38+
2539
/**
2640
* Util function to assert a piece will throw exception and assert on the error code
2741
*
@@ -99,7 +113,8 @@ public static void withSchema(Statement statement, String schemaName, ThrowingRu
99113
*/
100114
public static void withRandomSchema(Statement statement, ThrowingConsumer<String> action)
101115
throws Exception {
102-
String customSchema = "TEST_SCHEMA_" + SnowflakeUtil.randomAlphaNumeric(5);
116+
String customSchema =
117+
GENERATED_SCHEMA_PREFIX + SnowflakeUtil.randomAlphaNumeric(5).toUpperCase();
103118
try {
104119
statement.execute("CREATE OR REPLACE SCHEMA " + customSchema);
105120
action.call(customSchema);

src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java

+30-20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static java.sql.ResultSetMetaData.columnNullableUnknown;
88
import static org.hamcrest.Matchers.equalTo;
99
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
10+
import static org.hamcrest.Matchers.hasItem;
1011
import static org.junit.Assert.*;
1112

1213
import com.google.common.base.Strings;
@@ -139,41 +140,50 @@ public void testGetSchemas() throws Throwable {
139140
// CLIENT_METADATA_REQUEST_USE_CONNECTION_CTX = false
140141
try (Connection connection = getConnection()) {
141142
DatabaseMetaData metaData = connection.getMetaData();
143+
String currentSchema = connection.getSchema();
142144
assertEquals("schema", metaData.getSchemaTerm());
143-
ResultSet resultSet = metaData.getSchemas();
144-
verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_SCHEMAS);
145-
146145
Set<String> schemas = new HashSet<>();
147-
while (resultSet.next()) {
148-
schemas.add(resultSet.getString(1));
146+
try (ResultSet resultSet = metaData.getSchemas()) {
147+
verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_SCHEMAS);
148+
while (resultSet.next()) {
149+
String schema = resultSet.getString(1);
150+
if (currentSchema.equals(schema) || !TestUtil.isSchemaGeneratedInTests(schema)) {
151+
schemas.add(schema);
152+
}
153+
}
149154
}
150-
resultSet.close();
151-
assertThat(schemas.size(), greaterThanOrEqualTo(1)); // one or more schemas
155+
assertThat(schemas.size(), greaterThanOrEqualTo(1));
152156

153157
Set<String> schemasInDb = new HashSet<>();
154-
resultSet = metaData.getSchemas(connection.getCatalog(), "%");
155-
while (resultSet.next()) {
156-
schemasInDb.add(resultSet.getString(1));
158+
try (ResultSet resultSet = metaData.getSchemas(connection.getCatalog(), "%")) {
159+
while (resultSet.next()) {
160+
String schema = resultSet.getString(1);
161+
if (currentSchema.equals(schema) || !TestUtil.isSchemaGeneratedInTests(schema)) {
162+
schemasInDb.add(schema);
163+
}
164+
}
157165
}
158-
assertThat(schemasInDb.size(), greaterThanOrEqualTo(1)); // one or more schemas
166+
assertThat(schemasInDb.size(), greaterThanOrEqualTo(1));
159167
assertThat(schemas.size(), greaterThanOrEqualTo(schemasInDb.size()));
160-
assertTrue(schemas.containsAll(schemasInDb));
161-
assertTrue(schemas.contains(connection.getSchema()));
168+
schemasInDb.forEach(schemaInDb -> assertThat(schemas, hasItem(schemaInDb)));
169+
assertTrue(schemas.contains(currentSchema));
170+
assertTrue(schemasInDb.contains(currentSchema));
162171
}
163172

164173
// CLIENT_METADATA_REQUEST_USE_CONNECTION_CTX = true
165-
try (Connection connection = getConnection()) {
166-
Statement statement = connection.createStatement();
174+
try (Connection connection = getConnection();
175+
Statement statement = connection.createStatement()) {
167176
statement.execute("alter SESSION set CLIENT_METADATA_REQUEST_USE_CONNECTION_CTX=true");
168177

169178
DatabaseMetaData metaData = connection.getMetaData();
170179
assertEquals("schema", metaData.getSchemaTerm());
171-
ResultSet resultSet = metaData.getSchemas();
172-
Set<String> schemas = new HashSet<>();
173-
while (resultSet.next()) {
174-
schemas.add(resultSet.getString(1));
180+
try (ResultSet resultSet = metaData.getSchemas()) {
181+
Set<String> schemas = new HashSet<>();
182+
while (resultSet.next()) {
183+
schemas.add(resultSet.getString(1));
184+
}
185+
assertThat(schemas.size(), equalTo(1));
175186
}
176-
assertThat(schemas.size(), equalTo(1)); // more than 1 schema
177187
}
178188
}
179189

src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java

+40-18
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,20 @@ public void testDoubleQuotedDatabaseAndSchema() throws Exception {
171171
// To query the schema and table, we can use a normal java escaped quote. Wildcards are also
172172
// escaped here
173173
String schemaRandomPart = SnowflakeUtil.randomAlphaNumeric(5);
174-
String querySchema = "TEST\\_SCHEMA\\_\"WITH\\_QUOTES" + schemaRandomPart + "\"";
174+
String querySchema =
175+
TestUtil.ESCAPED_GENERATED_SCHEMA_PREFIX
176+
+ "TEST\\_SCHEMA\\_\"WITH\\_QUOTES"
177+
+ schemaRandomPart
178+
+ "\"";
175179
String queryTable = "TESTTABLE\\_\"WITH\\_QUOTES\"";
176180
// Create the schema and table. With SQL commands, double quotes must be escaped with another
177181
// quote
178-
String schemaName = "\"TEST_SCHEMA_\"\"WITH_QUOTES" + schemaRandomPart + "\"\"\"";
182+
String schemaName =
183+
"\""
184+
+ TestUtil.GENERATED_SCHEMA_PREFIX
185+
+ "TEST_SCHEMA_\"\"WITH_QUOTES"
186+
+ schemaRandomPart
187+
+ "\"\"\"";
179188
TestUtil.withSchema(
180189
statement,
181190
schemaName,
@@ -367,24 +376,26 @@ public void testGetFunctionSqlInjectionProtection() throws Throwable {
367376
*/
368377
@Test
369378
public void testGetProcedureColumnsWildcards() throws SQLException {
370-
try (Connection con = getConnection()) {
371-
Statement statement = con.createStatement();
379+
try (Connection con = getConnection();
380+
Statement statement = con.createStatement()) {
372381
String database = con.getCatalog();
373-
String schema1 = "SCH1";
374-
String schema2 = "SCH2";
382+
String schemaPrefix =
383+
TestUtil.GENERATED_SCHEMA_PREFIX + SnowflakeUtil.randomAlphaNumeric(5).toUpperCase();
384+
String schema1 = schemaPrefix + "SCH1";
385+
String schema2 = schemaPrefix + "SCH2";
375386
// Create 2 schemas, each with the same stored procedure declared in them
376387
statement.execute("create or replace schema " + schema1);
377388
statement.execute(TEST_PROC);
378389
statement.execute("create or replace schema " + schema2);
379390
statement.execute(TEST_PROC);
380391
DatabaseMetaData metaData = con.getMetaData();
381-
ResultSet rs = metaData.getProcedureColumns(database, "SCH_", "TESTPROC", "PARAM1");
382-
// Assert 4 rows returned for the param PARAM1 that's present in each of the 2 identical
383-
// stored procs in different schemas. A result row is returned for each procedure, making the
384-
// total rowcount 4
385-
assertEquals(4, getSizeOfResultSet(rs));
386-
rs.close();
387-
statement.close();
392+
try (ResultSet rs =
393+
metaData.getProcedureColumns(database, schemaPrefix + "SCH_", "TESTPROC", "PARAM1")) {
394+
// Assert 4 rows returned for the param PARAM1 that's present in each of the 2 identical
395+
// stored procs in different schemas. A result row is returned for each procedure, making
396+
// the total rowcount 4
397+
assertEquals(4, getSizeOfResultSet(rs));
398+
}
388399
}
389400
}
390401

@@ -453,7 +464,7 @@ public void testGetStringValueFromColumnDef() throws SQLException {
453464

454465
@Test
455466
public void testGetColumnsNullable() throws Throwable {
456-
try (Connection connection = getConnection()) {
467+
try (Connection connection = getConnection(); ) {
457468
String database = connection.getCatalog();
458469
String schema = connection.getSchema();
459470
final String targetTable = "T0";
@@ -484,8 +495,8 @@ public void testSessionDatabaseParameter() throws Throwable {
484495
String altdb = "ALTERNATEDB";
485496
String altschema1 = "ALTERNATESCHEMA1";
486497
String altschema2 = "ALTERNATESCHEMA2";
487-
try (Connection connection = getConnection()) {
488-
Statement statement = connection.createStatement();
498+
try (Connection connection = getConnection();
499+
Statement statement = connection.createStatement()) {
489500
String catalog = connection.getCatalog();
490501
String schema = connection.getSchema();
491502
statement.execute("create or replace database " + altdb);
@@ -905,7 +916,12 @@ public void testHandlingSpecialChars() throws Exception {
905916
statement.execute("INSERT INTO \"TEST\\1\\_1\" (\"C%1\",\"C\\1\\\\11\") VALUES (0,0)");
906917
// test getColumns with escaped special characters in schema and table name
907918
String specialSchemaSuffix = SnowflakeUtil.randomAlphaNumeric(5);
908-
String specialSchema = "\"SPECIAL%_\\SCHEMA" + specialSchemaSuffix + "\"";
919+
String specialSchema =
920+
"\""
921+
+ TestUtil.GENERATED_SCHEMA_PREFIX
922+
+ "SPECIAL%_\\SCHEMA"
923+
+ specialSchemaSuffix
924+
+ "\"";
909925
TestUtil.withSchema(
910926
statement,
911927
specialSchema,
@@ -939,7 +955,13 @@ public void testHandlingSpecialChars() throws Exception {
939955

940956
String escapedTable2 = "TEST" + escapeChar + "_1" + escapeChar + "_1";
941957
String escapedSchema =
942-
"SPECIAL%" + escapeChar + "_" + escapeChar + "\\SCHEMA" + specialSchemaSuffix;
958+
TestUtil.ESCAPED_GENERATED_SCHEMA_PREFIX
959+
+ "SPECIAL%"
960+
+ escapeChar
961+
+ "_"
962+
+ escapeChar
963+
+ "\\SCHEMA"
964+
+ specialSchemaSuffix;
943965
resultSet = metaData.getColumns(database, escapedSchema, escapedTable2, null);
944966
assertTrue(resultSet.next());
945967
assertEquals("RNUM", resultSet.getString("COLUMN_NAME"));

src/test/java/net/snowflake/client/jdbc/StatementLatestIT.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ public void testPreparedStatementLogging() throws SQLException {
229229

230230
@Test // SNOW-647217
231231
public void testSchemaWith255CharactersDoesNotCauseException() throws SQLException {
232-
String schemaName = "a" + SnowflakeUtil.randomAlphaNumeric(254);
232+
String schemaName =
233+
TestUtil.GENERATED_SCHEMA_PREFIX
234+
+ SnowflakeUtil.randomAlphaNumeric(255 - TestUtil.GENERATED_SCHEMA_PREFIX.length());
233235
try (Connection con = getConnection()) {
234236
try (Statement stmt = con.createStatement()) {
235237
stmt.execute("create schema " + schemaName);

0 commit comments

Comments
 (0)