Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ class RemoteIDEServer(private val devSpacesContext: DevSpacesContext) {
} ?: RemoteIDEServerStatus.empty()
}


@Throws(IOException::class)
suspend fun waitServerReady(checkCancelled: (() -> Unit)? = null) {
doWaitServerState(true, readyTimeout, checkCancelled)
suspend fun waitServerReady(checkCancelled: (() -> Unit)? = null, timeout: Long = readyTimeout): Boolean {
return doWaitServerState(true, timeout, checkCancelled)
.also {
if (!it) throw IOException(
"Remote IDE server is not ready after $readyTimeout seconds.",
Expand All @@ -80,7 +79,7 @@ class RemoteIDEServer(private val devSpacesContext: DevSpacesContext) {
}

@Throws(CancellationException::class)
suspend fun isServerState(
private suspend fun isServerState(
isReadyState: Boolean,
checkCancelled: (() -> Unit)? = null
): Boolean {
Expand All @@ -94,8 +93,8 @@ class RemoteIDEServer(private val devSpacesContext: DevSpacesContext) {
}

@Throws(IOException::class)
suspend fun waitServerTerminated(): Boolean {
return doWaitServerState(false, 10L)
suspend fun waitServerTerminated(timeout: Long = 10L): Boolean {
return doWaitServerState(false, timeout)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ data class RemoteIDEServerStatus(
return RemoteIDEServerStatus("", "", "", "", "", emptyArray())
}
}

val isReady: Boolean
get() {
return !joinLink.isNullOrBlank()
&& !projects.isNullOrEmpty() }

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down Expand Up @@ -54,8 +60,5 @@ data class RemoteIDEServerStatus(
result = 31 * result + (projects?.contentHashCode() ?: 0)
return result
}

val isReady: Boolean
get() = !joinLink.isNullOrBlank() && !projects.isNullOrEmpty()
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.io.IOException

class RemoteIDEServerTest {
Expand Down Expand Up @@ -65,29 +66,57 @@ class RemoteIDEServerTest {
}

@Test
fun `#waitServerTerminated should return true if server terminated`() {
fun `#waitServerReady should throw if server status has no join link`() {
// given
val withProjects = RemoteIDEServerStatus(
val withoutJoinLink = remoteIDEServerStatus(
null,
"",
"",
"",
"",
arrayOf(
ProjectInfo("test", "test", "test", "test", "test")
projectInfo("death star")
)
)
val withoutProjects = RemoteIDEServerStatus(
coEvery {
remoteIDEServer.getStatus()
} returns withoutJoinLink

// when, then
assertThrows<IOException> {
runBlocking {
remoteIDEServer.waitServerReady(timeout = 1)
}
}
}

@Test
fun `#waitServerReady should throw if server status has a join link but no projects`() {
// given
val withoutProjects = remoteIDEServerStatus(
"https://starwars.galaxy?peridea",
null
)
coEvery {
remoteIDEServer.getStatus()
} returns withoutProjects

// when, then
assertThrows<IOException> {
runBlocking {
remoteIDEServer.waitServerReady(timeout = 1)
}
}
}

@Test
fun `#waitServerTerminated should return true if server status has no join link`() {
// given
val withoutJoinLink = remoteIDEServerStatus(
null,
"",
"",
"",
"",
emptyArray()
arrayOf(
projectInfo("death star")
)
)
coEvery {
remoteIDEServer.getStatus()
} returns withProjects andThen withoutProjects
} returns withoutJoinLink

// when
val result = runBlocking {
Expand All @@ -98,31 +127,42 @@ class RemoteIDEServerTest {
assertThat(result).isTrue
}

@Test
fun `#waitServerTerminated should return true if server status has a join link but no projects`() {
// given
val withoutProjects = remoteIDEServerStatus(
"https://starwars.galaxy?peridea",
null
)
coEvery {
remoteIDEServer.getStatus()
} returns withoutProjects

// when
val result = runBlocking {
remoteIDEServer.waitServerTerminated(1)
}

// then
assertThat(result).isTrue
}

@Test
fun `#waitServerTerminated should return false on timeout`() {
// given
coEvery {
remoteIDEServer.getStatus()
} returns RemoteIDEServerStatus(
"test", // Should not be 'null' for a running server
"",
"",
"",
"",
} returns remoteIDEServerStatus(
// running server has join link and projects
"https://starwars.galaxy?peridea",
arrayOf(
ProjectInfo(
"test",
"test",
"test",
"test",
"test"
)
projectInfo("death star")
)
)

// when
val result = runBlocking {
remoteIDEServer.waitServerTerminated()
remoteIDEServer.waitServerTerminated(1)
}

// then
Expand All @@ -138,10 +178,33 @@ class RemoteIDEServerTest {

// when
val result = runBlocking {
remoteIDEServer.waitServerTerminated()
remoteIDEServer.waitServerTerminated(1)
}

// then
assertThat(result).isFalse
}

private fun remoteIDEServerStatus(joinLink: String? = null, projects: Array<ProjectInfo>?): RemoteIDEServerStatus {
return RemoteIDEServerStatus(
joinLink,
"",
"",
"",
"",
projects
)
}

private fun projectInfo(name: String): ProjectInfo {
return ProjectInfo(
name,
name,
name,
name,
name
)

}

}
Loading