DEV Community

Emmanuel Balpe
Emmanuel Balpe

Posted on

Solving the hanging tests in Micronaut application

We've been facing a strange issue with our Micronaut application tests.
Specifically tests that were producing Kafka messages were successfully run bug sometimes the test engine was kept hanging and never finishing.

Hanging tests

We are running the following versions:

This problem seems related with this issue described here.

After some investigation we've found out that the issue was caused by the fact that sometimes some
KafkaListener in our projects were still active and processing messages even after the test was finished.
This caused the application to keep running and the test engine to wait for the application to finish.

Our solution was to add a @PreDestroy method to a new AppApplicationListener singleton so that the listeners can terminate
their process so that the application shutdown can be successful.

@Singleton
public class AppEventListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppEventListener.class);

    @EventListener
    public void onStartupEvent(StartupEvent event) {
        ...
    }

    @PreDestroy
    void onShutdown() throws InterruptedException {
        LOGGER.info("PreDestroy: Shutting down after a sleep of 1000ms to allow the tests to complete.");
        LOGGER.debug("It looks like the test is stuck if we don't implement this @AfterAll method. " +
                "This is an issue with Micronaut and JUnit 5." +
                "https://github.com/micronaut-projects/micronaut-gcp/issues/638");
        Thread.sleep(1000);
    }

}
Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay