-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexec_test.go
135 lines (110 loc) · 2.63 KB
/
exec_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package piper
import (
"context"
"sync/atomic"
"testing"
"time"
)
type testExec struct {
startCountPtr *uint64
stopCountPtr *uint64
startFn execFnType
stopFn execFnType
}
func newTestExec() *testExec {
var startCount, stopCount uint64
startFn := func(ctx context.Context) {
atomic.AddUint64(&startCount, 1)
time.Sleep(50 * time.Millisecond)
}
stopFn := func(ctx context.Context) {
atomic.AddUint64(&stopCount, 1)
time.Sleep(50 * time.Millisecond)
}
return &testExec{
startCountPtr: &startCount,
stopCountPtr: &stopCount,
startFn: startFn,
stopFn: stopFn,
}
}
func TestExec_NewExec(t *testing.T) {
te := newTestExec()
e := newExec(te.startFn, te.stopFn)
if e == nil {
t.Fatal("newExec returned nil")
}
}
func TestExec_Start(t *testing.T) {
te := newTestExec()
e := newExec(te.startFn, te.stopFn)
e.start(context.TODO())
got := atomic.LoadUint64(te.startCountPtr)
if got != 1 {
t.Fatalf("startCount invalid: wanted: [%d], got [%d]", 1, got)
}
}
func TestExec_StartTwice(t *testing.T) {
te := newTestExec()
e := newExec(te.startFn, te.stopFn)
ctx := context.TODO()
e.start(ctx)
e.start(ctx)
got := atomic.LoadUint64(te.startCountPtr)
if got != 1 {
t.Fatalf("startCount invalid: wanted: [%d], got [%d]", 1, got)
}
}
func TestExec_Stop(t *testing.T) {
te := newTestExec()
e := newExec(te.startFn, te.stopFn)
ctx := context.TODO()
e.stop(ctx)
got := atomic.LoadUint64(te.stopCountPtr)
if got != 1 {
t.Fatalf("stopCount invalid: wanted: [%d], got [%d]", 1, got)
}
}
func TestExec_StopTwice(t *testing.T) {
te := newTestExec()
e := newExec(te.startFn, te.stopFn)
ctx := context.TODO()
e.stop(ctx)
e.stop(ctx)
got := atomic.LoadUint64(te.stopCountPtr)
if got != 1 {
t.Fatalf("stopCount invalid: wanted: [%d], got [%d]", 1, got)
}
}
func TestExec_StartStop(t *testing.T) {
te := newTestExec()
e := newExec(te.startFn, te.stopFn)
ctx := context.TODO()
e.start(ctx)
e.stop(ctx)
got1 := atomic.LoadUint64(te.startCountPtr)
if got1 != 1 {
t.Fatalf("startCount invalid: wanted: [%d], got [%d]", 1, got1)
}
got2 := atomic.LoadUint64(te.stopCountPtr)
if got2 != 1 {
t.Fatalf("stopCount invalid: wanted: [%d], got [%d]", 1, got2)
}
}
func TestExec_StartStopTwice(t *testing.T) {
te := newTestExec()
e := newExec(te.startFn, te.stopFn)
ctx := context.TODO()
e.start(ctx)
e.stop(ctx)
e.start(ctx)
e.stop(ctx)
got1 := atomic.LoadUint64(te.startCountPtr)
if got1 != 2 {
t.Fatalf("startCount invalid: wanted: [%d], got [%d]", 2, got1)
}
got2 := atomic.LoadUint64(te.stopCountPtr)
if got2 != 2 {
t.Fatalf("stopCount invalid: wanted: [%d], got [%d]", 2, got2)
}
}