-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_test.go
164 lines (145 loc) · 3.64 KB
/
handle_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
156
157
158
159
160
161
162
163
164
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package unique
import (
"fmt"
"internal/abi"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
"time"
"unsafe"
)
// Set up special types. Because the internal maps are sharded by type,
// this will ensure that we're not overlapping with other tests.
type testString string
type testIntArray [4]int
type testEface any
type testStringArray [3]string
type testStringStruct struct {
a string
}
type testStringStructArrayStruct struct {
s [2]testStringStruct
}
type testStruct struct {
z float64
b string
}
type testZeroSize struct{}
func TestHandle(t *testing.T) {
testHandle(t, testString("foo"))
testHandle(t, testString("bar"))
testHandle(t, testString(""))
testHandle(t, testIntArray{7, 77, 777, 7777})
testHandle(t, testEface(nil))
testHandle(t, testStringArray{"a", "b", "c"})
testHandle(t, testStringStruct{"x"})
testHandle(t, testStringStructArrayStruct{
s: [2]testStringStruct{{"y"}, {"z"}},
})
testHandle(t, testStruct{0.5, "184"})
testHandle(t, testEface("hello"))
testHandle(t, testZeroSize(struct{}{}))
}
func testHandle[T comparable](t *testing.T, value T) {
name := reflect.TypeFor[T]().Name()
t.Run(fmt.Sprintf("%s/%#v", name, value), func(t *testing.T) {
t.Parallel()
v0 := Make(value)
v1 := Make(value)
if v0.Value() != v1.Value() {
t.Error("v0.Value != v1.Value")
}
if v0.Value() != value {
t.Errorf("v0.Value not %#v", value)
}
if v0 != v1 {
t.Error("v0 != v1")
}
drainMaps[T](t)
checkMapsFor(t, value)
})
}
// drainMaps ensures that the internal maps are drained.
func drainMaps[T comparable](t *testing.T) {
t.Helper()
if unsafe.Sizeof(*(new(T))) == 0 {
return // zero-size types are not inserted.
}
wait := make(chan struct{}, 1)
// Set up a one-time notification for the next time the cleanup runs.
// Note: this will only run if there's no other active cleanup, so
// we can be sure that the next time cleanup runs, it'll see the new
// notification.
cleanupMu.Lock()
cleanupNotify = append(cleanupNotify, func() {
select {
case wait <- struct{}{}:
default:
}
})
runtime.GC()
cleanupMu.Unlock()
// Wait until cleanup runs.
<-wait
}
func checkMapsFor[T comparable](t *testing.T, value T) {
// Manually load the value out of the map.
typ := abi.TypeFor[T]()
a, ok := uniqueMaps.Load(typ)
if !ok {
return
}
m := a.(*uniqueMap[T])
wp, ok := m.Load(value)
if !ok {
return
}
if wp.Value() != nil {
t.Errorf("value %v still referenced a handle (or tiny block?) ", value)
return
}
t.Errorf("failed to drain internal maps of %v", value)
}
func TestMakeClonesStrings(t *testing.T) {
s := strings.Clone("abcdefghijklmnopqrstuvwxyz") // N.B. Must be big enough to not be tiny-allocated.
ran := make(chan bool)
runtime.AddCleanup(unsafe.StringData(s), func(ch chan bool) {
ch <- true
}, ran)
h := Make(s)
// Clean up s (hopefully) and run the cleanup.
runtime.GC()
select {
case <-time.After(1 * time.Second):
t.Fatal("string was improperly retained")
case <-ran:
}
runtime.KeepAlive(h)
}
func TestHandleUnsafeString(t *testing.T) {
var testData []string
for i := range 1024 {
testData = append(testData, strconv.Itoa(i))
}
var buf []byte
var handles []Handle[string]
for _, s := range testData {
if len(buf) < len(s) {
buf = make([]byte, len(s)*2)
}
copy(buf, s)
sbuf := unsafe.String(&buf[0], len(s))
handles = append(handles, Make(sbuf))
}
for i, s := range testData {
h := Make(s)
if handles[i].Value() != h.Value() {
t.Fatal("unsafe string improperly retained internally")
}
}
}