I maintain quite a big micronaut application with thousands of tests. Recently I've migrated to Azure and start seeing these flaky tests with this error
Test initializationError FAILED
io.micronaut.http.server.exceptions.ServerStartupException: Unable to start Micronaut server on port: 43218
application-test.yml
---
micronaut:
server:
port: ${random.port}
example Test
@MicronautTest
class MathServiceSpec extends Specification {
@Inject
MathService mathService
@Unroll
void "should compute #num times 4"() {
when:
def result = mathService.compute(num)
then:
result == expected
where:
num | expected
2 | 8
3 | 12
}
I can't see any obvious reason why micronaut picks a port in use for the random.port, or why this only started happening after migrating to Azure as there is no obvious connection I can make.
Seems micronaut is using a depracated SocketUtils that is problematic https://github.com/micronaut-projects/micronaut-core/blob/5a8a7a7318d0f041f5fdfb667a9da5af1860a8e2/inject/src/main/java/io/micronaut/context/env/PropertySourcePropertyResolver.java#L595
Issue report from Spring https://github.com/spring-projects/spring-framework/issues/28052
I'm confident this is the issue.
To clearly describe my problem: Shortly after the gradle test task finishes, the ports start to get closed, but not immediately. This happens because of the autoCancel feature of my pipeline, as I've made another commit to that PR so my pipeline cancels the previous build. Because gradle deamon is forcibly killed ports stand a chance of being in use and micronaut picks up this ports as free because its using a copy of the Spring SocketUtils that is now deprecated exactly for this bug. Disabling the autoCancel works around this problem but means I need the build to finish to start the next build on the next commit basically wasting my time or build agents.