Skip to content

Commit df5cf66

Browse files
CopilotCMGS
andauthored
test(agent): simplify watcher leak polling helper
Agent-Logs-Url: https://github.com/cocoonstack/cocoon-agent/sessions/bc1ceae8-d707-49f6-86fb-d5c0d6b8acf8 Co-authored-by: CMGS <506216+CMGS@users.noreply.github.com>
1 parent 58ecd57 commit df5cf66

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

agent/agent_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io"
88
"net"
99
"runtime"
10-
"runtime/debug"
1110
"strings"
1211
"sync"
1312
"testing"
@@ -291,16 +290,20 @@ func (l *errorAcceptListener) Addr() net.Addr {
291290
return &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0}
292291
}
293292

293+
const (
294+
initialGoroutineDumpSize = 1 << 16
295+
maxGoroutineDumpSize = 1 << 24
296+
)
297+
294298
func goroutineDump() string {
295-
size := 1 << 16
296-
const maxSize = 1 << 24
299+
size := initialGoroutineDumpSize
297300
for {
298301
buf := make([]byte, size)
299302
n := runtime.Stack(buf, true)
300-
if n < len(buf) || size == maxSize {
303+
if n < len(buf) || size == maxGoroutineDumpSize {
301304
return string(buf[:n])
302305
}
303-
size = min(size*2, maxSize)
306+
size = min(size*2, maxGoroutineDumpSize)
304307
}
305308
}
306309

@@ -338,11 +341,10 @@ func TestServerWatcherExitsOnPermanentAcceptError(t *testing.T) {
338341
// stack count returns to baseline.
339342
deadline := time.Now().Add(2 * time.Second)
340343
for time.Now().Before(deadline) {
341-
runtime.GC()
342-
debug.FreeOSMemory()
343344
if countServeWatcherGoroutines() <= before {
344345
return
345346
}
347+
runtime.Gosched()
346348
time.Sleep(10 * time.Millisecond)
347349
}
348350
t.Fatalf("Serve watcher goroutine still present:\n%s", goroutineDump())

0 commit comments

Comments
 (0)