From eaf0e3d4650fd223dec84ee52025c7a82bcb24bd Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Thu, 8 Dec 2022 08:24:57 -0500 Subject: [PATCH] runtime: remove arbitrary timeouts in finalizer tests These short timeouts can overrun due to system scheduling delay (or GC latency) on a slow or heavily-loaded host. Moreover, if the test deadlocks we will probably want to know what the GC goroutines were doing at the time. With an arbitrary timeout, we never get that information; however, if we allow the test to time out completely we will get a goroutine dump (and, if GOTRACEBACK is configured in the environment, that may even include GC goroutines). Fixes #57166. Change-Id: I136501883373c3ce4e250dc8340c60876b375f44 Reviewed-on: https://go-review.googlesource.com/c/go/+/456118 Auto-Submit: Bryan Mills Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Austin Clements --- src/runtime/mfinal_test.go | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/src/runtime/mfinal_test.go b/src/runtime/mfinal_test.go index 902ccc57f8..61d625ac27 100644 --- a/src/runtime/mfinal_test.go +++ b/src/runtime/mfinal_test.go @@ -53,7 +53,7 @@ func TestFinalizerType(t *testing.T) { }}, } - for i, tt := range finalizerTests { + for _, tt := range finalizerTests { done := make(chan bool, 1) go func() { // allocate struct with pointer to avoid hitting tinyalloc. @@ -71,11 +71,7 @@ func TestFinalizerType(t *testing.T) { }() <-done runtime.GC() - select { - case <-ch: - case <-time.After(time.Second * 4): - t.Errorf("#%d: finalizer for type %T didn't run", i, tt.finalizer) - } + <-ch } } @@ -109,11 +105,7 @@ func TestFinalizerInterfaceBig(t *testing.T) { }() <-done runtime.GC() - select { - case <-ch: - case <-time.After(4 * time.Second): - t.Errorf("finalizer for type *bigValue didn't run") - } + <-ch } func fin(v *int) { @@ -188,11 +180,7 @@ func TestEmptySlice(t *testing.T) { fin := make(chan bool, 1) runtime.SetFinalizer(y, func(z *objtype) { fin <- true }) runtime.GC() - select { - case <-fin: - case <-time.After(4 * time.Second): - t.Errorf("finalizer of next object in memory didn't run") - } + <-fin xsglobal = xs // keep empty slice alive until here } @@ -220,11 +208,7 @@ func TestEmptyString(t *testing.T) { // set finalizer on string contents of y runtime.SetFinalizer(y, func(z *objtype) { fin <- true }) runtime.GC() - select { - case <-fin: - case <-time.After(4 * time.Second): - t.Errorf("finalizer of next string in memory didn't run") - } + <-fin ssglobal = ss // keep 0-length string live until here } -- 2.48.1