-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_test.go
66 lines (59 loc) · 1.06 KB
/
example_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
package runnergroup_test
import (
"context"
"errors"
"log"
"net/http"
"time"
"github.com/txthinking/runnergroup"
)
func Example() {
g := runnergroup.New()
s1 := &http.Server{
Addr: ":9991",
}
g.Add(&runnergroup.Runner{
Start: func() error {
log.Println("start1")
return s1.ListenAndServe()
},
Stop: func() error {
log.Println("stop1")
return s1.Shutdown(context.Background())
},
})
s2 := &http.Server{
Addr: ":9992",
}
g.Add(&runnergroup.Runner{
Start: func() error {
log.Println("start2")
return errors.New("fail")
// return s2.ListenAndServe()
},
Stop: func() error {
log.Println("stop2")
return s2.Shutdown(context.Background())
},
})
s3 := &http.Server{
Addr: ":9993",
}
g.Add(&runnergroup.Runner{
Start: func() error {
log.Println("start3")
return s3.ListenAndServe()
},
Stop: func() error {
log.Println("stop3")
time.Sleep(5 * time.Second)
return s3.Shutdown(context.Background())
},
})
go func() {
time.Sleep(3 * time.Second)
g.Done()
}()
log.Println(g.Wait())
// Output:
}