-
Notifications
You must be signed in to change notification settings - Fork 0
/
once_test.go
89 lines (82 loc) · 1.7 KB
/
once_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package gonce
import (
"errors"
"fmt"
"math/rand"
"sync"
"testing"
)
func TestOnce(t *testing.T) {
o := Once[int64]{}
res, err := o.Do(func() (result int64, err error) {
return -1, errors.New("foo")
})
if err == nil {
t.Fatalf("expected an error, but got nil")
}
if res != -1 {
t.Fatalf("expected res = -1, but got %d", res)
}
res, err = o.Do(func() (result int64, err error) {
return rand.Int63(), nil
})
if err != nil {
t.Fatalf("expected no error, but got %s", err)
}
if res == -1 {
t.Fatalf("expected non-negative value, but got %d", res)
}
res2, err := o.Do(func() (result int64, err error) {
return rand.Int63(), nil
})
if err != nil {
t.Fatalf("expected no error, but got %s", err)
}
if res != res2 {
t.Fatalf("expected same values, but got res: %d; res2: %d", res, res2)
}
}
var benchmarkSyncOnce int
func BenchmarkSyncOnce(b *testing.B) {
o := sync.Once{}
b.RunParallel(func(pb *testing.PB) {
var res int
for pb.Next() {
o.Do(func() {
res = 42
})
}
benchmarkSyncOnce = res
})
}
var benchmarkOnce int
func BenchmarkOnce(b *testing.B) {
o := Once[int]{}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var res int
for pb.Next() {
res, _ = o.Do(func() (int, error) {
return 42, nil
})
}
benchmarkOnce = res
})
}
func ExampleOnce() {
o := Once[int64]{}
res1, err := o.Do(func() (result int64, err error) {
return rand.Int63(), nil
})
if err != nil {
panic(err)
}
res2, err := o.Do(func() (result int64, err error) {
return rand.Int63(), nil
})
if err != nil {
panic(err)
}
fmt.Printf("res1: (%T); res2: (%T); res1 == res2: %v", res1, res2, res1 == res2)
// Output: res1: (int64); res2: (int64); res1 == res2: true
}