Skip to content

Commit

Permalink
Remove testing code from SQLQuery class
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Rögner <benjamin.roegner@here.com>
  • Loading branch information
roegi authored and mchrza committed Jan 27, 2025
1 parent 517a9af commit 6f952ea
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
19 changes: 6 additions & 13 deletions xyz-util/src/main/java/com/here/xyz/util/db/SQLQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -887,20 +887,13 @@ public static boolean isRunning(DataSourceProvider dataSourceProvider, boolean u
*/
public static boolean isRunning(DataSourceProvider dataSourceProvider, boolean useReplica, String labelIdentifier, String labelValue)
throws SQLException {
return pgActivityQuery(dataSourceProvider, useReplica, "active", labelIdentifier, labelValue);
}

public static boolean pgActivityQuery(DataSourceProvider dataSourceProvider, boolean useReplica,
String state, String labelIdentifier, String labelValue)
throws SQLException {
return new SQLQuery("""
SELECT 1 FROM pg_stat_activity
WHERE state = #{state} AND ${{labelMatching}} AND pid != pg_backend_pid()
WHERE state = 'active' AND ${{labelMatching}} AND pid != pg_backend_pid()
""")
.withQueryFragment("labelMatching", buildLabelMatchQuery(labelIdentifier, labelValue))
.withNamedParameter("state", state)
.withLoggingEnabled(false)
.run(dataSourceProvider, rs -> rs.next(), useReplica);
.withQueryFragment("labelMatching", buildLabelMatchQuery(labelIdentifier, labelValue))
.withLoggingEnabled(false)
.run(dataSourceProvider, rs -> rs.next(), useReplica);
}

private static String getClashing(Map<String, ?> map1, Map<String, ?> map2) {
Expand Down Expand Up @@ -998,7 +991,7 @@ private enum ExecutionOperation {
UPDATE_BATCH
}

private static List<String> PwdPrefixList =
private static List<String> PwdPrefixList =
List.of("6URYTnCc", "pUNuBxnW", "JELgvJWS", "0n1UKjIv", "2YW9D4Kz", "3ZX9D4Kz", "9JwYhcgD", "qvukzFHW", "1CpZNKpG", "kwPU00Qy", "AhYtSea7", "AsSrbSE6");

private static String hidePwds( String s )
Expand Down Expand Up @@ -1278,7 +1271,7 @@ public Object handle(ResultSet rs) throws SQLException {
if (!calledBefore) {
calledBefore = true;
//TODO: using runWriteQueryAsync together with using query context throws NPE due to returned null value
return null;
return null;
}
return originalHandler.handle(rs);
}
Expand Down
12 changes: 12 additions & 0 deletions xyz-util/src/test/java/com/here/xyz/test/SQLITBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

package com.here.xyz.test;

import com.here.xyz.util.db.SQLQuery;
import com.here.xyz.util.db.datasource.DataSourceProvider;
import com.here.xyz.util.db.datasource.DatabaseSettings;
import com.here.xyz.util.db.datasource.DatabaseSettings.ScriptResourcePath;
import com.here.xyz.util.db.datasource.PooledDataSources;
import java.sql.SQLException;
import java.util.List;

public class SQLITBase {
Expand All @@ -46,4 +48,14 @@ protected static DataSourceProvider getDataSourceProvider() {
protected static DataSourceProvider getDataSourceProvider(DatabaseSettings dbSettings) {
return new PooledDataSources(dbSettings);
}

protected static boolean connectionIsIdle(DataSourceProvider dsp, String queryId) throws SQLException {
return new SQLQuery("""
SELECT 1 FROM pg_stat_activity
WHERE state = 'idle' AND query LIKE '%${{queryId}}%' AND pid != pg_backend_pid()
""")
.withQueryFragment("queryId", queryId)
.withLoggingEnabled(false)
.run(dsp, rs -> rs.next());
}
}
15 changes: 6 additions & 9 deletions xyz-util/src/test/java/com/here/xyz/test/SQLQueryIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,13 @@ public void runAsyncQueryAndCheckIfIdleConnectionIsClearedInErrorCase() throws E
//Start the query and directly close the connection
try (DataSourceProvider dsp = getDataSourceProvider()) {
asyncQuery.run(dsp);

while(SQLQuery.isRunning(dsp, false, asyncQuery.getQueryId()))
Thread.sleep(50);
}

//No Idle connection should be present
try (DataSourceProvider dsp = getDataSourceProvider()) {
assertFalse(SQLQuery.pgActivityQuery(dsp, false, "idle", asyncQuery.getQueryId(), SQLQuery.QUERY_ID));
while (SQLQuery.isRunning(dsp, false, asyncQuery.getQueryId()))
Thread.sleep(50);
assertFalse(connectionIsIdle(dsp, asyncQuery.getQueryId()));
}
}

Expand All @@ -112,15 +111,13 @@ public void runAsyncQueryAndCheckIfIdleConnectionIsClearedInSuccessCase() throws
//Start the query and directly close the connection
try (DataSourceProvider dsp = getDataSourceProvider()) {
asyncQuery.run(dsp);

while(SQLQuery.isRunning(dsp, false, asyncQuery.getQueryId())){
Thread.sleep(50);
}
}

//No Idle connection should be present
try (DataSourceProvider dsp = getDataSourceProvider()) {
assertFalse(SQLQuery.pgActivityQuery(dsp, false, "idle", asyncQuery.getQueryId(), SQLQuery.QUERY_ID));
while (SQLQuery.isRunning(dsp, false, asyncQuery.getQueryId()))
Thread.sleep(50);
assertFalse(connectionIsIdle(dsp, asyncQuery.getQueryId()));
}
}

Expand Down

0 comments on commit 6f952ea

Please sign in to comment.