File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
openai-java-core/src/test/kotlin/com/openai/core/handlers Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -5,12 +5,42 @@ import com.openai.core.http.HttpResponse
5
5
import com.openai.errors.OpenAIIoException
6
6
import java.io.IOException
7
7
import java.io.InputStream
8
+ import kotlin.streams.asSequence
8
9
import kotlin.test.Test
9
10
import org.assertj.core.api.Assertions.assertThat
10
11
import org.junit.jupiter.api.assertThrows
11
12
12
13
internal class StreamHandlerTest {
13
14
15
+ @Test
16
+ fun streamHandler_splitsStreamOnNewlines () {
17
+ val handler = streamHandler { _, lines -> yieldAll(lines) }
18
+ val streamResponse = handler.handle(httpResponse(" a\n bb\n ccc\n dddd" .byteInputStream()))
19
+
20
+ val lines = streamResponse.stream().asSequence().toList()
21
+
22
+ assertThat(lines).containsExactly(" a" , " bb" , " ccc" , " dddd" )
23
+ }
24
+
25
+ @Test
26
+ fun streamHandler_whenClosedEarly_stopsYielding () {
27
+ val handler = streamHandler { _, lines -> yieldAll(lines) }
28
+ val streamResponse = handler.handle(httpResponse(" a\n bb\n ccc\n dddd" .byteInputStream()))
29
+
30
+ val lines =
31
+ streamResponse
32
+ .stream()
33
+ .asSequence()
34
+ .onEach {
35
+ if (it == " bb" ) {
36
+ streamResponse.close()
37
+ }
38
+ }
39
+ .toList()
40
+
41
+ assertThat(lines).containsExactly(" a" , " bb" )
42
+ }
43
+
14
44
@Test
15
45
fun streamHandler_whenReaderThrowsIOException_wrapsException () {
16
46
val handler = streamHandler<String > { _, lines -> lines.forEach {} }
You can’t perform that action at this time.
0 commit comments