mirror of
https://github.com/golang/go
synced 2024-11-22 16:25:07 -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:
parent
c8fb0ec5a0
commit
bdddfd10ec
@ -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(®ConfirmRunPtr, *(*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(®ConfirmRunIface, *(*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, ®ConfirmRunPtr},
|
||||
{"Interface", regFinalizerIface, -2, ®ConfirmRunIface},
|
||||
{"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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user