-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointer_stack_test.go
60 lines (53 loc) · 1.12 KB
/
pointer_stack_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
package adhoctx
import (
"sync"
"testing"
"unsafe"
)
func TestSimpleOps(t *testing.T) {
v := NewPointerStackView()
n := int(10)
t.Log("adding a couple pointers, capturing the last one")
v.AllocateID(unsafe.Pointer(&n))
v.AllocateID(unsafe.Pointer(&n))
v.AllocateID(unsafe.Pointer(&n))
p := v.AllocateID(unsafe.Pointer(&n))
t.Log("added, now getting", p)
t.Log(v.String())
nPtr := v.GetPointer(p)
t.Log("got", *(*int)(nPtr), "now removing")
t.Log(v.String())
v.RemoveID(p)
t.Log(v.String())
}
func TestEventualCommits(t *testing.T) {
v := NewPointerStackView()
var wg sync.WaitGroup
for i := int32(0); i < 200; i++ {
wg.Add(1)
n := i
t.Log("starting", i)
go AddEventually(&wg, n, v)
}
t.Log("waiting")
wg.Wait()
t.Log(v.String())
}
func AddEventually(wg *sync.WaitGroup, n int32, v *PointerStackView) {
if wg != nil {
defer wg.Done()
}
p := v.AllocateID(unsafe.Pointer(&n))
v.RemoveID(p)
}
func BenchmarkEventualCommits(b *testing.B) {
v := NewPointerStackView()
n := int32(0)
para := func(pb *testing.PB) {
for pb.Next() {
AddEventually(nil, n, v)
}
}
b.RunParallel(para)
b.Log(v.String())
}