-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
73 lines (59 loc) · 1.38 KB
/
main_test.go
File metadata and controls
73 lines (59 loc) · 1.38 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
package do
import (
"fmt"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
"time"
"go.uber.org/goleak"
)
type ctxTestkey string
const (
ctxTestKey ctxTestkey = "test-key"
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
// https://github.com/stretchr/testify/issues/1101
func testWithTimeout(t *testing.T, timeout time.Duration) {
t.Helper()
testFinished := make(chan struct{})
t.Cleanup(func() {
close(testFinished)
})
line := ""
funcName := ""
var pc [1]uintptr
n := runtime.Callers(2, pc[:])
if n > 0 {
frames := runtime.CallersFrames(pc[:])
frame, _ := frames.Next()
line = frame.File + ":" + strconv.Itoa(frame.Line)
funcName = frame.Function
}
go func() {
select {
case <-testFinished:
case <-time.After(timeout):
if line == "" || funcName == "" {
panic(fmt.Sprintf("Test timed out after: %v", timeout))
}
panic(fmt.Sprintf("%s: Test timed out after: %v\n%s", funcName, timeout, line))
// t.Errorf("Test timed out after: %v", timeout)
// os.Exit(1)
}
}()
}
// pkgName holds the current package name (eg. "do"). It won't break
// if the package name is changed, in case of a fork or anything.
var pkgName = func() string {
t := reflect.TypeOf(__pkg_probe_type{})
s := t.String() // eg. "do.__pkg_probe_type"
if i := strings.IndexByte(s, '.'); i > 0 {
return s[:i]
}
return "do"
}()
type __pkg_probe_type struct{}