-
Notifications
You must be signed in to change notification settings - Fork 4
/
bcd_test.go
224 lines (180 loc) · 4.63 KB
/
bcd_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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package bcd
import (
"errors"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
type TestTracer struct {
tracer *exec.Cmd
options []string
sleepDuration int
m sync.Mutex
}
func (t *TestTracer) AddOptions(options []string, v ...string) []string {
if options != nil {
return append(options, v...)
}
t.m.Lock()
defer t.m.Unlock()
t.options = append(t.options, v...)
return nil
}
func (t *TestTracer) AddKV(options []string, key, val string) []string {
return t.AddOptions(options, key+":"+val)
}
func (t *TestTracer) AddThreadFilter(options []string, tid int) []string {
return t.AddOptions(options, "filter:"+strconv.Itoa(tid))
}
func (t *TestTracer) AddFaultedThread(options []string, tid int) []string {
return t.AddOptions(options, "fault:"+strconv.Itoa(tid))
}
func (t *TestTracer) AddClassifier(options []string, classifier string) []string {
return t.AddOptions(options, classifier)
}
func (t *TestTracer) Options() []string {
return t.options
}
func (t *TestTracer) ClearOptions() {
t.options = nil
}
func (t *TestTracer) DefaultTraceOptions() *TraceOptions {
return &TraceOptions{
Faulted: true,
CallerOnly: false,
ErrClassification: true,
Timeout: time.Second * 3}
}
func (t *TestTracer) Finalize(options []string) *exec.Cmd {
t.m.Lock()
defer t.m.Unlock()
t.tracer = exec.Command("/bin/sleep", strconv.Itoa(t.sleepDuration))
return t.tracer
}
func (t *TestTracer) Put(snapshot []byte) error {
return nil
}
func (t *TestTracer) PutOnTrace() bool {
return false
}
func (t *TestTracer) Logf(level LogPriority, format string, v ...interface{}) {
}
func (t *TestTracer) SetLogLevel(level LogPriority) {
}
func (t *TestTracer) String() string {
return "TestTracer"
}
func TestConcurrentRateLimit(t *testing.T) {
tracer := &TestTracer{}
count := new(int64)
var wg sync.WaitGroup
var rateLimit time.Duration = 3
UpdateConfig(GlobalConfig{
PanicOnKillFailure: true,
RateLimit: time.Second * rateLimit})
ng := 4
timeout := time.After(time.Second * 9)
done := make(chan struct{}, ng)
start := time.Now()
for i := 0; i < ng; i++ {
wg.Add(1)
go func() {
for {
select {
case <-done:
wg.Done()
return
default:
}
if Trace(tracer, nil, nil) == nil {
atomic.AddInt64(count, 1)
}
}
}()
}
<-timeout
close(done)
wg.Wait()
end := time.Now()
expected := end.Sub(start) / rateLimit
if *count > int64(expected) {
t.Fatalf("Rate limit exceeded: actual %v, expected %v\n",
*count, expected)
}
}
func TestTimeout(t *testing.T) {
UpdateConfig(GlobalConfig{RateLimit: 0})
tracer := &TestTracer{sleepDuration: 5}
traceErr := Trace(tracer, nil, &TraceOptions{
Timeout: time.Second * 4})
if traceErr == nil {
t.Fatal("Trace timeout failed")
} else if strings.Contains(traceErr.Error(), "execution timed out") == false {
t.Fatalf("Tracer failure not due to timeout (%v)\n", traceErr)
}
// Use the default tracer timeout (3 seconds -- see above).
traceErr = Trace(tracer, nil, nil)
if traceErr == nil {
t.Fatal("Trace timeout failed")
} else if strings.Contains(traceErr.Error(), "execution timed out") == false {
t.Fatal("Tracer failure not due to timeout:", traceErr)
}
// We shouldn't timeout with a negative timeout.
traceErr = Trace(tracer, nil, &TraceOptions{
Timeout: time.Second * -1})
if traceErr != nil {
t.Fatal("Tracer failed:", traceErr)
}
}
func TestTrace(t *testing.T) {
var tracer TestTracer
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := errors.New("Trace error")
traceErr := Trace(&tracer, err, &TraceOptions{
Faulted: true,
CallerOnly: true,
Timeout: time.Second * 30,
ErrClassification: true,
Classifications: []string{"classifier1", "classifier2"}})
if traceErr != nil {
t.Fatal("Failed to trace:", traceErr)
}
if !tracer.tracer.ProcessState.Success() {
t.Fatal("Failed to execute tracer successfully")
}
expectedSet := map[string]bool{
"error:"+err.Error(): false,
"*errors.errorString": false,
"classifier1": false,
"classifier2": false}
if tid, err := gettid(); err == nil {
expectedSet["fault:"+strconv.Itoa(tid)] = false
expectedSet["filter:"+strconv.Itoa(tid)] = false
}
opns := tracer.Options()
for _, s := range opns {
delete(expectedSet, s)
}
if len(expectedSet) != 0 {
t.Fatal("Expected options not set:", expectedSet)
}
}
func pan(t *TestTracer) {
defer Recover(t, false, &TraceOptions{
CallerOnly: false,
Timeout: time.Second * 30})
panic("panic")
}
func TestRecover(t *testing.T) {
var tracer TestTracer
pan(&tracer)
if !tracer.tracer.ProcessState.Success() {
t.Fatal("Failed to recover from panic and trace")
}
}