Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Resources do not work with PostgreSQL #390

Closed
runenielsen opened this issue Sep 14, 2023 · 1 comment
Closed

Test Resources do not work with PostgreSQL #390

runenielsen opened this issue Sep 14, 2023 · 1 comment

Comments

@runenielsen
Copy link

Expected Behavior

When I create a Micronaut application like this:

mn create-app example.micronaut.test-resources-postgres-fail-no-aot --build=gradle_kotlin --features=test-resources,postgres

And comment out everything related to AOT from build.gradle.kts (to ensure Micronaut AOT is not the cause of the problem), I expect to be able to build with ./gradlew build --info.

Actual Behaviour

I get the error below every time when building.

It seems Test Resources are not able to detect correctly, when the Postgres container has actually started up, so it tries to run the test before the container is running and therefore get a connection refused, when attempting to connect to it.

If I add this to application.properties, it works:

datasources.default.initialization-fail-timeout=10000

The port number it tries to connect on is correct.

I assume this problem is related to #389

I haven't been able to find a similar workaround as the one above for Redis though.

Error:

[test-resources-service] 08:56:32.540 [default-nioEventLoopGroup-1-2] INFO  tc.postgres:latest - Container is started (JDBC URL: jdbc:postgresql://localhost:32783/test?loggerLevel=OFF)

> Task :test
    08:56:32.577 [Test worker] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
    08:56:33.604 [Test worker] ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
    org.postgresql.util.PSQLException: Connection to localhost:32783 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:342)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:54)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:263)
        at org.postgresql.Driver.makeConnection(Driver.java:443)
        at org.postgresql.Driver.connect(Driver.java:297)
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359)
        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201)
        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100)
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
        at io.micronaut.configuration.jdbc.hikari.HikariUrlDataSource.<init>(HikariUrlDataSource.java:35)
        at io.micronaut.configuration.jdbc.hikari.DatasourceFactory.dataSource(DatasourceFactory.java:66)
        at io.micronaut.configuration.jdbc.hikari.$DatasourceFactory$DataSource0$Definition.instantiate(Unknown Source)
        at io.micronaut.context.BeanDefinitionDelegate.instantiate(BeanDefinitionDelegate.java:159)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2307)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2277)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2289)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3056)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:81)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2958)
        at io.micronaut.context.DefaultBeanContext.initializeEagerBean(DefaultBeanContext.java:2667)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1973)
        at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:279)
        at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3340)
        at io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3693)
        at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:345)
        at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:191)
        at io.micronaut.test.extensions.AbstractMicronautExtension.startApplicationContext(AbstractMicronautExtension.java:489)
        at io.micronaut.test.extensions.AbstractMicronautExtension.beforeClass(AbstractMicronautExtension.java:327)
        at io.micronaut.test.extensions.junit5.MicronautJunit5Extension.beforeAll(MicronautJunit5Extension.java:84)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$12(ClassBasedTestDescriptor.java:396)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllCallbacks(ClassBasedTestDescriptor.java:396)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:212)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:85)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:148)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
        at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
        at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
        at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:198)
        at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:169)
        at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:93)
        at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:58)
        at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:141)
        at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:57)
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:103)
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:85)
        at org.junit.platform.launcher.core.DelegatingLauncher.execute(DelegatingLauncher.java:47)
        at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:110)
        at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:90)
        at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:85)
        at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:62)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
        at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
        at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
        at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
        at jdk.proxy1/jdk.proxy1.$Proxy2.stop(Unknown Source)
        at org.gradle.api.internal.tasks.testing.worker.TestWorker$3.run(TestWorker.java:193)
        at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
        at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
        at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
        at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
        at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:113)
        at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:65)
        at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
        at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
    Caused by: java.net.ConnectException: Connection refused
        at java.base/sun.nio.ch.Net.pollConnect(Native Method)
        at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
        at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:554)
        at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:602)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
        at java.base/java.net.Socket.connect(Socket.java:633)
        at org.postgresql.core.PGStream.createSocket(PGStream.java:243)
        at org.postgresql.core.PGStream.<init>(PGStream.java:98)
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:132)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:258)
        ... 88 common frames omitted

Gradle Test Executor 1 finished executing tests.

> Task :test FAILED

