最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

micronaut: have port available for application properties - Stack Overflow

programmeradmin4浏览0评论

Can we somehow inject properties to use the still unknown micronaut server port? The problem is that our tests sometimes fail because the port selected isn't available by the time we start the Embedded Server.

We have a pattern where we use:

@Requires(property = "micronaut.http.services.some-service.url")

for loading a client and micronaut uses the same. However, when testing controller code we use:

val port: Int = SocketUtils.findAvailableTcpPort()
val applicationProperties: Map<String, Any> = 
    mapOf(
        "micronaut.server.port" to port,
        "micronaut.http.services.some-service.url" to "http://localhost:$port"
    )
val embeddedServer = ApplicationContext.run(
    EmbeddedServer::class.java, 
    applicationProperties, 
    "test"
 )

On a busy build server, this often fails (~5% of the time) as the port is in a race condition and not available. I'm aware of the Random Port, but I'm not sure we can use it in the properties.

Can we somehow inject properties to use the still unknown micronaut server port? The problem is that our tests sometimes fail because the port selected isn't available by the time we start the Embedded Server.

We have a pattern where we use:

@Requires(property = "micronaut.http.services.some-service.url")

for loading a client and micronaut uses the same. However, when testing controller code we use:

val port: Int = SocketUtils.findAvailableTcpPort()
val applicationProperties: Map<String, Any> = 
    mapOf(
        "micronaut.server.port" to port,
        "micronaut.http.services.some-service.url" to "http://localhost:$port"
    )
val embeddedServer = ApplicationContext.run(
    EmbeddedServer::class.java, 
    applicationProperties, 
    "test"
 )

On a busy build server, this often fails (~5% of the time) as the port is in a race condition and not available. I'm aware of the Random Port, but I'm not sure we can use it in the properties.

Share Improve this question asked Mar 10 at 16:18 KurtKurt 5551 gold badge6 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Yes, you can. For example in your application-test.properties set a random port for the Micronaut Server. 99% of all execution it will pick a free port without any problem.

Then set the micronaut.server.port to your http client.

micronaut.server.port=${random.port}
micronaut.http.services.some-service.url=http://localhost:${micronaut.server.port}
发布评论

评论列表(0)

  1. 暂无评论