@@ -2,6 +2,7 @@ package immortalstreams_test
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"fmt"
6
7
"io"
7
8
"net"
@@ -129,10 +130,10 @@ func TestStream_HandleReconnect(t *testing.T) {
129
130
_ = fromServerWrite1 .Close ()
130
131
131
132
// Wait until the stream is marked disconnected
132
- deadline0 := time . Now (). Add ( 2 * time . Second )
133
- for stream . IsConnected () && time . Now (). Before ( deadline0 ) {
134
- time . Sleep ( 10 * time . Millisecond )
135
- }
133
+ ctx := testutil . Context ( t , testutil . WaitShort )
134
+ testutil . Eventually ( ctx , t , func ( ctx context. Context ) bool {
135
+ return ! stream . IsConnected ( )
136
+ }, testutil . IntervalFast )
136
137
require .False (t , stream .IsConnected ())
137
138
138
139
// Create new client connection (full-duplex)
@@ -297,23 +298,23 @@ func TestStream_ConcurrentAccess(t *testing.T) {
297
298
defer wg .Done ()
298
299
for i := 0 ; i < 100 ; i ++ {
299
300
_ = stream .IsConnected ()
300
- time . Sleep ( time . Microsecond )
301
+ runtime . Gosched () // Yield to other goroutines
301
302
}
302
303
}()
303
304
304
305
go func () {
305
306
defer wg .Done ()
306
307
for i := 0 ; i < 100 ; i ++ {
307
308
_ = stream .ToAPI ()
308
- time . Sleep ( time . Microsecond )
309
+ runtime . Gosched () // Yield to other goroutines
309
310
}
310
311
}()
311
312
312
313
go func () {
313
314
defer wg .Done ()
314
315
for i := 0 ; i < 100 ; i ++ {
315
316
_ = stream .LastDisconnectionAt ()
316
- time . Sleep ( time . Microsecond )
317
+ runtime . Gosched () // Yield to other goroutines
317
318
}
318
319
}()
319
320
@@ -323,7 +324,7 @@ func TestStream_ConcurrentAccess(t *testing.T) {
323
324
// Test other concurrent operations instead
324
325
_ = stream .IsConnected ()
325
326
_ = stream .ToAPI ()
326
- time . Sleep ( time . Microsecond )
327
+ runtime . Gosched () // Yield to other goroutines
327
328
}
328
329
}()
329
330
@@ -403,9 +404,9 @@ func BenchmarkImmortalStream_ReconnectLatency(b *testing.B) {
403
404
require .NoError (b , stream .HandleReconnect (c1 , 0 ))
404
405
// Ensure disconnected before starting benchmark loop
405
406
_ = r1 .Close ()
406
- deadline := time . Now (). Add ( 2 * time . Second )
407
- for stream .IsConnected () && time . Now (). Before ( deadline ) {
408
- time . Sleep ( 5 * time . Millisecond )
407
+ // Use a simple loop for benchmarks to avoid overhead
408
+ for stream .IsConnected () {
409
+ runtime . Gosched ( )
409
410
}
410
411
411
412
b .ReportAllocs ()
@@ -425,10 +426,9 @@ func BenchmarkImmortalStream_ReconnectLatency(b *testing.B) {
425
426
426
427
// Immediately disconnect for next iteration
427
428
_ = remote .Close ()
428
- // Wait until disconnected
429
- deadline := time .Now ().Add (2 * time .Second )
430
- for stream .IsConnected () && time .Now ().Before (deadline ) {
431
- time .Sleep (5 * time .Millisecond )
429
+ // Wait until disconnected - use a simple loop with runtime.Gosched for benchmarks
430
+ for stream .IsConnected () {
431
+ runtime .Gosched ()
432
432
}
433
433
}
434
434
}
@@ -544,10 +544,10 @@ func TestStream_ReconnectionScenarios(t *testing.T) {
544
544
_ = fromServerWriteA .Close ()
545
545
546
546
// Wait until the stream is marked disconnected
547
- deadline := time . Now (). Add ( 2 * time . Second )
548
- for stream2 . IsConnected () && time . Now (). Before ( deadline ) {
549
- time . Sleep ( 10 * time . Millisecond )
550
- }
547
+ ctx := testutil . Context ( t , testutil . WaitShort )
548
+ testutil . Eventually ( ctx , t , func ( ctx context. Context ) bool {
549
+ return ! stream2 . IsConnected ( )
550
+ }, testutil . IntervalFast )
551
551
require .False (t , stream2 .IsConnected ())
552
552
553
553
// Reconnect with new client
@@ -580,7 +580,7 @@ func TestStream_ReconnectionScenarios(t *testing.T) {
580
580
require .NoError (t , err )
581
581
require .True (t , stream2 .IsConnected ())
582
582
583
- // Wait for replay to complete
583
+ // Wait for replay to complete - this ensures the connection is fully established
584
584
<- replayDone
585
585
require .Equal (t , testData , replayBuf , "should receive replayed data" )
586
586
@@ -649,10 +649,10 @@ func TestStream_ReconnectionScenarios(t *testing.T) {
649
649
_ = fromServerWrite .Close ()
650
650
651
651
// Wait until the stream is marked disconnected
652
- deadline := time . Now (). Add ( 2 * time . Second )
653
- for stream3 . IsConnected () && time . Now (). Before ( deadline ) {
654
- time . Sleep ( 10 * time . Millisecond )
655
- }
652
+ ctx := testutil . Context ( t , testutil . WaitShort )
653
+ testutil . Eventually ( ctx , t , func ( ctx context. Context ) bool {
654
+ return ! stream3 . IsConnected ( )
655
+ }, testutil . IntervalFast )
656
656
require .False (t , stream3 .IsConnected ())
657
657
}
658
658
})
@@ -771,10 +771,9 @@ func TestStream_SequenceNumberReconnection_WithSequenceNumbers(t *testing.T) {
771
771
// Simulate disconnection and wait for detection
772
772
_ = clientConn1 .Close ()
773
773
_ = serverConn1 .Close ()
774
- deadline1 := time .Now ().Add (2 * time .Second )
775
- for stream .IsConnected () && time .Now ().Before (deadline1 ) {
776
- time .Sleep (10 * time .Millisecond )
777
- }
774
+ require .Eventually (t , func () bool {
775
+ return ! stream .IsConnected ()
776
+ }, testutil .WaitShort , testutil .IntervalFast )
778
777
require .False (t , stream .IsConnected ())
779
778
780
779
// Client reconnects with its sequence numbers
@@ -872,10 +871,7 @@ func TestStream_SequenceNumberReconnection_WithDataLoss(t *testing.T) {
872
871
require .NoError (t , err )
873
872
require .True (t , stream .IsConnected ())
874
873
875
- // Wait a bit for the connection to be fully established
876
- time .Sleep (100 * time .Millisecond )
877
-
878
- // Send some data
874
+ // Send some data - this will verify the connection is fully established
879
875
testData1 := []byte ("first message" )
880
876
_ , err = serverConn1 .Write (testData1 )
881
877
require .NoError (t , err )
@@ -892,10 +888,9 @@ func TestStream_SequenceNumberReconnection_WithDataLoss(t *testing.T) {
892
888
// Simulate disconnection and wait for detection
893
889
_ = clientConn1 .Close ()
894
890
_ = serverConn1 .Close ()
895
- deadline2 := time .Now ().Add (2 * time .Second )
896
- for stream .IsConnected () && time .Now ().Before (deadline2 ) {
897
- time .Sleep (10 * time .Millisecond )
898
- }
891
+ require .Eventually (t , func () bool {
892
+ return ! stream .IsConnected ()
893
+ }, testutil .WaitShort , testutil .IntervalFast )
899
894
require .False (t , stream .IsConnected ())
900
895
901
896
// Client reconnects with its sequence numbers
0 commit comments