"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}
}
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)
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)
+ }
}
})
}