...
Source file
src/runtime/abi_test.go
Documentation: runtime
1
2
3
4
5
6
7
8
9
10 package runtime_test
11
12 import (
13 "internal/abi"
14 "internal/runtime/atomic"
15 "internal/testenv"
16 "os"
17 "os/exec"
18 "runtime"
19 "strings"
20 "testing"
21 "time"
22 )
23
24 var regConfirmRun atomic.Int32
25
26
27 func regFinalizerPointer(v *TintPointer) (int, float32, [10]byte) {
28 regConfirmRun.Store(int32(*(*int)(v.p)))
29 return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
30 }
31
32
33 func regFinalizerIface(v Tinter) (int, float32, [10]byte) {
34 regConfirmRun.Store(int32(*(*int)(v.(*TintPointer).p)))
35 return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
36 }
37
38
39
40 type TintPointer struct {
41 p *Tint
42 }
43
44 func (*TintPointer) m() {}
45
46 func TestFinalizerRegisterABI(t *testing.T) {
47 testenv.MustHaveExec(t)
48
49
50
51 if os.Getenv("TEST_FINALIZER_REGABI") != "1" {
52 cmd := testenv.CleanCmdEnv(exec.Command(os.Args[0], "-test.run=^TestFinalizerRegisterABI$", "-test.v"))
53 cmd.Env = append(cmd.Env, "TEST_FINALIZER_REGABI=1")
54 out, err := cmd.CombinedOutput()
55 if !strings.Contains(string(out), "PASS\n") || err != nil {
56 t.Fatalf("%s\n(exit status %v)", string(out), err)
57 }
58 return
59 }
60
61
62
63
64
65
66
67
68 runtime.GC()
69 runtime.GC()
70
71
72
73 success := false
74 for i := 0; i < 100; i++ {
75 if runtime.FinalizerGAsleep() {
76 success = true
77 break
78 }
79 time.Sleep(20 * time.Millisecond)
80 }
81 if !success {
82 t.Fatal("finalizer not asleep?")
83 }
84
85 argRegsBefore := runtime.SetIntArgRegs(abi.IntArgRegs)
86 defer runtime.SetIntArgRegs(argRegsBefore)
87
88 tests := []struct {
89 name string
90 fin any
91 confirmValue int
92 }{
93 {"Pointer", regFinalizerPointer, -1},
94 {"Interface", regFinalizerIface, -2},
95 }
96 for i := range tests {
97 test := &tests[i]
98 t.Run(test.name, func(t *testing.T) {
99 x := &TintPointer{p: new(Tint)}
100 *x.p = (Tint)(test.confirmValue)
101 runtime.SetFinalizer(x, test.fin)
102
103 runtime.KeepAlive(x)
104
105
106 runtime.GC()
107 runtime.GC()
108
109 if !runtime.BlockUntilEmptyFinalizerQueue(int64(time.Second)) {
110 t.Fatal("finalizer failed to execute")
111 }
112 if got := int(regConfirmRun.Load()); got != test.confirmValue {
113 t.Fatalf("wrong finalizer executed? got %d, want %d", got, test.confirmValue)
114 }
115 })
116 }
117 }
118
View as plain text