-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweakinterface_test.go
155 lines (135 loc) · 2.85 KB
/
weakinterface_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package weakref
import (
"fmt"
"runtime"
"sync/atomic"
"testing"
"time"
"unsafe"
)
type (
TestInterface interface {
A() int
}
Struct struct {
v int
}
PointerStruct struct {
v int
}
)
func (s Struct) A() int {
return s.v
}
func (s *PointerStruct) A() int {
return s.v
}
func testNewWeakInterface(i int, t *testing.T) {
//r := makeRef()
var ps TestInterface
s := Struct{123}
ps = &PointerStruct{123}
func() {
defer func() {
if e := recover(); e == nil {
t.Error(`should not accept non-pointer interface`)
}
}()
NewWeakInterface(s)
}()
r := NewWeakInterface(ps)
runtime.GC()
time.Sleep(time.Millisecond * 1)
runtime.GC()
time.Sleep(time.Millisecond * 1)
if !IsAliveI(r) {
t.Error(`early freed`)
}
w := GetInterface(r)
if w.A() != 123 {
t.Fail()
}
time.Sleep(time.Millisecond * 10)
if w.A() != 123 {
t.Fail()
}
time.Sleep(time.Second)
if w.A() != 123 {
t.Fail()
}
runtime.KeepAlive(ps)
_ = ps // keep a in memory til here
runtime.GC()
time.Sleep(time.Millisecond * 1)
runtime.GC()
time.Sleep(time.Millisecond * 1)
if IsAliveI(r) {
if !testingRace { // finalizer is called in a separated GoProc and may not finish yet in race condition
t.Error(`not freed`)
}
} else {
atomic.AddInt64(&interfaceFinalizeCount, 1)
}
if testingRace {
wg.Done()
}
}
func testNewInterfaceFromSlice(i int, t *testing.T) {
a := []TestInterface{&PointerStruct{123}, &PointerStruct{222}, &PointerStruct{333}}
r := NewWeakInterface(a[0])
if !IsAliveI(r) {
t.Error(`early freed`)
}
if GetInterface(r).A() != 123 {
t.Fail()
}
a = append(a, make([]TestInterface, 31)...)
runtime.GC()
time.Sleep(time.Millisecond * 1)
runtime.GC()
time.Sleep(time.Millisecond * 1)
if !IsAliveI(r) {
t.Error(`early free`)
}
if GetInterface(r) != a[0] {
t.Error(`bad reference`)
}
if testingRace {
wg.Done()
}
}
func TestInterfaceOnce(t *testing.T) {
testNewWeakInterface(-1, t)
testNewInterfaceFromSlice(-1, t)
}
func TestInterfaceRace(t *testing.T) {
testingRace = true
testCount := 500000
wg.Add(testCount * 2)
for i := 0; i < testCount; i++ {
ii := i
go testNewWeakInterface(ii, t)
go testNewInterfaceFromSlice(ii, t)
}
wg.Wait()
testingRace = false
fmt.Println(`WeakInterface test times: `, testCount, `, finalizeCount: `, interfaceFinalizeCount)
if interfaceFinalizeCount == 0 {
t.Error(`none finalized`)
}
}
func TestPointerInterface(t *testing.T) {
var i, ip any
a := Struct{123}
i = a
ip = &a
ii := (*GoInterface)(unsafe.Pointer(&i))
iip := (*GoInterface)(unsafe.Pointer(&ip))
if iip.word != uintptr(unsafe.Pointer(&a)) {
t.Error(`incompatible Go version, interface underlying structure changed!`)
}
if iip.word == ii.word {
t.Error(`non-pointer interface should make copy of variable and take the address of the copy`)
// this is not related though, just curious to find out the difference
}
}