Elffuss BraidHub

kikocis/continue public

Add tests + better coroutine scope granularity and read/write coroutines can be cancelled
Changeset554dc5ae3b37dcaad620fc51206de49eef9358358a1541a9e1c52469e66cb9f3
AuthorJakub Blach <jakubblach@gmail.com> · 2025-07-07 15:47 +02:00
CommitterJakub Blach <jakubblach@gmail.com> · 2025-07-08 14:27 +02:00
Parentsc7054bc87e23

Browse the files at this changeset

2 files changed

modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/process/ContinueProcessHandler.kt +17 −14
@@ -2,41 +2,43 @@
22
33 import com.github.continuedev.continueintellijextension.error.ContinueErrorService
44 import com.intellij.openapi.components.service
5-import kotlinx.coroutines.CoroutineScope
6-import kotlinx.coroutines.Dispatchers
5+import kotlinx.coroutines.*
76 import kotlinx.coroutines.channels.Channel
8-import kotlinx.coroutines.delay
9-import kotlinx.coroutines.launch
107 import java.io.BufferedReader
8+import java.io.IOException
119 import java.io.InputStreamReader
1210 import java.io.OutputStreamWriter
1311
1412 class ContinueProcessHandler(
15- coroutineScope: CoroutineScope,
13+ parentScope: CoroutineScope,
1614 private val process: ContinueProcess,
1715 handleMessage: (String) -> (Unit)
1816 ) {
19- private val pendingWrites = Channel<String>(Channel.Factory.UNLIMITED)
17+ private val innerJob = Job()
18+ private val scope = CoroutineScope(parentScope.coroutineContext + innerJob)
19+ private val pendingWrites = Channel<String>(Channel.UNLIMITED)
2020 private val writer = OutputStreamWriter(process.output)
2121 private val reader = BufferedReader(InputStreamReader(process.input))
2222
2323 init {
24- coroutineScope.launch(Dispatchers.IO) {
25- while (true) {
26- if (reader.ready()) {
24+ scope.launch(Dispatchers.IO) {
25+ try {
26+ while (isActive) {
2727 val line = reader.readLine()
28- if (line.isNotEmpty()) {
28+ if (line != null && line.isNotEmpty()) {
2929 try {
3030 handleMessage(line)
3131 } catch (e: Exception) {
3232 service<ContinueErrorService>().report(e, "Error handling message: $line")
3333 }
34- }
35- } else
36- delay(100)
34+ } else
35+ delay(100)
36+ }
37+ } catch (e: IOException) {
38+ service<ContinueErrorService>().report(e)
3739 }
3840 }
39- coroutineScope.launch(Dispatchers.IO) {
41+ scope.launch(Dispatchers.IO) {
4042 for (message in pendingWrites) {
4143 writer.write(message)
4244 writer.write("\r\n")
@@ -49,6 +51,7 @@
4951 pendingWrites.trySend(message)
5052
5153 fun close() {
54+ innerJob.cancel()
5255 reader.close()
5356 writer.close()
5457 process.close()
added extensions/intellij/src/test/kotlin/com/github/continuedev/continueintellijextension/continue/process/ContinueProcessHandlerTest.kt +49 −0
@@ -0,0 +1,49 @@
1+package com.github.continuedev.continueintellijextension.`continue`.process
2+
3+import kotlinx.coroutines.Dispatchers
4+import kotlinx.coroutines.joinAll
5+import kotlinx.coroutines.launch
6+import kotlinx.coroutines.test.runTest
7+import org.junit.jupiter.api.Assertions.assertTrue
8+import org.junit.jupiter.api.RepeatedTest
9+import java.io.InputStream
10+import java.io.OutputStream
11+
12+class ContinueProcessHandlerTest {
13+
14+ @RepeatedTest(5)
15+ fun `test concurrent writes with queue`() = runTest {
16+ val process = StringContinueProcess()
17+ val handler = ContinueProcessHandler(this, process) {}
18+
19+ (0..100).map { i -> launch(Dispatchers.Default) { handler.write("message number $i") } }
20+ .joinAll()
21+
22+ handler.close()
23+
24+ // assert all message lines are present (order not important)
25+ val lines = process.string.split("\r\n").filter { it.isNotBlank() }
26+ for (i in 0..100)
27+ assertTrue(lines.contains("message number $i"))
28+
29+ // note: this test will fail if the handler writes messages directly to the output stream
30+ // this problem can be easily reproduced by removing the channel<string> from the handler code
31+ }
32+
33+ class StringContinueProcess : ContinueProcess {
34+ val string = StringBuilder()
35+ override val input: InputStream = InputStream.nullInputStream()
36+ override val output: OutputStream = object : OutputStream() {
37+ override fun write(b: Int) {
38+ string.append(b.toChar())
39+ }
40+
41+ override fun write(b: ByteArray, off: Int, len: Int) {
42+ string.append(String(b, off, len))
43+ }
44+ }
45+
46+ override fun close() {
47+ }
48+ }
49+}
No newline at the end of the file