-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtesting.go
More file actions
65 lines (58 loc) · 1.7 KB
/
testing.go
File metadata and controls
65 lines (58 loc) · 1.7 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
// Copyright (c) [2019] [dabank.io]
// [devtools4chains] is licensed under the Mulan PSL v1.
// You can use this software according to the terms and conditions of the Mulan PSL v1.
// You may obtain a copy of Mulan PSL v1 at:
// http://license.coscl.org.cn/MulanPSL
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v1 for more details.
package devtools4chains
import (
"context"
"reflect"
"runtime/debug"
"strings"
"testing"
"time"
)
// WaitSomething 等待fn() return nil
func WaitSomething(t *testing.T, timeout time.Duration, fn func() error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for {
select {
case <-ctx.Done():
t.Fatalf("wait something timeout, %s", timeout)
default:
if e := fn(); e == nil {
return
}
}
time.Sleep(500 * time.Millisecond)
}
}
func tShouldNil(t *testing.T, v interface{}, args ...interface{}) {
if v != nil {
debug.PrintStack()
t.Fatalf("[test assert] should nil, but got: %v, %v", v, args)
}
}
func tShouldTrue(t *testing.T, b bool, args ...interface{}) {
if !b {
debug.PrintStack()
t.Fatalf("[test assert] should true, args: %v", args)
}
}
func tShouldNotZero(t *testing.T, v interface{}, args ...interface{}) {
if reflect.ValueOf(v).IsZero() {
debug.PrintStack()
t.Fatalf("[test assert] should not [zero value], %v", args)
}
}
func tShouldNotContains(t *testing.T, v, containV string) {
if strings.Contains(v, containV) {
debug.PrintStack()
t.Fatalf("[test assert] [%s] should not contains [%s]", v, containV)
}
}