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 default wait strategy is not "port" as specified in the documentation, and this makes them not work correctly #389

Open
runenielsen opened this issue Sep 13, 2023 · 0 comments

Comments

@runenielsen
Copy link

runenielsen commented Sep 13, 2023

Expected Behavior

Test Resources default wait strategy is not "port" as specified in the documentation under "Wait strategies", and this makes them not work correctly in some environments.

I expect the default wait strategy for Test Containers created by Test Resources to be "port" as specified in the documentation.

When I create a Micronaut application like this:

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

... and:

  1. Comment out redis.uri in application.properties.
  2. Comment out the aot plugin from build.gradle.kts.
  3. Add a test using a StatefulRedisConnection from Lettuce.

I expect the application to be able to build with ./gradlew build --info and the test to be able to connect to Redis.

Actual Behaviour

I get the error below every time when building.

It seems Test Resources are not able to detect correctly, when the Redis 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 test-resources.containers.redis.wait-strategy=port to application.properties, the application builds fine afterwards.

This must mean, that the default wait strategy for containers created by Test Resources is not set to "port" as specified in the documentation under "Wait strategies".

I am using a new MacBook, which is very fast. I have also tested it on a slower Windows computer, where the error below doesn't occur.

Error:

RedisTest > testRedis() FAILED
    org.junit.jupiter.api.extension.ParameterResolutionException: Failed to resolve parameter [io.lettuce.core.api.StatefulRedisConnection<java.lang.String, java.lang.String> redisConnection] in constructor [public example.micronaut.RedisTest(io.lettuce.core.api.StatefulRedisConnection<java.lang.String, java.lang.String>)]: Error instantiating bean of type  [io.lettuce.core.api.StatefulRedisConnection]

    Message: Unable to connect to localhost/<unresolved>:32817
    Path Taken: DefaultRedisClientFactory.redisConnection(RedisClient redisClient)
        at [email protected]/java.util.Optional.orElseGet(Optional.java:364)
        at [email protected]/java.util.ArrayList.forEach(ArrayList.java:1511)
        at [email protected]/java.util.ArrayList.forEach(ArrayList.java:1511)

        Caused by:
        io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [io.lettuce.core.api.StatefulRedisConnection]

        Message: Unable to connect to localhost/<unresolved>:32817
        Path Taken: DefaultRedisClientFactory.redisConnection(RedisClient redisClient)
            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.resolveBeanRegistration(DefaultBeanContext.java:2919)
            at app//io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2730)
            at app//io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1693)
            at app//io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:840)
            at app//io.micronaut.test.extensions.junit5.MicronautJunit5Extension.resolveParameter(MicronautJunit5Extension.java:380)
            ... 3 more

            Caused by:
            io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:32817
                at app//io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)
                at app//io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
                at app//io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:350)
                at app//io.lettuce.core.RedisClient.connect(RedisClient.java:216)
                at app//io.micronaut.configuration.lettuce.AbstractRedisClientFactory.redisConnection(AbstractRedisClientFactory.java:88)
                at app//io.micronaut.configuration.lettuce.DefaultRedisClientFactory.redisConnection(DefaultRedisClientFactory.java:68)
                at app//io.micronaut.configuration.lettuce.$DefaultRedisClientFactory$RedisConnection1$Definition.instantiate(Unknown Source)
                at app//io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2307)
                ... 13 more

                Caused by:
                io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:32817

                    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.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946)
                        at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:337)
                        at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
                        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776)
                        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
                        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
                        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
                        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
                        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
                        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
                        at java.base/java.lang.Thread.run(Thread.java:833)

Steps To Reproduce

Note: as stated above, this only fails in some environments, my best guess is it depends on the speed of the computer, it is running on. It fails every time in the environment specified below.

  1. Clone https://github.com/runenielsen/test-resources-redis-fail-no-aot.
  2. Inspect the very simple test class RedisTest and verify that it should succeed when run.
  3. Run ./gradlew build --info.
  4. See that the build fails with the error above.
  5. Comment test-resources.containers.redis.wait-strategy=port in in application.properties.
  6. Run ./gradlew build --info.
  7. See that the build now succeeds.

Environment Information

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

Example Application

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

Version

4.1.0

@runenielsen runenielsen changed the title Test Resources do not work with Redis Test Resources default wait strategy is not "port" as specified in the documentation, and this makes them not work correctly 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