Skip to content

Commit

Permalink
chore: fix stream gc test
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Oct 17, 2024
1 parent 4e639ca commit 05d1465
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions pkg/streamx/streamx_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,14 @@ func TestStreamingGoroutineLeak(t *testing.T) {
// create server
svr := server.NewServer(server.WithListener(ln), server.WithExitWaitTime(time.Millisecond*10))
var streamStarted int32
waitStreamStarted := func(waitStreams int) {
for atomic.LoadInt32(&streamStarted) < int32(waitStreams) {
waitStreamStarted := func(streamWaited int) {
for {
stated, waited := atomic.LoadInt32(&streamStarted), int32(streamWaited)
if stated >= waited {
return
}
t.Logf("streamStarted=%d < streamWaited=%d", stated, waited)
time.Sleep(time.Millisecond * 10)
t.Logf("streamStarted=%d < waitStreams=%d", atomic.LoadInt32(&streamStarted), waitStreams)
}
}
_ = svr.RegisterService(
Expand Down Expand Up @@ -539,8 +543,7 @@ func TestStreamingGoroutineLeak(t *testing.T) {
wg.Wait()
}

t.Logf("=== Checking Streams GCed ===")
oldNGs := runtime.NumGoroutine()
t.Logf("=== Checking streams GCed ===")
streams := 100
streamList := make([]streamx.ServerStream, streams)
atomic.StoreInt32(&streamStarted, 0)
Expand All @@ -551,14 +554,17 @@ func TestStreamingGoroutineLeak(t *testing.T) {
streamList[i] = bs
}
waitStreamStarted(streams)
ngs := runtime.NumGoroutine()
test.Assert(t, ngs > streams, ngs)
// before GC
ngBeforeGC := runtime.NumGoroutine()
test.Assert(t, ngBeforeGC > streams, ngBeforeGC)
// after GC
for i := 0; i < streams; i++ {
streamList[i] = nil
}
for ngs-oldNGs > 10 {
// the goroutines diff should < 10
for runtime.NumGoroutine()-ngBeforeGC > 10 {
t.Logf("ngCurrent=%d > ngBeforeGC=%d", runtime.NumGoroutine(), ngBeforeGC)
runtime.GC()
ngs = runtime.NumGoroutine()
time.Sleep(time.Millisecond * 50)
}

Expand Down Expand Up @@ -586,10 +592,10 @@ func TestStreamingGoroutineLeak(t *testing.T) {
}()
}
wg.Wait()
ngs = runtime.NumGoroutine()
for ngs-oldNGs > 10 {
// the goroutines diff should < 10
for runtime.NumGoroutine()-ngBeforeGC > 10 {
t.Logf("ngCurrent=%d > ngBeforeGC=%d", runtime.NumGoroutine(), ngBeforeGC)
runtime.GC()
ngs = runtime.NumGoroutine()
time.Sleep(time.Millisecond * 50)
}
})
Expand Down

0 comments on commit 05d1465

Please sign in to comment.