| 1 |
package runtime |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestTypeCache(t *testing.T) { |
| 9 |
var cache TypeCache[int] |
| 10 |
if cache.Load(0x1000) != nil { |
| 11 |
t.Fatal("an empty cache has a value") |
| 12 |
} |
| 13 |
|
| 14 |
// the addresses of the types are aligned and close to each other, and the table grows many times. |
| 15 |
const num = 5000 |
| 16 |
values := make([]int, num) |
| 17 |
addr := func(i int) uintptr { return 0x100000 + uintptr(i)*32 } |
| 18 |
for i := range values { |
| 19 |
values[i] = i |
| 20 |
if got := cache.Store(addr(i), &values[i]); got != &values[i] { |
| 21 |
t.Fatalf("Store(%d): expected the value which is stored", i) |
| 22 |
} |
| 23 |
// every value stored so far stays, also after the table grew. |
| 24 |
for _, j := range []int{0, i / 2, i} { |
| 25 |
if got := cache.Load(addr(j)); got != &values[j] { |
| 26 |
t.Fatalf("Load(%d) after Store(%d): unexpected value %v", j, i, got) |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
for i := range values { |
| 31 |
if got := cache.Load(addr(i)); got != &values[i] { |
| 32 |
t.Fatalf("Load(%d): unexpected value %v", i, got) |
| 33 |
} |
| 34 |
// an address between the types, and one after them, has no value. |
| 35 |
if cache.Load(addr(i)+8) != nil || cache.Load(addr(num+i)) != nil { |
| 36 |
t.Fatalf("Load near %d: a value for an address which is not stored", i) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// the value which was stored first stays. |
| 41 |
other := -1 |
| 42 |
if got := cache.Store(addr(10), &other); got != &values[10] { |
| 43 |
t.Fatal("Store replaced the value") |
| 44 |
} |
| 45 |
if got := cache.Load(addr(10)); got != &values[10] { |
| 46 |
t.Fatal("Store replaced the value") |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func TestTypeCacheConcurrently(t *testing.T) { |
| 51 |
var cache TypeCache[int] |
| 52 |
const ( |
| 53 |
workers = 16 |
| 54 |
num = 2000 |
| 55 |
) |
| 56 |
addr := func(i int) uintptr { return 0x7f000000 + uintptr(i)*64 } |
| 57 |
results := make([][]*int, workers) |
| 58 |
|
| 59 |
var wg sync.WaitGroup |
| 60 |
for w := 0; w < workers; w++ { |
| 61 |
wg.Add(1) |
| 62 |
go func(w int) { |
| 63 |
defer wg.Done() |
| 64 |
results[w] = make([]*int, num) |
| 65 |
for i := 0; i < num; i++ { |
| 66 |
// every worker stores its own value for every type, while the others load and store. |
| 67 |
v := cache.Load(addr(i)) |
| 68 |
if v == nil { |
| 69 |
own := i |
| 70 |
v = cache.Store(addr(i), &own) |
| 71 |
} |
| 72 |
if *v != i { |
| 73 |
t.Errorf("worker %d: the value of %d is %d", w, i, *v) |
| 74 |
return |
| 75 |
} |
| 76 |
results[w][i] = v |
| 77 |
} |
| 78 |
}(w) |
| 79 |
} |
| 80 |
wg.Wait() |
| 81 |
|
| 82 |
for i := 0; i < num; i++ { |
| 83 |
cached := cache.Load(addr(i)) |
| 84 |
if cached == nil { |
| 85 |
t.Fatalf("no value for %d", i) |
| 86 |
} |
| 87 |
for w := range results { |
| 88 |
if results[w][i] != cached { |
| 89 |
t.Fatalf("worker %d got a value for %d which is not cached", w, i) |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |