11package lsp.utils
22
3- import kotlinx.coroutines.delay
4- import kotlinx.coroutines.runBlocking
5- import kotlinx.coroutines.withTimeoutOrNull
63import completions.lsp.client.KotlinLspClient
74import completions.lsp.client.LspConnectionManager
5+ import java.util.concurrent.TimeUnit
6+ import java.util.concurrent.TimeoutException
87import kotlin.time.Duration.Companion.milliseconds
9- import kotlin.time.Duration.Companion.seconds
108
119object LspIntegrationTestUtils {
1210 const val DEFAULT_LSP_HOST = " localhost"
1311 const val DEFAULT_LSP_PORT = 9999
1412
15- fun waitForLspReady (host : String = DEFAULT_LSP_HOST , port : Int = DEFAULT_LSP_PORT , workspace : String ) =
16- runBlocking {
17- var lastError: Throwable ? = null
18- var attempt = 0
19- withTimeoutOrNull(90 .seconds) {
20- while (true ) {
21- try {
22- KotlinLspClient (host, port).use { client ->
23- client.initRequest(workspace, projectName = " probe" ).join()
24- }
25- return @withTimeoutOrNull
26- } catch (t: Throwable ) {
27- lastError = t
28- delay(LspConnectionManager .exponentialBackoffMillis(attempt++ ).milliseconds)
29- }
13+ fun waitForLspReady (host : String = DEFAULT_LSP_HOST , port : Int = DEFAULT_LSP_PORT , workspace : String ) {
14+ var lastError: Throwable ?
15+ var attempt = 0
16+ while (true ) {
17+ try {
18+ KotlinLspClient (host, port).use { client ->
19+ client.initRequest(workspace, projectName = " probe" ).get(90 , TimeUnit .SECONDS )
3020 }
31- } ? : error(" LSP server did not become ready in time: ${lastError?.message} " )
21+ return
22+ } catch (t: Throwable ) {
23+ lastError = t
24+ if (t is TimeoutException ) error(" LSP server never started: $lastError " )
25+ Thread .sleep(LspConnectionManager .exponentialBackoffMillis(attempt++ ).milliseconds.inWholeMilliseconds)
26+ }
3227 }
28+ }
3329}
0 commit comments