-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
162 lines (140 loc) · 3.57 KB
/
Copy pathexample_test.go
File metadata and controls
162 lines (140 loc) · 3.57 KB
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
package nls_test
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/mmcshane/nls"
)
type Service struct {
state string
makeRequestScope nls.Scoper
stop chan struct{}
done chan struct{}
}
func NewService(newScope nls.Scoper) *Service {
return &Service{
state: "stopped",
makeRequestScope: newScope,
stop: make(chan struct{}),
done: make(chan struct{}),
}
}
func (svc *Service) Stop(ctx context.Context) error {
close(svc.stop)
svc.state = "stopped"
select {
case <-svc.done:
case <-ctx.Done():
return ctx.Err()
}
return nil
}
func (svc *Service) State() string {
return svc.state
}
// HandleRequest simulates a long-running request to this service by sleeping
// for the supplied duration.
func (svc *Service) HandleRequest(ctx context.Context, d time.Duration) error {
reqscope := svc.makeRequestScope()
defer reqscope.Exit(context.TODO())
// spawn a watchdog agent but have it run more slowly than the request
// processing so that it never fires (need predictable output for the test)
err := spawnRequestWatchdog(ctx, reqscope, 10*d)
if err != nil {
return err
}
fmt.Printf("pretending to work by sleeping for %s\n", d)
select {
case <-time.After(d):
case <-svc.stop:
}
// we don't have to clean up anything here as the deferred scope Exit
// set up above will clean up the background request watchdog.
return nil
}
func (svc *Service) ListenAndServe(ready chan<- struct{}) error {
svc.state = "running"
close(ready)
<-svc.stop
close(svc.done)
return nil
}
func spawnRequestWatchdog(ctx context.Context, s *nls.Scope, d time.Duration) error {
t := time.NewTicker(time.Duration(d))
stop := make(chan struct{})
done := make(chan struct{})
return s.Spawn(ctx, func(context.Context) (nls.Reaper, error) {
fmt.Println("launching request watchdog")
go func() {
defer close(done)
for {
select {
case <-stop:
fmt.Println("request interrupted, cleaning up watchdog")
return
case <-t.C:
fmt.Println("watchdog check")
}
}
}()
return func(ctx context.Context) error {
close(stop)
select {
case <-done:
case <-ctx.Done():
}
return nil
}, nil
})
}
func Example() {
mainscope := nls.NewScope()
svc := mustSpawnService(context.TODO(), mainscope)
fmt.Printf("svc.State() == %q\n", svc.State())
go func() {
svc.HandleRequest(context.TODO(), 10*time.Second)
}()
mainwait(1*time.Second, mainscope.Err())
exitctx, _ := context.WithTimeout(context.Background(), time.Duration(3*time.Second))
err := mainscope.Exit(exitctx)
if err != nil {
panic(err)
}
fmt.Printf("svc.State() == %q\n", svc.State())
// Output:
// svc.State() == "running"
// launching request watchdog
// pretending to work by sleeping for 10s
// demo exits after 1s
// request interrupted, cleaning up watchdog
// svc.State() == "stopped"
}
func mainwait(d time.Duration, errs <-chan error) {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
select {
case err := <-errs:
fmt.Printf("unhandled error: %q", err)
case sig := <-sigchan:
fmt.Printf("received signal (%v)", sig)
case <-time.After(d):
fmt.Printf("demo exits after %v\n", d)
}
}
func mustSpawnService(ctx context.Context, s *nls.Scope) *Service {
svc := NewService(s.NewChildScope)
ready := make(chan struct{}, 1)
nls.MustSpawn(ctx, s, func(context.Context) (nls.Reaper, error) {
go func() {
if err := svc.ListenAndServe(ready); err != nil {
s.Err() <- err
}
}()
return svc.Stop, nil
})
<-ready
return svc
}