Source file
src/sync/counter_block_test.go
1
2
3
4
5 package sync_test
6
7 import (
8 "runtime"
9 "sync"
10 "testing"
11 )
12
13 func TestAllocCounters(t *testing.T) {
14 t.Run("single request", func(t *testing.T) {
15 ps := runtime.GOMAXPROCS(0)
16
17
18 m := make(map[*uint64]bool)
19 for ptr := range sync.AllocCounters(ps) {
20 if _, ok := m[ptr]; ok {
21 t.Errorf("duplicate ptrs detected %+v", ptr)
22 continue
23 }
24 m[ptr] = true
25 }
26 if len(m) != ps {
27 t.Errorf("want %d got %d", ps, len(m))
28 }
29 })
30
31 t.Run("multiple request", func(t *testing.T) {
32 ps := runtime.GOMAXPROCS(0)
33 var m sync.Map
34 var wg sync.WaitGroup
35
36 singleMemRequest := func() {
37
38 var got int
39 for ptr := range sync.AllocCounters(ps) {
40 got++
41 if _, ok := m.Load(ptr); ok {
42 t.Errorf("duplicate ptrs detected %+v", ptr)
43 continue
44 }
45 m.Store(ptr, true)
46 }
47 if got != ps {
48 t.Errorf("want %d got %d", ps, got)
49 }
50 }
51 for range 1000 {
52 wg.Go(singleMemRequest)
53 }
54 wg.Wait()
55 })
56 }
57
58 func TestAllocCountersGrow(t *testing.T) {
59 testAlloc := func(want int) {
60 var got int
61 for range sync.AllocCounters(want) {
62 got++
63 }
64 if got != want {
65 t.Errorf("want %d got %d", want, got)
66 }
67 }
68 testAlloc(runtime.GOMAXPROCS(0))
69 testAlloc(2 * runtime.GOMAXPROCS(0))
70 }
71
View as plain text