From a2cfb9a39fa3bca6b829ca7178748da23ecbf417 Mon Sep 17 00:00:00 2001 From: tsosunchia <59512455+tsosunchia@users.noreply.github.com> Date: Fri, 4 Sep 2026 19:33:37 +0800 Subject: [PATCH] =?UTF-8?q?test(geo):=20=E5=8A=A8=E6=80=81=E6=89=A9?= =?UTF-8?q?=E5=AE=B9=20goroutine=20=E5=A0=86=E6=A0=88=E5=BF=AB=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 检测截断并扩容至 16 MiB 后再扫描完整 dump - 同步覆盖 util 与 ipgeo 两条 resolver scope 阻塞测试 --- ipgeo/ipgeo_test.go | 14 ++++++++++++-- util/dns_resolver_test.go | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/ipgeo/ipgeo_test.go b/ipgeo/ipgeo_test.go index 11b6ad8..681aae3 100644 --- a/ipgeo/ipgeo_test.go +++ b/ipgeo/ipgeo_test.go @@ -183,7 +183,7 @@ func TestGetSourceWithGeoDNSEmptyResolverUsesIsolatedScope(t *testing.T) { go observeEmptyGeoSourceScope(GetSourceWithGeoDNS("disable-geoip", ""), sourceStarted, sourceDone) <-sourceStarted - blocked, completed, earlyErr := waitForGeoSourceScopeBlock(sourceDone) + blocked, completed, earlyErr := waitForGeoSourceScopeBlock(t, sourceDone) if completed { close(releaseDot) <-dotDone @@ -212,7 +212,9 @@ func observeEmptyGeoSourceScope(source Source, started chan<- struct{}, result c result <- err } -func waitForGeoSourceScopeBlock(result <-chan error) (blocked bool, completed bool, err error) { +func waitForGeoSourceScopeBlock(t *testing.T, result <-chan error) (blocked bool, completed bool, err error) { + t.Helper() + const maxStackDumpSize = 16 << 20 deadline := time.Now().Add(2 * time.Second) stack := make([]byte, 1<<20) for time.Now().Before(deadline) { @@ -222,6 +224,14 @@ func waitForGeoSourceScopeBlock(result <-chan error) (blocked bool, completed bo default: } n := runtime.Stack(stack, true) + for n == len(stack) { + if len(stack) >= maxStackDumpSize { + t.Errorf("goroutine stack dump exceeded %d bytes", maxStackDumpSize) + return false, false, err + } + stack = make([]byte, min(len(stack)*2, maxStackDumpSize)) + n = runtime.Stack(stack, true) + } for _, goroutine := range bytes.Split(stack[:n], []byte("\n\n")) { if bytes.Contains(goroutine, []byte("observeEmptyGeoSourceScope")) && bytes.Contains(goroutine, []byte("[sync.Mutex.Lock")) { diff --git a/util/dns_resolver_test.go b/util/dns_resolver_test.go index 4712abb..efb33a1 100644 --- a/util/dns_resolver_test.go +++ b/util/dns_resolver_test.go @@ -318,7 +318,7 @@ func TestWithGeoDNSResolver_EmptyScopeIsolatedFromConcurrentDoT(t *testing.T) { go observeEmptyGeoResolverScope(emptyStarted, emptyDone, emptyPolicy) <-emptyStarted - blocked, completed, got := waitForGeoResolverScopeBlock(emptyPolicy) + blocked, completed, got := waitForGeoResolverScopeBlock(t, emptyPolicy) if completed { close(releaseDot) <-dotDone @@ -361,7 +361,9 @@ func observeEmptyGeoResolverScope(started chan<- struct{}, done chan<- struct{}, }) } -func waitForGeoResolverScopeBlock(result <-chan geoResolverScopeObservation) (blocked bool, completed bool, got geoResolverScopeObservation) { +func waitForGeoResolverScopeBlock(t *testing.T, result <-chan geoResolverScopeObservation) (blocked bool, completed bool, got geoResolverScopeObservation) { + t.Helper() + const maxStackDumpSize = 16 << 20 deadline := time.Now().Add(2 * time.Second) stack := make([]byte, 1<<20) for time.Now().Before(deadline) { @@ -371,6 +373,14 @@ func waitForGeoResolverScopeBlock(result <-chan geoResolverScopeObservation) (bl default: } n := runtime.Stack(stack, true) + for n == len(stack) { + if len(stack) >= maxStackDumpSize { + t.Errorf("goroutine stack dump exceeded %d bytes", maxStackDumpSize) + return false, false, got + } + stack = make([]byte, min(len(stack)*2, maxStackDumpSize)) + n = runtime.Stack(stack, true) + } for _, goroutine := range bytes.Split(stack[:n], []byte("\n\n")) { if bytes.Contains(goroutine, []byte("observeEmptyGeoResolverScope")) && bytes.Contains(goroutine, []byte("[sync.Mutex.Lock")) {