When you have some IntegrationTests that required s.th. is started before, you need to create a task that creates another thread.
What is the easiest way to detect when the service is started?
In the sample below I wait ‘startupBlockMillis’ millis. But waiting is fragile and sucks:)
Is it useful to great another thread that runs a request against the above service until a valid response is identified. How do you solve such issues?
Regards, Leif
Here is a sample to detail what I mean:
ext.springBootModuleHttpPort = 8000
ext.springBootModuleStartupBlockMillis = 5000
ext.springBootModuleJarPath = jar.archivePath
task startSpringBootModule(type: SpringBootModuleStarter) {
group = 'spring-boot-module'
description = 'Starting spring boot module in a separate process.'
httpPort = springBootModuleHttpPort
startupBlockMillis = springBootModuleStartupBlockMillis
jarPath = springBootModuleJarPath
}
task stopSpringBootModule {
group = 'spring-boot-module'
description = 'Shutting down spring boot module'
doLast {
"curl -X POST http://localhost:${springBootModuleHttpPort}/shutdown".execute()
}
}
class SpringBootModuleStarter extends DefaultTask {
@Input
Integer httpPort
@Input
Integer startupBlockMillis
@Input
String jarPath
@TaskAction
void start() {
Thread.start {
"java -jar ${jarPath} --server.port=${httpPort}".execute()
}
// this is what I want to get rid off
Thread.sleep(startupBlockMillis)
}
}