zonkyio / embedded-database-spring-test

A library for creating isolated embedded databases for Spring-powered integration tests.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to release database warning

vbezhenar opened this issue · comments

After tests are run, I receive the following warning:

{2022-09-06 23:57:32,588} level=WARN logger=io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider thread=ForkJoinPool.commonPool-worker-1 
Unable to release 'aihloqghwiau' database
org.postgresql.util.PSQLException: FATAL: terminating connection due to administrator command
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2675)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2365)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:355)
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:490)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:408)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:167)
	at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:156)
	at io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider$DatabaseInstance.executeStatement(ZonkyPostgresDatabaseProvider.java:190)
	at io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider$DatabaseInstance.lambda$dropDatabase$0(ZonkyPostgresDatabaseProvider.java:176)
	at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804)
	at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1796)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)

It works for fairly bare-banes spring boot application.

Here's source code, whole application is attached as zip.

It's not always reproduced, but it reproduced often. I'm using macOS on M1.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
    </parent>

    <groupId>spring-test</groupId>
    <artifactId>spring-test</artifactId>
    <version>0.0.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
        <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.zonky.test.postgres</groupId>
                <artifactId>embedded-postgres-binaries-bom</artifactId>
                <version>14.5.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.zonky.test</groupId>
            <artifactId>embedded-database-spring-test</artifactId>
            <version>2.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.zonky.test</groupId>
            <artifactId>embedded-postgres</artifactId>
            <version>2.0.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.zonky.test.postgres</groupId>
            <artifactId>embedded-postgres-binaries-darwin-arm64v8</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
spring:
  datasource:
    url: jdbc:postgresql://127.0.0.1:55432/test
    username: test
    password: test
package kz.doubleservices.healthbus.zonkytest;

import io.zonky.test.db.AutoConfigureEmbeddedDatabase;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import springtest.SpringTestApplication;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.ZONKY;
import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest(classes = SpringTestApplication.class)
@AutoConfigureEmbeddedDatabase(provider = ZONKY, type = POSTGRES)
public class ZonkyTest {

    @Autowired
    private DataSource dataSource;

    @Test
    public void test11() throws SQLException {
        test("test11", dataSource);
    }

    @Test
    public void test12() throws SQLException {
        test("test12", dataSource);
    }

    static void test(String name, DataSource dataSource) throws SQLException {
        System.out.println(name + ": dataSource=" + dataSource);
        try (Connection connection = dataSource.getConnection();
             Statement statement = connection.createStatement()) {
            try (ResultSet resultSet = statement.executeQuery("select current_user, current_database()")) {
                assertTrue(resultSet.next());
                System.out.println(name + ": " + resultSet.getString(1) + " " + resultSet.getString(2));
                assertFalse(resultSet.next());
            }
            try (ResultSet resultSet = statement.executeQuery("""
                    select tablename from pg_catalog.pg_tables where schemaname = 'public'
                    """)) {
                while (resultSet.next()) {
                    System.out.println(name + ": " + resultSet.getString(1));
                }
            }
            statement.executeUpdate("create table " + name + " (id text)");
        }
    }
}
% mvn test
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< spring-test:spring-test >-----------------------
[INFO] Building spring-test 0.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ spring-test ---
[INFO] Using 'utf-8' encoding to copy filtered resources.
[INFO] Using 'utf-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ spring-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ spring-test ---
[INFO] Using 'utf-8' encoding to copy filtered resources.
[INFO] Using 'utf-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ spring-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/vbezhenar/projects/test/spring-test/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ spring-test ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running kz.doubleservices.healthbus.zonkytest.ZonkyTest

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.3)


{2022-09-07 00:02:23,477} level=WARN logger=org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration$JpaWebConfiguration thread=main 
spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
test11: dataSource=io.zonky.test.db.provider.support.BlockingDatabaseWrapper@5e055ce1
test11: postgres lvoegvaessgb
test12: dataSource=io.zonky.test.db.provider.support.BlockingDatabaseWrapper@5e055ce1
test12: postgres lvoegvaessgb
test12: test11
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.199 s - in kz.doubleservices.healthbus.zonkytest.ZonkyTest

{2022-09-07 00:02:23,852} level=WARN logger=io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider thread=ForkJoinPool.commonPool-worker-1 
Unable to release 'lvoegvaessgb' database
org.postgresql.util.PSQLException: FATAL: terminating connection due to administrator command
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2675)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2365)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:355)
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:490)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:408)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:167)
	at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:156)
	at io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider$DatabaseInstance.executeStatement(ZonkyPostgresDatabaseProvider.java:190)
	at io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider$DatabaseInstance.lambda$dropDatabase$0(ZonkyPostgresDatabaseProvider.java:176)
	at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804)
	at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1796)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.954 s
[INFO] Finished at: 2022-09-07T00:02:24+06:00
[INFO] ------------------------------------------------------------------------

spring-test.zip

Hi @vbezhenar, thanks for the report and for the reproducer. It always makes solving a problem much easier.

The problem has been fixed. The warnings were just false positives caused by shutting down the postgres process at the end of the test suite.