forked from didi/sharingan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
69 lines (62 loc) · 1.2 KB
/
main_test.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
package main
import (
"flag"
"os"
"os/signal"
"sync"
"syscall"
"testing"
"time"
_ "github.com/didi/sharingan"
)
const waitFlagParseTime = 10
var endRunning chan struct{}
var callback sync.Once
func stop() {
endRunning <- struct{}{}
}
func signalHandler() {
// 定义并监听 kill信号, On ^C or SIGTERM
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigc
callback.Do(stop)
}()
}
// TestMain Test started when the test binary is started. Only calls main.
func TestMain(m *testing.M) {
if os.Getenv("BAN_SYSTEM_TEST") == "1" {
// Original test flow
os.Exit(m.Run())
return
}
endRunning = make(chan struct{}, 1)
signalHandler()
go func() {
main()
callback.Do(stop)
}()
// Maximum waiting time(10s) for flag.Parse.
// If the flag still missed to execute after 10 seconds, check your logic with main function.
checkTime := time.After(waitFlagParseTime * time.Second)
for {
if flag.Parsed() {
break
}
select {
case <-checkTime:
if !flag.Parsed() {
flag.Parse()
}
break
case <-endRunning:
os.Exit(m.Run())
return
default:
time.Sleep(200 * time.Millisecond)
}
}
<-endRunning
os.Exit(m.Run())
}