Use psql 11 docker image in all tests (#372)

* Use psql 11 docker image in all tests
This commit is contained in:
Weimin Yu 2019-11-18 14:08:58 -05:00 committed by GitHub
parent 365c5da942
commit 05c45da07a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 34 deletions

View file

@ -75,6 +75,9 @@ public class JpaTransactionManagerRule extends ExternalResource {
private final ImmutableMap userProperties;
private static final JdbcDatabaseContainer database = create();
private static final long ACTIVE_CONNECTIONS_BASELINE =
getActiveConnectionCountByUser(database.getUsername());
;
private static final HibernateSchemaExporter exporter =
HibernateSchemaExporter.create(
database.getJdbcUrl(), database.getUsername(), database.getPassword());
@ -143,25 +146,26 @@ public class JpaTransactionManagerRule extends ExternalResource {
assertNormalActiveConnection();
}
private static long getActiveConnectionCountByUser(String userName) {
try (Connection conn = createConnection(POSTGRES_DB_NAME);
Statement statement = conn.createStatement()) {
ResultSet rs =
statement.executeQuery(
"SELECT COUNT(1) FROM pg_stat_activity WHERE usename = '" + userName + "'");
rs.next();
return rs.getLong(1);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* This function throws exception if it detects connection leak by checking the metadata table
* pg_stat_activity.
*/
private void assertNormalActiveConnection() {
try (Connection conn = createConnection(POSTGRES_DB_NAME);
Statement statement = conn.createStatement()) {
ResultSet rs =
statement.executeQuery(
"SELECT COUNT(1) FROM pg_stat_activity WHERE usename = '"
+ database.getUsername()
+ "'");
rs.next();
long activeConns = rs.getLong(1);
// There should be only 1 active connection which is executing this query
assertThat(activeConns).isEqualTo(1L);
} catch (Exception e) {
throw new RuntimeException(e);
}
assertThat(getActiveConnectionCountByUser(database.getUsername()))
.isEqualTo(ACTIVE_CONNECTIONS_BASELINE);
}
private static String readSqlInClassPath(String sqlScriptPath) {
@ -181,7 +185,7 @@ public class JpaTransactionManagerRule extends ExternalResource {
}
}
private String getJdbcUrlFor(String dbName) {
private static String getJdbcUrlFor(String dbName) {
// Disable Postgres driver use of java.util.logging to reduce noise at startup time
return "jdbc:postgresql://"
+ database.getContainerIpAddress()
@ -192,7 +196,7 @@ public class JpaTransactionManagerRule extends ExternalResource {
+ "?loggerLevel=OFF";
}
private Connection createConnection(String dbName) {
private static Connection createConnection(String dbName) {
final Properties info = new Properties();
info.put("user", database.getUsername());
info.put("password", database.getPassword());

View file

@ -18,8 +18,7 @@ public class NomulusPostgreSql {
/** The current PostgreSql version in Cloud SQL. */
// TODO(weiminyu): setup periodic checks to detect version changes in Cloud SQL.
// TODO(weiminyu): Upgrade to 11.5, which apparently breaks JpaTransactionManagerRule.
private static final String TARGET_VERSION = "9.6.12";
private static final String TARGET_VERSION = "11.5";
/** Returns the docker image tag of the targeted Postgresql server version. */
public static String getDockerTag() {

View file

@ -2,8 +2,8 @@
-- PostgreSQL database dump
--
-- Dumped from database version 9.6.12
-- Dumped by pg_dump version 9.6.12
-- Dumped from database version 11.5 (Debian 11.5-3.pgdg90+1)
-- Dumped by pg_dump version 11.5 (Debian 11.5-3.pgdg90+1)
SET statement_timeout = 0;
SET lock_timeout = 0;
@ -12,23 +12,10 @@ SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
SET default_tablespace = '';
SET default_with_oids = false;