Skip to content
Merged
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 @@ -157,9 +157,9 @@ class MockCliServer : AutoCloseable {
/** Reset all request counters. */
fun resetCounts() { counts.clear() }

private val executor = Executors.newCachedThreadPool { r ->
Thread(r, "mock-cli-${Thread.currentThread().id}").apply { isDaemon = true }
}
private val executor = Executors.newThreadPerTaskExecutor(
Thread.ofVirtual().name("mock-cli-", 0).factory(),
)
private val closed = AtomicBoolean(false)

private var server: ServerSocket? = null
Expand Down Expand Up @@ -221,6 +221,7 @@ class MockCliServer : AutoCloseable {
if (!closed.compareAndSet(false, true)) return
shutdownServer()
executor.shutdownNow()
check(executor.awaitTermination(5, TimeUnit.SECONDS)) { "Mock CLI executor did not terminate" }
}

private fun shutdownServer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import ai.kilocode.backend.testing.MockCliServer
import ai.kilocode.backend.testing.TestLog
import ai.kilocode.jetbrains.api.client.DefaultApi
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand All @@ -40,10 +43,13 @@ class KiloBackendWorkspaceTest {

@AfterTest
fun tearDown() {
apps.forEach { it.dispose() }
apps.clear()
scope.cancel()
mock.close()
runBlocking {
apps.forEach { it.dispose() }
apps.clear()
scope.cancel()
mock.close()
withTimeout(10_000) { scope.coroutineContext[Job]?.join() }
}
}

private fun setup(): KiloBackendAppService =
Expand Down Expand Up @@ -458,7 +464,7 @@ class KiloBackendWorkspaceTest {
mock.skills = SKILLS_JSON

val app = setup()
ready(app)
val initial = ready(app)

// Change providers response then fire disposed event
mock.providers = """{
Expand All @@ -474,26 +480,16 @@ class KiloBackendWorkspaceTest {
"connected": ["openai"]
}"""

mock.awaitSseConnection()
mock.pushEvent("global.disposed", """{"type":"global.disposed"}""")

// global.disposed triggers full app reload which restarts the
// workspace manager (stop + start), clearing all cached workspaces.
// Wait for app to reach Ready again after reload.
withTimeout(15_000) {
// App may briefly leave Ready during reload
while (true) {
val state = app.appState.value
if (state is KiloAppState.Ready) {
delay(300)
if (app.appState.value is KiloAppState.Ready) break
}
delay(100)
}
assertTrue(mock.awaitSseConnection())
val reload = async(start = CoroutineStart.UNDISPATCHED) {
app.appState.drop(1).first { it is KiloAppState.Ready }
}
mock.pushEvent("global.disposed", """{"type":"global.disposed"}""")
withTimeout(15_000) { reload.await() }

// Get a fresh workspace — old one was stopped during reload
val ws = app.workspaces.get("/test/project")
assertTrue(ws !== initial)
withTimeout(15_000) {
ws.state.first { it is KiloWorkspaceState.Ready }
}
Expand Down
Loading