-
Notifications
You must be signed in to change notification settings - Fork 0
/
occ_test.go
70 lines (63 loc) · 1.45 KB
/
occ_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
61
62
63
64
65
66
67
68
69
70
package occ
import (
"sync"
"testing"
)
func TestSimple(t *testing.T) {
a := 123
c := Wrap(&a)
if *(c.Value()) != a {
t.Fatalf("unexpected value %v, expected %v", *(c.Value()), a)
}
c.Update(func(old *int) *int {
res := *old + 1
return &res
})
if *(c.Value()) != a+1 {
t.Fatalf("unexpected value %v, expected %v", *(c.Value()), a+1)
}
}
func TestUpdateCollision(t *testing.T) {
var childWG sync.WaitGroup
childWG.Add(2)
firstAcquiredPtr := make(chan struct{})
secondAcquiredPtr := make(chan struct{})
firstDone := make(chan struct{})
var firstCalled, secondCalled int
var firstOnce sync.Once
var secondOnce sync.Once
a := 0
c := Wrap(&a)
go func() {
defer childWG.Done()
defer close(firstDone)
c.Update(func(old *int) *int {
firstCalled++
firstOnce.Do(func() { close(firstAcquiredPtr) })
<-secondAcquiredPtr
res := *old + 1
return &res
})
}()
go func() {
defer childWG.Done()
c.Update(func(old *int) *int {
secondCalled++
secondOnce.Do(func() { close(secondAcquiredPtr) })
<-firstAcquiredPtr
<-firstDone
res := *old + 1
return &res
})
}()
childWG.Wait()
if firstCalled != 1 {
t.Fatalf("update txn in first goroutine called %d times, but should be called once", firstCalled)
}
if secondCalled != 2 {
t.Fatalf("update txn in second goroutine called %d times, but should be called twice", secondCalled)
}
if *(c.Value()) != 2 {
t.Fatalf("unexpected value: got %d instead of 2", 2)
}
}