-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprobe_test.go
More file actions
177 lines (163 loc) · 3.74 KB
/
probe_test.go
File metadata and controls
177 lines (163 loc) · 3.74 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package probe
import (
"context"
"errors"
"log/slog"
"math/rand"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type (
// MockRandom is a mock Random stream for testing.
MockRandom struct {
mock.Mock
}
)
var (
logHandler = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})
log = slog.New(logHandler)
)
// Read implementation of io.Reader for MockRandom.
func (m *MockRandom) Read(p []byte) (n int, err error) {
args := m.Called(p)
for _, i := range p {
p[i] = 0
}
if args.Get(1) != nil {
err = args.Error(1)
}
return args.Int(0), err
}
func waitForRunning(p *Probe) {
for i := 0; i < 10; i++ {
if p.Running() {
break
}
time.Sleep(10 * time.Millisecond)
}
}
func waitForIdle(p *Probe) {
for i := 0; i < 10; i++ {
// wait for goroutine to become idle after finishing work
if p.Idle() {
break
}
time.Sleep(10 * time.Millisecond)
}
}
func TestProbe_Working(t *testing.T) {
var test bool
runner := func() {
test = true
}
ctx, cancel := context.WithCancel(context.Background())
p := NewProbe(&ProbeConfig{
Ctx: ctx,
LogHandler: logHandler,
})
waitForRunning(p)
assert.True(t, p.Running(), "NewProbe -> p.Running == true")
p.WorkChan() <- runner
p.Stop(true)
assert.False(t, p.Running(), "p.Stop -> p.Running == false")
assert.True(t, test, "test == true")
p.Run()
cancel()
waitForIdle(p)
assert.False(t, p.Running(), "ctx cancel -> p.Running == false")
}
func TestProbe_Idle(t *testing.T) {
ctx := context.Background()
ctrl := make(chan struct{})
work := make(chan struct{})
runner := func() {
close(work)
<-ctrl
}
p := NewProbe(&ProbeConfig{
Ctx: ctx,
LogHandler: logHandler,
})
waitForIdle(p)
assert.True(t, p.Idle(), "NewProbe -> p.Idle == true")
p.WorkChan() <- runner
<-work
assert.False(t, p.Idle(), "runner -> p.Idle == false")
close(ctrl)
waitForIdle(p)
assert.True(t, p.Idle(), "close(done) -> p.Idle == true")
p.Stop(true)
}
func TestProbe_ID(t *testing.T) {
ctx := context.Background()
p := NewProbe(&ProbeConfig{
Ctx: ctx,
LogHandler: logHandler,
})
assert.NotEmpty(t, p.ID(), "p.ID -> !empty")
}
func TestProbe_Stop(t *testing.T) {
ctx := context.Background()
done := make(chan struct{})
runner := func() {
<-done
}
p := NewProbe(&ProbeConfig{
Ctx: ctx,
LogHandler: logHandler,
})
waitForRunning(p)
assert.True(t, p.Running(), "NewProbe -> p.Running == true")
p.Stop(true)
assert.False(t, p.Running(), "p.Stop(true) -> p.Running == false")
p.Run()
waitForRunning(p)
assert.True(t, p.Running(), "p.Run -> p.Running == true")
p.WorkChan() <- runner
p.Stop(false)
assert.True(t, p.Running(), "p.Stop(false) -> p.Running == true")
close(done)
p.Stop(true)
assert.False(t, p.Running(), "p.Stop(false) -> p.Running == false")
p.Stop(false)
}
func TestProbe_Run(t *testing.T) {
ctx := context.Background()
p := NewProbe(&ProbeConfig{
Ctx: ctx,
LogHandler: logHandler,
})
old := p.done
waitForRunning(p)
p.Run()
assert.Equal(t, old, p.done, "p.Run && p.Running -> old == new")
p.Stop(true)
p.Run()
waitForRunning(p)
assert.NotEqual(t, old, p.done, "p.Run && !p.Running -> old != new")
p.Stop(true)
}
func TestGetID(t *testing.T) {
id := getID(log)
assert.NotEmpty(t, id, "id -> !empty")
m := &MockRandom{}
m.On("Read", mock.Anything).Return(3, nil)
randStream = m
id = getID(log)
assert.Equal(t, "000000", id, "id -> 000000")
m = &MockRandom{}
m.On("Read", mock.Anything).Return(0, errors.New("unexpected error"))
randStream = m
assert.Panics(t, func() {
id = getID(log)
}, "getID -> panic")
randStream = rand.New(rand.NewSource(time.Now().UnixNano()))
}
func BenchmarkGetID(b *testing.B) {
for i := 0; i < b.N; i++ {
getID(log)
}
}