micronaut: have port available for application properties - Stack Overflow

Can we somehow inject properties to use the still unknown micronaut server port?The problem is that o

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}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744835218a4596222.html

相关推荐

  • micronaut: have port available for application properties - Stack Overflow

    Can we somehow inject properties to use the still unknown micronaut server port?The problem is that o

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信