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
55import com.openai.errors.OpenAIIoException
66import java.io.IOException
77import java.io.InputStream
8+ import kotlin.streams.asSequence
89import kotlin.test.Test
910import org.assertj.core.api.Assertions.assertThat
1011import org.junit.jupiter.api.assertThrows
1112
1213internal class StreamHandlerTest {
1314
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+
1444 @Test
1545 fun streamHandler_whenReaderThrowsIOException_wrapsException () {
1646 val handler = streamHandler<String > { _, lines -> lines.forEach {} }
You can’t perform that action at this time.
0 commit comments