Bjorn Kristensson
Bjorn Kristensson

Reputation: 65

RabbitMQ Testcontainer drops connection upon teststart

I'm having same problems as this question with rabbitmq dropping connection same second as it has started. I have a minimal project on github.

It is a Spring Boot project where two docker containers are running. One with RMQ and one with psql. Using the containers when running the project is no problem, just the rabbitmq testcontainer that seem to be unstable. I did try to have the container wrapped in a generic container instead, with same result.

The first test to see if the containers are up and running passes, so it seems to be something with the amqp connection only.

Configclass:

@SpringBootTest(classes = TestContainersDemoApplication.class)
@Testcontainers
@AutoConfigureMockMvc
@ExtendWith(SpringExtension.class)
public abstract class TestContainersConfig  {

    @Autowired
    public MockMvc mockMvc;

    @Container
    public static final RabbitMQContainer rabbitMQContainer = new RabbitMQContainer("rabbitmq:3.8-management-alpine");

    @Container
    public static PostgreSQLContainer sqlContainer = new PostgreSQLContainer("postgres:latest")
                .withDatabaseName("demo")
                .withUsername("postgres")
                .withPassword("postgres");


    @DynamicPropertySource
    static void registerProperties(DynamicPropertyRegistry dynamicPropertyRegistry) {
        dynamicPropertyRegistry.add("spring.datasource.url",  () -> sqlContainer.getJdbcUrl());
        dynamicPropertyRegistry.add("spring.datasource.username", () -> sqlContainer.getUsername());
        dynamicPropertyRegistry.add("spring.datasource.password", () -> sqlContainer.getPassword());
        dynamicPropertyRegistry.add("spring.rabbitmq.host", rabbitMQContainer::getHost);
        dynamicPropertyRegistry.add("spring.rabbitmq.port", rabbitMQContainer::getAmqpPort);
    }

    static  {
        Startables.deepStart(Stream.of(rabbitMQContainer, sqlContainer)).join();
    }
}

The stacktrace seem to be identical as the linked question:

2022-11-18 21:00:23.555  INFO 441007 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Broker not available; cannot force queue declarations during start: java.io.IOException
2022-11-18 21:00:23.559  INFO 441007 --- [ntContainer#0-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:49596]
2022-11-18 21:00:23.770  WARN 441007 --- [127.0.0.1:49596] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occurred (Exception message: Socket closed)
2022-11-18 21:00:23.773 ERROR 441007 --- [ntContainer#0-1] o.s.a.r.l.SimpleMessageListenerContainer : Failed to check/redeclare auto-delete queue(s).
org.springframework.amqp.AmqpIOException: java.io.IOException
    at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:70) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:602) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:725) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:252) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2180) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2153) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2133) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:463) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:447) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1930) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1911) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.initialize(SimpleMessageListenerContainer.java:1377) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1223) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.io.IOException: null
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:129) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:125) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:396) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1225) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1173) ~[amqp-client-5.14.2.jar:5.14.2]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connectAddresses(AbstractConnectionFactory.java:640) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:615) ~[spring-rabbit-2.4.7.jar:2.4.7]
    at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:565) ~[spring-rabbit-2.4.7.jar:2.4.7]
    ... 12 common frames omitted
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:502) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:326) ~[amqp-client-5.14.2.jar:5.14.2]
    ... 17 common frames omitted
Caused by: java.io.EOFException: null
    at java.base/java.io.DataInputStream.readUnsignedByte(DataInputStream.java:296) ~[na:na]
    at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:91) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:184) ~[amqp-client-5.14.2.jar:5.14.2]
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:665) ~[amqp-client-5.14.2.jar:5.14.2]
    ... 1 common frames omitted
2022-11-18 21:00:23.774  INFO 441007 --- [ntContainer#0-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:49596]
2022-11-18 21:00:23.978  WARN 441007 --- [127.0.0.1:49596] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occurred (Exception message: Socket closed)
2022-11-18 21:00:24.022  INFO 441007 --- [           main] message.MessageControllerTest            : Started MessageControllerTest in 4.155 seconds (JVM running for 14.327)
2022-11-18 21:00:24.378  INFO 441007 --- [           main] message.MessageControllerTest            : sqlcontianers are working
2022-11-18 21:00:24.390  INFO 441007 --- [           main] message.MessageControllerTest            : InsertNewMessage
2022-11-18 21:00:24.447  INFO 441007 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:49596]```

Upvotes: 2

Views: 3172

Answers (1)

Kevin Wittek
Kevin Wittek

Reputation: 1582

You are manually managing the container lifecycle, which is a good approach:

Startables.deepStart(Stream.of(rabbitMQContainer, sqlContainer)).join();

In this case, remove the @Container and @Testcontainers annotation that interfere with the test lifecycle.

Your tests fails with:

Caused by: com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.

The default admin username and password in RabbitMQContainer should be guest. Changing it accordingly in the application.yml makes the test pass. Once changed it will also work when still using the @Testcontainers extension (although not recommended in this case).

Btw. thanks for sharing the reproducer, it made debugging very easy.

Upvotes: 1

Related Questions