Add tests + better coroutine scope granularity and read/write coroutines can be cancelled

This commit is contained in:
Jakub Blach 2025-07-07 15:47:49 +02:00
parent 672f782af2
commit 48ecd0a92a
2 changed files with 66 additions and 14 deletions

View file

@ -2,41 +2,43 @@ package com.github.continuedev.continueintellijextension.`continue`.process
import com.github.continuedev.continueintellijextension.error.ContinueErrorService
import com.intellij.openapi.components.service
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.io.OutputStreamWriter
class ContinueProcessHandler(
coroutineScope: CoroutineScope,
parentScope: CoroutineScope,
private val process: ContinueProcess,
handleMessage: (String) -> (Unit)
) {
private val pendingWrites = Channel<String>(Channel.Factory.UNLIMITED)
private val innerJob = Job()
private val scope = CoroutineScope(parentScope.coroutineContext + innerJob)
private val pendingWrites = Channel<String>(Channel.UNLIMITED)
private val writer = OutputStreamWriter(process.output)
private val reader = BufferedReader(InputStreamReader(process.input))
init {
coroutineScope.launch(Dispatchers.IO) {
while (true) {
if (reader.ready()) {
scope.launch(Dispatchers.IO) {
try {
while (isActive) {
val line = reader.readLine()
if (line.isNotEmpty()) {
if (line != null && line.isNotEmpty()) {
try {
handleMessage(line)
} catch (e: Exception) {
service<ContinueErrorService>().report(e, "Error handling message: $line")
}
}
} else
delay(100)
} else
delay(100)
}
} catch (e: IOException) {
service<ContinueErrorService>().report(e)
}
}
coroutineScope.launch(Dispatchers.IO) {
scope.launch(Dispatchers.IO) {
for (message in pendingWrites) {
writer.write(message)
writer.write("\r\n")
@ -49,6 +51,7 @@ class ContinueProcessHandler(
pendingWrites.trySend(message)
fun close() {
innerJob.cancel()
reader.close()
writer.close()
process.close()

View file

@ -0,0 +1,49 @@
package com.github.continuedev.continueintellijextension.`continue`.process
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.RepeatedTest
import java.io.InputStream
import java.io.OutputStream
class ContinueProcessHandlerTest {
@RepeatedTest(5)
fun `test concurrent writes with queue`() = runTest {
val process = StringContinueProcess()
val handler = ContinueProcessHandler(this, process) {}
(0..100).map { i -> launch(Dispatchers.Default) { handler.write("message number $i") } }
.joinAll()
handler.close()
// assert all message lines are present (order not important)
val lines = process.string.split("\r\n").filter { it.isNotBlank() }
for (i in 0..100)
assertTrue(lines.contains("message number $i"))
// note: this test will fail if the handler writes messages directly to the output stream
// this problem can be easily reproduced by removing the channel<string> from the handler code
}
class StringContinueProcess : ContinueProcess {
val string = StringBuilder()
override val input: InputStream = InputStream.nullInputStream()
override val output: OutputStream = object : OutputStream() {
override fun write(b: Int) {
string.append(b.toChar())
}
override fun write(b: ByteArray, off: Int, len: Int) {
string.append(String(b, off, len))
}
}
override fun close() {
}
}
}