TestResourcesPostgresFailNoAotTest > initializationError FAILED
    io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [javax.sql.DataSource] could not be loaded: Error instantiating bean of type  [javax.sql.DataSource]

    Message: Failed to initialize pool: Connection to localhost:32783 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    Path Taken: DatasourceFactory.dataSource(DatasourceConfiguration datasourceConfiguration)
        at app//io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1979)
        at app//io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:279)
        at app//io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3340)
        at app//io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3693)
        at app//io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:345)
        at app//io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:191)
        at app//io.micronaut.test.extensions.AbstractMicronautExtension.startApplicationContext(AbstractMicronautExtension.java:489)
        at app//io.micronaut.test.extensions.AbstractMicronautExtension.beforeClass(AbstractMicronautExtension.java:327)
        at app//io.micronaut.test.extensions.junit5.MicronautJunit5Extension.beforeAll(MicronautJunit5Extension.java:84)
        at [email protected]/java.util.ArrayList.forEach(ArrayList.java:1511)

        Caused by:
        io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [javax.sql.DataSource]

        Message: Failed to initialize pool: Connection to localhost:32783 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
        Path Taken: DatasourceFactory.dataSource(DatasourceConfiguration datasourceConfiguration)
            at app//io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2322)
            at app//io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2277)
            at app//io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2289)
            at app//io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3056)
            at app//io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:81)
            at app//io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2958)
            at app//io.micronaut.context.DefaultBeanContext.initializeEagerBean(DefaultBeanContext.java:2667)
            at app//io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1973)
            ... 9 more

            Caused by:
            com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Connection to localhost:32783 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
                at com.zaxxer.hikari.pool.HikariPool.throwPoolInitializationException(HikariPool.java:596)
                at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:582)
                at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100)
                at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
                at io.micronaut.configuration.jdbc.hikari.HikariUrlDataSource.<init>(HikariUrlDataSource.java:35)
                at io.micronaut.configuration.jdbc.hikari.DatasourceFactory.dataSource(DatasourceFactory.java:66)
                at io.micronaut.configuration.jdbc.hikari.$DatasourceFactory$DataSource0$Definition.instantiate(Unknown Source)
                at io.micronaut.context.BeanDefinitionDelegate.instantiate(BeanDefinitionDelegate.java:159)
                at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2307)
                ... 16 more

                Caused by:
                org.postgresql.util.PSQLException: Connection to localhost:32783 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
                    at app//org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:342)
                    at app//org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:54)
                    at app//org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:263)
                    at app//org.postgresql.Driver.makeConnection(Driver.java:443)
                    at app//org.postgresql.Driver.connect(Driver.java:297)
                    at app//com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
                    at app//com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359)
                    at app//com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201)
                    at app//com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470)
                    at app//com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561)
                    ... 23 more

                    Caused by:
                    java.net.ConnectException: Connection refused
                        at java.base/sun.nio.ch.Net.pollConnect(Native Method)
                        at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
                        at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:554)
                        at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:602)
                        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
                        at java.base/java.net.Socket.connect(Socket.java:633)
                        at org.postgresql.core.PGStream.createSocket(PGStream.java:243)
                        at org.postgresql.core.PGStream.<init>(PGStream.java:98)
                        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:132)
                        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:258)
                        ... 32 more

1 test completed, 1 failed
Finished generating test XML results (0.017 secs) into: .../test-resources-postgres-fail-no-aot/build/test-results/test
Generating HTML test report...
Finished generating test html results (0.009 secs) into: .../test-resources-postgres-fail-no-aot/build/reports/tests/test

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file://.../test-resources-postgres-fail-no-aot/build/reports/tests/test/index.html

* Try:
> Run with --scan to get full insights.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.2.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 10s

Steps To Reproduce

  1. Clone the project https://github.com/runenielsen/test-resources-postgres-fail-no-aot.
  2. Run ./gradlew build --info.
  3. See the build fail.
  4. Comment the line datasources.default.initialization-fail-timeout=10000 back in in application.properties.
  5. Run ./gradlew build --info.
  6. See the build succeed.

Environment Information

OS: MacOS Ventura 13.5.2
JDK: OpenJDK Temurin 17.0.8.1
Docker installed via Rancher Desktop 1.9.1 with dockerd container engine

Example Application

https://github.com/runenielsen/test-resources-postgres-fail-no-aot

Version

4.1.0

@runenielsen
Copy link
Author

runenielsen commented Sep 14, 2023

Adding

test-resources.containers.postgres.wait-strategy=port

fixes the problem. According to the documentation, this is supposed to be the default setting, but apparently it is not. I will report this in #389, that is caused by the same problem.

Closing this since it is a duplicate of #389

@runenielsen runenielsen closed this as not planned Won't fix, can't repro, duplicate, stale Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant