zonkyio/embedded-postgres

EmbeddedPostgres not starting in JUnit 5 tests on Windows

zodac opened this issue · 5 comments

zodac commented

Hi, I was previously using v1.2.6, but once I updated to v1.2.8+, my unit tests are unable to start up the embedded Postgres database. I've not changed anything else besides the version of the dependency from 1.2.6 -> 1.2.8 (1.2.7 works fine). I didn't see anything in the release that might require a change.

My code is a utility class used by my JUnit tests to retrieve a Connection as follows:

public class TestDbConnectionPool {
    private final DataSource dataSource;

    public TestDbConnectionPool() {
        try {
            dataSource = EmbeddedPostgres.builder()
                .start() // <-- Failing here
                .getPostgresDatabase();
            createDatabaseTables();
        } catch (final Exception e) {
            throw new AssertionError("Unable to start test DB", e);
        }
    }
    
    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (final SQLException e) {
            throw new DatabaseConnectionException("Error opening connection", e);
        }
    }
}

The stack trace is:

Caused by: java.io.IOException: Gave up waiting for server to start after 300000ms
	at io.zonky.test.db.postgres.embedded.EmbeddedPostgres.waitForServerStartup(EmbeddedPostgres.java:331)
	at io.zonky.test.db.postgres.embedded.EmbeddedPostgres.startPostmaster(EmbeddedPostgres.java:281)
	at io.zonky.test.db.postgres.embedded.EmbeddedPostgres.<init>(EmbeddedPostgres.java:160)
	at io.zonky.test.db.postgres.embedded.EmbeddedPostgres$Builder.start(EmbeddedPostgres.java:580)
	at me.zodac.folding.db.postgres.TestDbConnectionPool.<init>(TestDbConnectionPool.java:38)
	... 69 more
Caused by: java.sql.SQLException: connect failed
	at io.zonky.test.db.postgres.embedded.EmbeddedPostgres.verifyReady(EmbeddedPostgres.java:341)
	at io.zonky.test.db.postgres.embedded.EmbeddedPostgres.waitForServerStartup(EmbeddedPostgres.java:316)
	... 73 more
Caused by: java.net.SocketTimeoutException: connect timed out
	at java.base/java.net.PlainSocketImpl.waitForConnect(Native Method)
	at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:107)
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
	at java.base/java.net.Socket.connect(Socket.java:591)
	at io.zonky.test.db.postgres.embedded.EmbeddedPostgres.verifyReady(EmbeddedPostgres.java:339)
	... 74 more

Hi, thanks for the report. Maybe the problem could be related to #66?

zodac commented

Hmm, seems like it might be. I turned on the logs and did find the same error:

[main] INFO io.zonky.test.db.postgres.embedded.EmbeddedPostgres - 4da981e5-6f24-48d8-b787-b1671b3bc4df initdb completed in 00:00:11.007
[main] INFO io.zonky.test.db.postgres.embedded.EmbeddedPostgres - 4da981e5-6f24-48d8-b787-b1671b3bc4df postmaster started as Process[pid=7272, exitValue="not exited"] on port 57830.  Waiting up to PT10S for server startup to finish.
[postgres:pid(7272)] INFO io.zonky.test.db.postgres.embedded.EmbeddedPostgres - Execution of PostgreSQL by a user with administrative permissions is not
[postgres:pid(7272)] INFO io.zonky.test.db.postgres.embedded.EmbeddedPostgres - permitted.
[postgres:pid(7272)] INFO io.zonky.test.db.postgres.embedded.EmbeddedPostgres - The server must be started under an unprivileged user ID to prevent
[postgres:pid(7272)] INFO io.zonky.test.db.postgres.embedded.EmbeddedPostgres - possible system security compromises.  See the documentation for
[postgres:pid(7272)] INFO io.zonky.test.db.postgres.embedded.EmbeddedPostgres - more information on how to properly start the server.

Is there any more information I should gather?

zodac commented

Upgraded my machine to Windows 10 (from Windows 7), and am no longer seeing this problem. Will close the issue.

Ok, thank you for letting me know.

Hello ,

For me also it is failing in the real environment, but passing in my local build.
anything related to environment based ?

private static EmbeddedPostgres embeddedPostgres;

private DataSource embeddedPostgres() throws IOException {
if (Objects.isNull(embeddedPostgres)) {
embeddedPostgres = EmbeddedPostgres.builder().start(); // failing here in the real environment
}
return embeddedPostgres.getPostgresDatabase();
}