-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathruntime_test.go
49 lines (45 loc) · 938 Bytes
/
runtime_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
package utils
import (
"bytes"
"context"
"testing"
"time"
"github.com/fufuok/utils/assert"
)
func TestSafeGo(t *testing.T) {
var (
err interface{}
trace []byte
ctx = context.Background()
)
rcb := func(e interface{}, s []byte) {
err = e
trace = s
}
SafeGo(testFn2, rcb)
time.Sleep(5 * time.Millisecond)
assert.Equal(t, "fn1", err)
assert.Equal(t, true, bytes.Contains(trace, []byte("panic")))
SafeGoWithContext(ctx, testFn3, rcb)
time.Sleep(5 * time.Millisecond)
assert.Equal(t, "fn1", err)
assert.Equal(t, true, bytes.Contains(trace, []byte("panic")))
SafeGoCommonFunc(ctx, testFn4, rcb)
time.Sleep(5 * time.Millisecond)
assert.Equal(t, "fn1", err)
assert.Equal(t, true, bytes.Contains(trace, []byte("panic")))
}
var (
testFn1 = func() {
panic("fn1")
}
testFn2 = func() {
testFn1()
}
testFn3 = func(ctx context.Context) {
testFn1()
}
testFn4 = func(args interface{}) {
testFn1()
}
)