Slow startup on spring integration test. Cause? Ca

2019-07-22 04:32发布

问题:

I'm struggling in a performance problem on startup with my integration tests.

I'm trying to mock the messaging of the system. To do that, I basically use @MockBean on my gateway and use @EnableAutoConfiguration(exclude = {RabbitAutoConfiguration.class}). Example:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
@WebAppConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@ActiveProfiles("test")
@EnableAutoConfiguration(exclude = {RabbitAutoConfiguration.class})
public class MyTestIT {

These two configuration did the job very well, my tests run without problem and without any dependency with some external RabbitMQ.

But the startup time of the spring boot is very, very slow. It's about two minutes only in this part configuring SimpleMessageListenerContainer, AmqpInboundChannelAdapter, EventDrivenConsumer, RabbitExchangeQueueProvisioner, etc.

The log has some tips about what is the problem (I cut a lot of the messages, this is a sample):

2018-02-09 14:26:37.784  INFO [ms-project-name-service,,,] 13804 --- [           main] o.s.integration.channel.DirectChannel    : Channel 'ms-project-name-service:test:-1.channel2-output' has 1 subscriber(s).

2018-02-09 14:26:54.110  INFO [ms-project-name-service,,,] 13804 --- [           main] c.s.b.r.p.RabbitExchangeQueueProvisioner : declaring queue for inbound: channel1-input.anonymous.417FtxKTTce7-_IR0tGuNA, bound to: channel1-input

2018-02-09 14:27:00.147  INFO [ms-project-name-service,,,] 13804 --- [           main] o.s.c.stream.binder.BinderErrorChannel   : Channel 'ms-project-name-service:test:-1.channel1-input.anonymous.417FtxKTTce7-_IR0tGuNA.errors' has 2 subscriber(s).

2018-02-09 14:27:09.186  INFO [ms-project-name-service,,,] 13804 --- [ce7-_IR0tGuNA-1] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@4f0ea6f8: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0

The most strange is this one:

2018-02-09 14:58:42.783  WARN [ms-project-name-service,,,] 208 --- [geGeQP_9Li3Jg-1] o.s.a.r.l.SimpleMessageListenerContainer : Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect

This last one seems that he is still trying to connect on a local RabbitMQ.

There are a lot of these messages on the log. I can't understand why this is still happening even with RabbitMQAutoConfiguration disabled. It's a mystery too how the spring is subscribing in a channel if there is no RabbitMQ to connect.

回答1:

We had similar problem here and it was solved by changing the runner:

@RunWith(SpringRunner.class)

to

@RunWith(SpringJUnit4ClassRunner.class)

They seems like the same on the documentation, but really kickoff our test performance. Let me know if it work, I'm still looking into the documentation for more details.



回答2:

After @Marco response, the problem was back after I removed the follow dependency of my pom.xml (for instance, it is my first dependency on <dependencies/>:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-test-support</artifactId>
    <scope>test</scope>
</dependency>

After return the dependency, the problem was soved. I changed to SpringRunner.class and the tests continued to be fast. I removed the dependency and the tests turned slow again...

I think it's a bug related with the Spring looking for the classpath to do the auto configuration.

By the way, some of the messages on log continues to happen, but they not take so long.

Other thing that help is use this configuration for your tests:

@Configuration
public class RabbitMqConfiguration {

    @Bean
    ConnectionFactory connectionFactory() {
        return new CachingConnectionFactory();
    }

}