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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
// Copyright 2020 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 intern
import (
"fmt"
"runtime"
"testing"
)
func TestBasics(t *testing.T) {
clearMap()
foo := Get("foo")
bar := Get("bar")
empty := Get("")
nilEface := Get(nil)
i := Get(0x7777777)
foo2 := Get("foo")
bar2 := Get("bar")
empty2 := Get("")
nilEface2 := Get(nil)
i2 := Get(0x7777777)
foo3 := GetByString("foo")
empty3 := GetByString("")
if foo.Get() != foo2.Get() {
t.Error("foo/foo2 values differ")
}
if foo.Get() != foo3.Get() {
t.Error("foo/foo3 values differ")
}
if foo.Get() != "foo" {
t.Error("foo.Get not foo")
}
if foo != foo2 {
t.Error("foo/foo2 pointers differ")
}
if foo != foo3 {
t.Error("foo/foo3 pointers differ")
}
if bar.Get() != bar2.Get() {
t.Error("bar values differ")
}
if bar.Get() != "bar" {
t.Error("bar.Get not bar")
}
if bar != bar2 {
t.Error("bar pointers differ")
}
if i.Get() != i.Get() {
t.Error("i values differ")
}
if i.Get() != 0x7777777 {
t.Error("i.Get not 0x7777777")
}
if i != i2 {
t.Error("i pointers differ")
}
if empty.Get() != empty2.Get() {
t.Error("empty/empty2 values differ")
}
if empty.Get() != empty.Get() {
t.Error("empty/empty3 values differ")
}
if empty.Get() != "" {
t.Error("empty.Get not empty string")
}
if empty != empty2 {
t.Error("empty/empty2 pointers differ")
}
if empty != empty3 {
t.Error("empty/empty3 pointers differ")
}
if nilEface.Get() != nilEface2.Get() {
t.Error("nilEface values differ")
}
if nilEface.Get() != nil {
t.Error("nilEface.Get not nil")
}
if nilEface != nilEface2 {
t.Error("nilEface pointers differ")
}
if n := mapLen(); n != 5 {
if runtime.Compiler != "gccgo" {
t.Errorf("map len = %d; want 4", n)
}
}
wantEmpty(t)
}
func wantEmpty(t testing.TB) {
if runtime.Compiler == "gccgo" {
// Fails with conservative GC.
return
}
t.Helper()
const gcTries = 5000
for try := 0; try < gcTries; try++ {
runtime.GC()
n := mapLen()
if n == 0 {
break
}
if try == gcTries-1 {
t.Errorf("map len = %d after (%d GC tries); want 0, contents: %v", n, gcTries, mapKeys())
}
}
}
func TestStress(t *testing.T) {
iters := 10000
if testing.Short() {
iters = 1000
}
var sink []byte
for i := 0; i < iters; i++ {
_ = Get("foo")
sink = make([]byte, 1<<20)
}
_ = sink
}
func BenchmarkStress(b *testing.B) {
done := make(chan struct{})
defer close(done)
go func() {
for {
select {
case <-done:
return
default:
}
runtime.GC()
}
}()
clearMap()
v1 := Get("foo")
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
v2 := Get("foo")
if v1 != v2 {
b.Fatal("wrong value")
}
// And also a key we don't retain:
_ = Get("bar")
}
})
runtime.GC()
wantEmpty(b)
}
func mapLen() int {
mu.Lock()
defer mu.Unlock()
return len(valMap)
}
func mapKeys() (keys []string) {
mu.Lock()
defer mu.Unlock()
for k := range valMap {
keys = append(keys, fmt.Sprint(k))
}
return keys
}
func clearMap() {
mu.Lock()
defer mu.Unlock()
for k := range valMap {
delete(valMap, k)
}
}
var (
globalString = "not a constant"
sink string
)
func TestGetByStringAllocs(t *testing.T) {
allocs := int(testing.AllocsPerRun(100, func() {
GetByString(globalString)
}))
if allocs != 0 {
t.Errorf("GetString allocated %d objects, want 0", allocs)
}
}
func BenchmarkGetByString(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
v := GetByString(globalString)
sink = v.Get().(string)
}
}
|