@@ -19,7 +19,6 @@ import io.ktor.sse.*
19
19
import io.ktor.utils.io.*
20
20
import io.ktor.utils.io.charsets.*
21
21
import kotlinx.coroutines.*
22
- import kotlinx.coroutines.flow.collect
23
22
import kotlinx.coroutines.flow.collectIndexed
24
23
import kotlinx.coroutines.flow.single
25
24
import kotlinx.coroutines.flow.take
@@ -33,8 +32,6 @@ import kotlin.coroutines.resume
33
32
import kotlin.coroutines.suspendCoroutine
34
33
import kotlin.test.*
35
34
import kotlin.time.Duration.Companion.milliseconds
36
- import kotlin.time.Duration.Companion.seconds
37
- import kotlin.time.measureTime
38
35
39
36
class ServerSentEventsTest : ClientLoader () {
40
37
@@ -138,19 +135,23 @@ class ServerSentEventsTest : ClientLoader() {
138
135
139
136
test { client ->
140
137
coroutineScope {
141
- val job = launch {
142
- val session = client.serverSentEventsSession(" $TEST_SERVER /sse/hello?times=20&interval=100" )
138
+ val started = Job ()
139
+ val session = client.serverSentEventsSession(" $TEST_SERVER /sse/hello?times=20&interval=100" )
140
+ val readJob = launch {
143
141
try {
144
- session.incoming.collect()
142
+ session.incoming.collect {
143
+ started.complete()
144
+ }
145
145
} finally {
146
146
withContext(NonCancellable ) {
147
- delay( 500 )
147
+ started.join( )
148
148
assertFalse(session.isActive)
149
149
}
150
150
}
151
151
}
152
- delay(500 )
153
- job.cancelAndJoin()
152
+ started.join()
153
+ readJob.cancelAndJoin()
154
+ assertFalse(session.isActive)
154
155
}
155
156
}
156
157
}
@@ -582,25 +583,22 @@ class ServerSentEventsTest : ClientLoader() {
582
583
config {
583
584
install(SSE ) {
584
585
maxReconnectionAttempts = 1
585
- reconnectionTime = 2 .seconds
586
+ reconnectionTime = 100 .milliseconds
586
587
}
587
588
}
588
589
589
590
test { client ->
590
591
val events = mutableListOf<ServerSentEvent >()
591
592
592
- val time = measureTime {
593
- client.sse(" $TEST_SERVER /sse/reconnection?count=5" ) {
594
- incoming.take(10 ).collect {
595
- events.add(it)
596
- }
593
+ client.sse(" $TEST_SERVER /sse/reconnection?count=5" ) {
594
+ incoming.take(10 ).collect {
595
+ events.add(it)
597
596
}
598
597
}
599
598
600
599
events.forEachIndexed { index, event ->
601
600
assertEquals(index + 1 , event.id?.toInt())
602
601
}
603
- assertTrue { time > 2 .seconds }
604
602
}
605
603
}
606
604
@@ -617,7 +615,7 @@ class ServerSentEventsTest : ClientLoader() {
617
615
var count = 0
618
616
619
617
assertFailsWith<IllegalStateException > {
620
- client.sse(" $TEST_SERVER /sse/reconnection?count=5" , reconnectionTime = 1 .seconds ) {
618
+ client.sse(" $TEST_SERVER /sse/reconnection?count=5" , reconnectionTime = 10 .milliseconds ) {
621
619
incoming.collect {
622
620
events.add(it)
623
621
count++
@@ -669,7 +667,6 @@ class ServerSentEventsTest : ClientLoader() {
669
667
fun testSeveralReconnections () = clientTests(except(" OkHttp" )) {
670
668
config {
671
669
install(SSE ) {
672
- reconnectionTime = 500 .milliseconds
673
670
maxReconnectionAttempts = 2
674
671
}
675
672
}
@@ -678,14 +675,12 @@ class ServerSentEventsTest : ClientLoader() {
678
675
val events = mutableListOf<ServerSentEvent >()
679
676
var count = 0
680
677
681
- val time = measureTime {
682
- client.sse(" $TEST_SERVER /sse/reconnection?count=5" ) {
683
- incoming.collect {
684
- events.add(it)
685
- count++
686
- if (count == 15 ) {
687
- cancel()
688
- }
678
+ client.sse(" $TEST_SERVER /sse/reconnection?count=5" , reconnectionTime = 10 .milliseconds) {
679
+ incoming.collect {
680
+ events.add(it)
681
+ count++
682
+ if (count == 15 ) {
683
+ cancel()
689
684
}
690
685
}
691
686
}
@@ -694,15 +689,14 @@ class ServerSentEventsTest : ClientLoader() {
694
689
events.forEachIndexed { index, event ->
695
690
assertEquals(index + 1 , event.id?.toInt())
696
691
}
697
- assertTrue { 1 .seconds < time && time < 2 .seconds }
698
692
}
699
693
}
700
694
701
695
@Test
702
696
fun testMaxRetries () = clientTests(except(" OkHttp" )) {
703
697
config {
704
698
install(SSE ) {
705
- reconnectionTime = 500 .milliseconds
699
+ reconnectionTime = 10 .milliseconds
706
700
maxReconnectionAttempts = 4
707
701
}
708
702
}
@@ -711,14 +705,12 @@ class ServerSentEventsTest : ClientLoader() {
711
705
val events = mutableListOf<ServerSentEvent >()
712
706
var count = 0
713
707
714
- val time = measureTime {
715
- client.sse(" $TEST_SERVER /sse/exception-on-reconnection?count=5&count-of-reconnections=4" ) {
716
- incoming.collect {
717
- events.add(it)
718
- count++
719
- if (count == 10 ) {
720
- cancel()
721
- }
708
+ client.sse(" $TEST_SERVER /sse/exception-on-reconnection?count=5&count-of-reconnections=4" ) {
709
+ incoming.collect {
710
+ events.add(it)
711
+ count++
712
+ if (count == 10 ) {
713
+ cancel()
722
714
}
723
715
}
724
716
}
@@ -727,7 +719,6 @@ class ServerSentEventsTest : ClientLoader() {
727
719
events.forEachIndexed { index, event ->
728
720
assertEquals(index % 5 , event.id?.toInt())
729
721
}
730
- assertTrue { 2 .seconds < time && time < 3 .seconds }
731
722
}
732
723
}
733
724
@@ -741,7 +732,6 @@ class ServerSentEventsTest : ClientLoader() {
741
732
}
742
733
743
734
test { client ->
744
-
745
735
client.sse(" $TEST_SERVER /sse/no-content" ) {
746
736
assertEquals(HttpStatusCode .NoContent , call.response.status)
747
737
assertEquals(0 , incoming.toList().size)
@@ -759,4 +749,18 @@ class ServerSentEventsTest : ClientLoader() {
759
749
}
760
750
}
761
751
}
752
+
753
+ @Test
754
+ fun testNoContentStream () = clientTests(except(" OkHttp" )) {
755
+ config {
756
+ install(SSE )
757
+ }
758
+
759
+ test { client ->
760
+ client.sse(" $TEST_SERVER /sse/no-events" ) {
761
+ assertEquals(HttpStatusCode .OK , call.response.status)
762
+ assertEquals(0 , incoming.toList().size)
763
+ }
764
+ }
765
+ }
762
766
}
0 commit comments