-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
88 lines (74 loc) · 2.25 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package xrun_test
import (
"context"
"net/http"
"os"
"os/signal"
"github.com/gojekfarm/xrun"
"github.com/gojekfarm/xrun/component"
)
func ExampleNewManager() {
m := xrun.NewManager(xrun.ShutdownTimeout(xrun.NoTimeout))
if err := m.Add(component.HTTPServer(component.HTTPServerOptions{Server: &http.Server{}})); err != nil {
panic(err)
}
if err := m.Add(xrun.ComponentFunc(func(ctx context.Context) error {
// Start something here in a blocking way and continue on ctx.Done
<-ctx.Done()
// Call Stop on component if cleanup is required
return nil
})); err != nil {
panic(err)
}
// ctx is marked done (its Done channel is closed) when one of the listed signals arrives
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer stop()
if err := m.Run(ctx); err != nil {
os.Exit(1)
}
}
func ExampleNewManager_nested() {
m1 := xrun.NewManager()
if err := m1.Add(component.HTTPServer(component.HTTPServerOptions{Server: &http.Server{}})); err != nil {
panic(err)
}
m2 := xrun.NewManager()
if err := m2.Add(xrun.ComponentFunc(func(ctx context.Context) error {
// Start something here in a blocking way and continue on ctx.Done
<-ctx.Done()
// Call Stop on component if cleanup is required
return nil
})); err != nil {
panic(err)
}
gm := xrun.NewManager()
if err := gm.Add(m1); err != nil {
panic(err)
}
if err := gm.Add(m2); err != nil {
panic(err)
}
// ctx is marked done (its Done channel is closed) when one of the listed signals arrives
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer stop()
// Run will start m1 and m2 simultaneously
if err := gm.Run(ctx); err != nil {
os.Exit(1)
}
}
func ExampleAll() {
// ctx is marked done (its Done channel is closed) when one of the listed signals arrives
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer stop()
if err := xrun.All(xrun.NoTimeout,
component.HTTPServer(component.HTTPServerOptions{Server: &http.Server{}}),
xrun.ComponentFunc(func(ctx context.Context) error {
// Start something here in a blocking way and continue on ctx.Done
<-ctx.Done()
// Call Stop on component if cleanup is required
return nil
}),
).Run(ctx); err != nil {
os.Exit(1)
}
}