-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwihte_test.go
87 lines (78 loc) · 1.83 KB
/
wihte_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
package sync2
import (
"context"
"errors"
"testing"
"time"
)
func testMutexCancel(t *testing.T, reason error) {
g := NewMutexGroup()
m := g.NewMutex()
if err := m.Lock(); err != nil {
t.Fatal(err)
}
go g.cancel(reason)
if err := m.Lock(); err != reason {
t.Fatal(err)
}
// All locking methods should return the same error
if err := m.Lock(); err != reason {
t.Fatal(err)
}
if err := m.Unlock(); err != reason {
t.Fatal(err)
}
if err := g.LockAll(m, g.NewMutex()); err != reason {
t.Fatal(err)
}
}
func TestMutexCancelPkg(t *testing.T) {
testMutexCancel(t, ErrCanceled)
testMutexCancel(t, errors.New("error1"))
}
func TestMutexMultiCancelPkg(t *testing.T) {
g := NewMutexGroup()
err1, err2 := errors.New("error1"), errors.New("error2")
g.cancel(err1) // Set the cause to err1
g.cancel(err2) // Nop
if err := g.NewMutex().Lock(); err != err1 {
t.Fatal(err)
}
}
func TestSleepWithCond(t *testing.T) {
const d = time.Millisecond * 200
start := time.Now()
if err := sleepWithCond(context.Background(), d); err != nil {
t.Fatal(err)
}
diff := time.Since(start) - d
if diff < 0 || diff > d/3 {
t.Fatalf("inaccurate sleep %v", diff)
}
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()
if err := sleepWithCond(ctx, time.Second); err != context.DeadlineExceeded {
t.Fatal(err)
}
}
func BenchmarkSleep(b *testing.B) {
for range b.N {
Sleep(context.Background(), 0)
Sleep(context.Background(), 1)
Sleep(context.Background(), time.Millisecond*10)
}
}
func BenchmarkSleepWithCond(b *testing.B) {
for range b.N {
sleepWithCond(context.Background(), 0)
sleepWithCond(context.Background(), 1)
sleepWithCond(context.Background(), time.Millisecond*10)
}
}
func BenchmarkTimeSleep(b *testing.B) {
for range b.N {
time.Sleep(0)
time.Sleep(1)
time.Sleep(time.Millisecond * 10)
}
}