@@ -58,67 +58,78 @@ kotlin {
5858 }
5959}
6060
61- open class LocalTestServers : DefaultTask () {
62- @Internal
63- var server: Closeable ? = null
64- private set
61+ val osName = System .getProperty(" os.name" )
6562
66- @Internal
67- lateinit var main: String
63+ abstract class TestServerService :
64+ BuildService <TestServerService .Params >,
65+ AutoCloseable {
66+ interface Params : BuildServiceParameters {
67+ val sslConfigPath: Property <String >
68+ val mainClass: Property <String >
69+ val classpath: ConfigurableFileCollection
70+ }
6871
69- @Internal
70- lateinit var classpath: FileCollection
72+ private var server: Closeable ? = null
7173
72- @Input
73- lateinit var sslConfigPath : String
74+ fun startServers () {
75+ if (server != null ) return
7476
75- @TaskAction
76- fun exec () {
7777 try {
7878 println (" [TestServers] start" )
79- val urlClassLoaderSource = classpath.map { file -> file .toURI().toURL() }.toTypedArray()
79+ val urlClassLoaderSource = parameters. classpath.map { it .toURI().toURL() }.toTypedArray()
8080 val loader = URLClassLoader (urlClassLoaderSource, ClassLoader .getSystemClassLoader())
8181
82- val mainClass = loader.loadClass(main )
83- val main = mainClass.getMethod(" startServers" , String ::class .java)
84- server = main .invoke(null , sslConfigPath) as Closeable
82+ val mainClass = loader.loadClass(parameters.mainClass.get() )
83+ val method = mainClass.getMethod(" startServers" , String ::class .java)
84+ server = method .invoke(null , parameters. sslConfigPath.get() ) as Closeable
8585 println (" [TestServers] started" )
8686 } catch (cause: Throwable ) {
8787 println (" [TestServers] failed: ${cause.message} " )
8888 throw cause
8989 }
9090 }
9191
92- fun stop () {
93- if (server != null ) {
94- server?.close()
95- println (" [TestServers] stop" )
96- }
92+ override fun close () {
93+ server?.close()
94+ println (" [TestServers] stopped" )
9795 }
9896}
9997
100- val osName = System .getProperty(" os.name" )
98+ val testServerService = gradle.sharedServices.registerIfAbsent(" testServers" , TestServerService ::class ) {
99+ parameters.sslConfigPath.set(File .createTempFile(" ssl-" , " .cfg" ).absolutePath)
100+ parameters.mainClass.set(" aws.smithy.kotlin.runtime.http.test.util.TestServersKt" )
101+ val kotlinCompilation = kotlin.targets.getByName(" jvm" ).compilations[" test" ]
102+ parameters.classpath.from(kotlinCompilation.runtimeDependencyFiles!! )
103+ }
101104
102- val startTestServers = task<LocalTestServers >(" startTestServers" ) {
103- dependsOn(tasks[" jvmJar" ])
105+ abstract class StartTestServersTask : DefaultTask () {
106+ @get:Internal
107+ abstract val serverService: Property <TestServerService >
104108
105- main = " aws.smithy.kotlin.runtime.http.test.util.TestServersKt"
106- val kotlinCompilation = kotlin.targets.getByName(" jvm" ).compilations[" test" ]
107- classpath = kotlinCompilation.runtimeDependencyFiles!!
108- sslConfigPath = File .createTempFile(" ssl-" , " .cfg" ).absolutePath
109+ @TaskAction
110+ fun start () {
111+ serverService.get().startServers()
112+ }
113+ }
114+
115+ val startTestServers = tasks.register<StartTestServersTask >(" startTestServers" ) {
116+ dependsOn(tasks[" jvmJar" ])
117+ usesService(testServerService)
118+ serverService.set(testServerService)
109119}
110120
111121val testTasks = listOf (" allTests" , " jvmTest" )
112122 .forEach {
113123 tasks.named(it) {
114124 dependsOn(startTestServers)
125+ usesService(testServerService)
115126 }
116127 }
117128
118129tasks.jvmTest {
119130 // set test environment for proxy tests
120131 systemProperty(" MITM_PROXY_SCRIPTS_ROOT" , projectDir.resolve(" proxy-scripts" ).absolutePath)
121- systemProperty(" SSL_CONFIG_PATH" , startTestServers .sslConfigPath)
132+ systemProperty(" SSL_CONFIG_PATH" , testServerService.get().parameters .sslConfigPath)
122133
123134 val enableProxyTestsProp = " aws.test.http.enableProxyTests"
124135 val runningInCodeBuild = System .getenv().containsKey(" CODEBUILD_BUILD_ID" )
@@ -127,7 +138,3 @@ tasks.jvmTest {
127138
128139 systemProperty(enableProxyTestsProp, System .getProperties().getOrDefault(enableProxyTestsProp, shouldRunProxyTests))
129140}
130-
131- gradle.buildFinished {
132- startTestServers.stop()
133- }
0 commit comments