...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package testutil
17
18 import (
19 "net/url"
20 "os"
21 "runtime"
22 "testing"
23 "time"
24 )
25
26
27
28 func WaitSchedule() {
29 time.Sleep(10 * time.Millisecond)
30 }
31
32 func MustNewURLs(t *testing.T, urls []string) []url.URL {
33 if urls == nil {
34 return nil
35 }
36 var us []url.URL
37 for _, url := range urls {
38 u := MustNewURL(t, url)
39 us = append(us, *u)
40 }
41 return us
42 }
43
44 func MustNewURL(t *testing.T, s string) *url.URL {
45 u, err := url.Parse(s)
46 if err != nil {
47 t.Fatalf("parse %v error: %v", s, err)
48 }
49 return u
50 }
51
52
53 func FatalStack(t *testing.T, s string) {
54 stackTrace := make([]byte, 1024*1024)
55 n := runtime.Stack(stackTrace, true)
56 t.Errorf("---> Test failed: %s", s)
57 t.Error(string(stackTrace[:n]))
58 t.Fatal(s)
59 }
60
61
62 type ConditionFunc func() (bool, error)
63
64
65
66
67 func Poll(interval time.Duration, timeout time.Duration, condition ConditionFunc) (bool, error) {
68 timeoutCh := time.After(timeout)
69 ticker := time.NewTicker(interval)
70 defer ticker.Stop()
71
72 for {
73 select {
74 case <-timeoutCh:
75 return false, nil
76 case <-ticker.C:
77 success, err := condition()
78 if err != nil {
79 return false, err
80 }
81 if success {
82 return true, nil
83 }
84 }
85 }
86 }
87
88 func SkipTestIfShortMode(t TB, reason string) {
89 if t != nil {
90 t.Helper()
91 if testing.Short() {
92 t.Skip(reason)
93 }
94 }
95 }
96
97
98
99
100
101
102 func ExitInShortMode(reason string) {
103 if os.Getenv("GOLANG_TEST_SHORT") == "true" {
104 os.Exit(0)
105 }
106 }
107
View as plain text