Debian Codegraph @

libgo/go/internal/intern/intern_test.go

raw
1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package intern
6
7 import (
8 "fmt"
9 "runtime"
10 "testing"
11 )
12
13 func TestBasics(t *testing.T) {
14 clearMap()
15 foo := Get("foo")
16 bar := Get("bar")
17 empty := Get("")
18 nilEface := Get(nil)
19 i := Get(0x7777777)
20 foo2 := Get("foo")
21 bar2 := Get("bar")
22 empty2 := Get("")
23 nilEface2 := Get(nil)
24 i2 := Get(0x7777777)
25 foo3 := GetByString("foo")
26 empty3 := GetByString("")
27
28 if foo.Get() != foo2.Get() {
29 t.Error("foo/foo2 values differ")
30 }
31 if foo.Get() != foo3.Get() {
32 t.Error("foo/foo3 values differ")
33 }
34 if foo.Get() != "foo" {
35 t.Error("foo.Get not foo")
36 }
37 if foo != foo2 {
38 t.Error("foo/foo2 pointers differ")
39 }
40 if foo != foo3 {
41 t.Error("foo/foo3 pointers differ")
42 }
43
44 if bar.Get() != bar2.Get() {
45 t.Error("bar values differ")
46 }
47 if bar.Get() != "bar" {
48 t.Error("bar.Get not bar")
49 }
50 if bar != bar2 {
51 t.Error("bar pointers differ")
52 }
53
54 if i.Get() != i.Get() {
55 t.Error("i values differ")
56 }
57 if i.Get() != 0x7777777 {
58 t.Error("i.Get not 0x7777777")
59 }
60 if i != i2 {
61 t.Error("i pointers differ")
62 }
63
64 if empty.Get() != empty2.Get() {
65 t.Error("empty/empty2 values differ")
66 }
67 if empty.Get() != empty.Get() {
68 t.Error("empty/empty3 values differ")
69 }
70 if empty.Get() != "" {
71 t.Error("empty.Get not empty string")
72 }
73 if empty != empty2 {
74 t.Error("empty/empty2 pointers differ")
75 }
76 if empty != empty3 {
77 t.Error("empty/empty3 pointers differ")
78 }
79
80 if nilEface.Get() != nilEface2.Get() {
81 t.Error("nilEface values differ")
82 }
83 if nilEface.Get() != nil {
84 t.Error("nilEface.Get not nil")
85 }
86 if nilEface != nilEface2 {
87 t.Error("nilEface pointers differ")
88 }
89
90 if n := mapLen(); n != 5 {
91 if runtime.Compiler != "gccgo" {
92 t.Errorf("map len = %d; want 4", n)
93 }
94 }
95
96 wantEmpty(t)
97 }
98
99 func wantEmpty(t testing.TB) {
100 if runtime.Compiler == "gccgo" {
101 // Fails with conservative GC.
102 return
103 }
104 t.Helper()
105 const gcTries = 5000
106 for try := 0; try < gcTries; try++ {
107 runtime.GC()
108 n := mapLen()
109 if n == 0 {
110 break
111 }
112 if try == gcTries-1 {
113 t.Errorf("map len = %d after (%d GC tries); want 0, contents: %v", n, gcTries, mapKeys())
114 }
115 }
116 }
117
118 func TestStress(t *testing.T) {
119 iters := 10000
120 if testing.Short() {
121 iters = 1000
122 }
123 var sink []byte
124 for i := 0; i < iters; i++ {
125 _ = Get("foo")
126 sink = make([]byte, 1<<20)
127 }
128 _ = sink
129 }
130
131 func BenchmarkStress(b *testing.B) {
132 done := make(chan struct{})
133 defer close(done)
134 go func() {
135 for {
136 select {
137 case <-done:
138 return
139 default:
140 }
141 runtime.GC()
142 }
143 }()
144
145 clearMap()
146 v1 := Get("foo")
147 b.ReportAllocs()
148 b.RunParallel(func(pb *testing.PB) {
149 for pb.Next() {
150 v2 := Get("foo")
151 if v1 != v2 {
152 b.Fatal("wrong value")
153 }
154 // And also a key we don't retain:
155 _ = Get("bar")
156 }
157 })
158 runtime.GC()
159 wantEmpty(b)
160 }
161
162 func mapLen() int {
163 mu.Lock()
164 defer mu.Unlock()
165 return len(valMap)
166 }
167
168 func mapKeys() (keys []string) {
169 mu.Lock()
170 defer mu.Unlock()
171 for k := range valMap {
172 keys = append(keys, fmt.Sprint(k))
173 }
174 return keys
175 }
176
177 func clearMap() {
178 mu.Lock()
179 defer mu.Unlock()
180 for k := range valMap {
181 delete(valMap, k)
182 }
183 }
184
185 var (
186 globalString = "not a constant"
187 sink string
188 )
189
190 func TestGetByStringAllocs(t *testing.T) {
191 allocs := int(testing.AllocsPerRun(100, func() {
192 GetByString(globalString)
193 }))
194 if allocs != 0 {
195 t.Errorf("GetString allocated %d objects, want 0", allocs)
196 }
197 }
198
199 func BenchmarkGetByString(b *testing.B) {
200 b.ReportAllocs()
201 for i := 0; i < b.N; i++ {
202 v := GetByString(globalString)
203 sink = v.Get().(string)
204 }
205 }