1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:24:47 -07:00

runtime: improve synchronization in TestFinalizerRegisterABI

Replace busy-wait with a channel.

Change-Id: I51ddfd5dbde15fff56c62c618570e66930902cbb
Reviewed-on: https://go-review.googlesource.com/c/go/+/310630
Trust: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Austin Clements 2021-04-15 17:23:16 -04:00
parent c8fb0ec5a0
commit bdddfd10ec

View File

@ -17,36 +17,21 @@ import (
"os/exec"
"runtime"
"strings"
"sync"
"testing"
"time"
)
var regConfirmRunIface int
var regConfirmRunPtr int
var regConfirmMU sync.Mutex
func guardedRead(p *int) int {
regConfirmMU.Lock()
defer regConfirmMU.Unlock()
return *p
}
func guardedWrite(p *int, v int) {
regConfirmMU.Lock()
defer regConfirmMU.Unlock()
*p = v
}
var regConfirmRun chan int
//go:registerparams
func regFinalizerPointer(v *Tint) (int, float32, [10]byte) {
guardedWrite(&regConfirmRunPtr, *(*int)(v))
regConfirmRun <- *(*int)(v)
return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
}
//go:registerparams
func regFinalizerIface(v Tinter) (int, float32, [10]byte) {
guardedWrite(&regConfirmRunIface, *(*int)(v.(*Tint)))
regConfirmRun <- *(*int)(v.(*Tint))
return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
}
@ -96,15 +81,14 @@ func TestFinalizerRegisterABI(t *testing.T) {
name string
fin interface{}
confirmValue int
confirmRun *int
}{
{"Pointer", regFinalizerPointer, -1, &regConfirmRunPtr},
{"Interface", regFinalizerIface, -2, &regConfirmRunIface},
{"Pointer", regFinalizerPointer, -1},
{"Interface", regFinalizerIface, -2},
}
for i := range tests {
test := &tests[i]
t.Run(test.name, func(t *testing.T) {
guardedWrite(test.confirmRun, 0)
regConfirmRun = make(chan int)
x := new(Tint)
*x = (Tint)(test.confirmValue)
@ -116,17 +100,13 @@ func TestFinalizerRegisterABI(t *testing.T) {
runtime.GC()
runtime.GC()
for i := 0; i < 100; i++ {
time.Sleep(10 * time.Millisecond)
if guardedRead(test.confirmRun) != 0 {
break
}
}
v := guardedRead(test.confirmRun)
if v == 0 {
select {
case <-time.After(time.Second):
t.Fatal("finalizer failed to execute")
} else if v != test.confirmValue {
t.Fatalf("wrong finalizer executed? regConfirmRun = %d", v)
case gotVal := <-regConfirmRun:
if gotVal != test.confirmValue {
t.Fatalf("wrong finalizer executed? got %d, want %d", gotVal, test.confirmValue)
}
}
})
}