forked from weaveworks/weave
-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
67 lines (60 loc) · 1.57 KB
/
util.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
package testing
import (
"fmt"
"os"
"reflect"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func AssertErrorInterface(t *testing.T, expected interface{}, actual error, desc string) {
require.Error(t, actual, desc)
require.Implements(t, expected, actual, desc)
}
func AssertErrorType(t *testing.T, expected interface{}, actual error, desc string) {
require.Error(t, actual, desc)
// require.IsType doesn't take the pointer element, and doesn't resolve
// through interfaces, so we have to do this one ourselves.
actualT, expectedT := reflect.TypeOf(actual), reflect.TypeOf(expected).Elem()
if actualT != expectedT {
require.FailNow(
t,
fmt.Sprintf("Expected %s but got %s", expectedT.String(), actualT.String()),
desc,
)
}
}
func stackTrace(all bool) string {
buf := make([]byte, 1<<20)
stacklen := runtime.Stack(buf, all)
return string(buf[:stacklen])
}
// Borrowed from net/http tests:
// goTimeout runs f, failing t if f takes more than d to complete.
func RunWithTimeout(t *testing.T, d time.Duration, f func()) {
ch := make(chan bool, 2)
timer := time.AfterFunc(d, func() {
t.Errorf("Timeout expired after %v: stacks:\n%s", d, stackTrace(true))
ch <- true
})
defer timer.Stop()
go func() {
defer func() { ch <- true }()
f()
}()
<-ch
}
// TrimTestArgs finds the first -- in os.Args and trim all args before that
func TrimTestArgs() {
i, l := 0, len(os.Args)
for ; i < l; i++ {
if os.Args[i] == "--" {
break
}
}
if i == l {
panic("Specify weave args after --")
}
os.Args = append(os.Args[:1], os.Args[i+1:l]...)
}