-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.go
226 lines (195 loc) · 4.63 KB
/
tests.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package utils
import (
"context"
"fmt"
"net"
"os"
"sync"
"testing"
"time"
"github.com/Laisky/errors/v2"
glog "github.com/Laisky/go-utils/v5/log"
)
// WaitTCPOpen wait tcp open
func WaitTCPOpen(ctx context.Context, ip string, port int) error {
for {
select {
case <-ctx.Done():
return errors.Wrap(ctx.Err(), "wait tcp open")
default:
}
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: net.ParseIP(ip),
Port: port,
})
if err != nil {
time.Sleep(time.Millisecond * 100)
continue
}
defer LogErr(conn.Close, glog.Shared)
return nil
}
}
// GoroutineTest testing.T support goroutine
type GoroutineTest struct {
mu sync.Mutex
testing.TB
cancel func()
}
// NewGoroutineTest new test for goroutine
//
// any fail will call cancel()
func NewGoroutineTest(tb testing.TB, cancel func()) *GoroutineTest {
tb.Helper()
return &GoroutineTest{
TB: tb,
cancel: cancel,
}
}
// Cleanup add cleanup func
func (t *GoroutineTest) Cleanup(f func()) {
t.mu.Lock()
defer t.mu.Unlock()
t.TB.Cleanup(f)
}
// Error call cancal and exit current goroutine
func (t *GoroutineTest) Error(args ...any) {
t.mu.Lock()
defer t.mu.Unlock()
t.TB.Error(args...)
}
// Errorf call cancal and exit current goroutine
func (t *GoroutineTest) Errorf(format string, args ...any) {
t.mu.Lock()
defer t.mu.Unlock()
t.TB.Errorf(format, args...)
}
// Fail call cancal and exit current goroutine
func (t *GoroutineTest) Fail() {
t.mu.Lock()
defer t.mu.Unlock()
t.cancel()
t.TB.Fail()
}
// FailNow call cancal and exit current goroutine
func (t *GoroutineTest) FailNow() {
t.mu.Lock()
defer t.mu.Unlock()
t.cancel()
t.TB.FailNow()
}
// Failed call cancal and exit current goroutine
func (t *GoroutineTest) Failed() bool {
t.mu.Lock()
defer t.mu.Unlock()
return t.TB.Failed()
}
// Fatal call cancal and exit current goroutine
func (t *GoroutineTest) Fatal(args ...any) {
t.mu.Lock()
defer t.mu.Unlock()
t.cancel()
t.TB.Fatal(args...)
}
// Fatalf call cancal and exit current goroutine
func (t *GoroutineTest) Fatalf(format string, args ...any) {
t.mu.Lock()
defer t.mu.Unlock()
t.cancel()
t.TB.Fatalf(format, args...)
}
// Helper call cancal and exit current goroutine
func (t *GoroutineTest) Helper() {
t.mu.Lock()
defer t.mu.Unlock()
t.TB.Helper()
}
// Log call cancal and exit current goroutine
func (t *GoroutineTest) Log(args ...any) {
t.mu.Lock()
defer t.mu.Unlock()
glog.Shared.Info(fmt.Sprint(args...))
}
// Logf call cancal and exit current goroutine
func (t *GoroutineTest) Logf(format string, args ...any) {
t.mu.Lock()
defer t.mu.Unlock()
glog.Shared.Info(fmt.Sprintf(format, args...))
}
// Name call cancal and exit current goroutine
func (t *GoroutineTest) Name() string {
t.mu.Lock()
defer t.mu.Unlock()
return t.TB.Name()
}
// Setenv call cancal and exit current goroutine
func (t *GoroutineTest) Setenv(key, value string) {
t.mu.Lock()
defer t.mu.Unlock()
t.TB.Setenv(key, value)
}
// Skip call cancal and exit current goroutine
func (t *GoroutineTest) Skip(args ...any) {
t.mu.Lock()
defer t.mu.Unlock()
t.TB.Skip(args...)
}
// SkipNow call cancal and exit current goroutine
func (t *GoroutineTest) SkipNow() {
t.mu.Lock()
defer t.mu.Unlock()
t.TB.SkipNow()
}
// Skipf call cancal and exit current goroutine
func (t *GoroutineTest) Skipf(format string, args ...any) {
t.mu.Lock()
defer t.mu.Unlock()
t.TB.Skipf(format, args...)
}
// Skipped call cancal and exit current goroutine
func (t *GoroutineTest) Skipped() bool {
t.mu.Lock()
defer t.mu.Unlock()
return t.TB.Skipped()
}
// TempDir call cancal and exit current goroutine
func (t *GoroutineTest) TempDir() string {
t.mu.Lock()
defer t.mu.Unlock()
return t.TB.TempDir()
}
// MockStdout mock stdout to a temp file
//
// Example:
//
// func TestMockStdout(t *testing.T) {
// recover, stdout, err := MockStdout()
// require.NoError(t, err)
// defer recover()
// fmt.Println("hello")
// stdout.Seek(0, 0)
// buf, err := io.ReadAll(stdout)
// require.NoError(t, err)
// require.Equal(t, "hello\n", string(buf))
// }
func MockStdout() (recoverFn func(), stdout *os.File, err error) {
// get result from stdout
dir, err := os.MkdirTemp("", "mockStdout")
if err != nil {
return nil, nil, errors.Wrap(err, "create temp dir")
}
fpath, err := JoinFilepath(dir, "mockStdout")
if err != nil {
return nil, nil, errors.Wrap(err, "join filepath")
}
fp, err := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, nil, errors.Wrap(err, "create temp file")
}
old := os.Stdout
os.Stdout = fp
return func() {
defer LogErr(func() error { return os.RemoveAll(dir) }, glog.Shared)
os.Stdout = old
}, fp, nil
}