-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope_test.go
187 lines (154 loc) · 4.97 KB
/
scope_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
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
package nls_test
import (
"context"
"errors"
"testing"
"time"
"github.com/mmcshane/nls"
)
func nilReaper(context.Context) error { return nil }
func require(t *testing.T, expr bool, msg string, args ...interface{}) {
t.Helper()
if !expr {
t.Fatalf(msg, args...)
}
}
func TestSpawnReap(t *testing.T) {
got := 0
s := nls.NewScope()
err := s.Spawn(context.TODO(), func(context.Context) (nls.Reaper, error) {
got = 1
return func(context.Context) error {
got = 2
return nil
}, nil
})
require(t, err == nil, "unexpected error: %q", err)
require(t, got == 1, "expected Scope.Spawn to have run Spawner")
err = s.Exit(context.TODO())
require(t, err == nil, "unexpected error from Scope.Exit: %q", err)
require(t, got == 2, "expected Scope.Exit to have run Reaper")
}
func TestDoneState(t *testing.T) {
s := nls.NewScope()
s.Exit(context.TODO())
err := s.Spawn(context.TODO(),
func(context.Context) (nls.Reaper, error) { return nil, nil })
require(t, err != nil,
"expected error when spawning from a Scope in the 'done' state")
child := s.NewChildScope()
require(t, child != nil, "NewChildScope should not return nil")
err = child.Spawn(context.TODO(),
func(context.Context) (nls.Reaper, error) { return nil, nil })
require(t, err != nil, "expected error when spawning from the "+
"descendent of a Scope in the 'done' state")
err = child.Exit(context.TODO())
require(t, err == nil,
"exiting a scope that is already done is not an error")
}
func TestExitErrorHandler(t *testing.T) {
want := errors.New(t.Name())
s := nls.NewScope()
err := s.Spawn(context.TODO(), func(context.Context) (nls.Reaper, error) {
return func(context.Context) error { return want }, nil
})
require(t, err == nil, "unexpected error: %q", err)
var got error
err = s.Exit(context.TODO(),
nls.WithErrorHandler(func(err error) { got = err }))
require(t, err == nil, "unexpected error: %q", err)
require(t, want == got,
"expected error handler invocation with %#v (got %#v)", want, got)
}
func TestAsyncErrors(t *testing.T) {
want := errors.New(t.Name())
out := make(chan error)
s := nls.NewScope(nls.WithErrorChan(out))
err := s.Spawn(context.TODO(), func(context.Context) (nls.Reaper, error) {
go func() { s.Err() <- want }()
return nilReaper, nil
})
require(t, err == nil, "unexpected error: %q", err)
got := <-out
require(t, want == got, "expected async error on output chan")
}
func TestSyncSpawnError(t *testing.T) {
want := errors.New(t.Name())
s := nls.NewScope()
got := s.Spawn(context.TODO(), func(context.Context) (nls.Reaper, error) {
return nil, want
})
require(t, want == got, "expected error. want: %q, got: %q", want, got)
}
const (
uninitialized = 0
spawned = 1
reaped = 2
)
type testProcess int
func (tp *testProcess) Spawn(context.Context) (nls.Reaper, error) {
*tp = spawned
return tp.Reap, nil
}
func (tp *testProcess) Reap(context.Context) error {
*tp = reaped
return nil
}
func (tp *testProcess) Is(i int) bool { return int(*tp) == i }
func TestParentChild(t *testing.T) {
// root
// / \
// a d
// / \ \
// b c e
root := nls.NewScope()
a := root.NewChildScope()
b := a.NewChildScope()
c := a.NewChildScope()
d := root.NewChildScope()
e := d.NewChildScope()
rootsvc := new(testProcess)
asvc := new(testProcess)
bsvc := new(testProcess)
csvc := new(testProcess)
dsvc := new(testProcess)
esvc := new(testProcess)
nls.MustSpawn(context.TODO(), root, rootsvc.Spawn)
nls.MustSpawn(context.TODO(), a, asvc.Spawn)
nls.MustSpawn(context.TODO(), b, bsvc.Spawn)
nls.MustSpawn(context.TODO(), c, csvc.Spawn)
nls.MustSpawn(context.TODO(), d, dsvc.Spawn)
nls.MustSpawn(context.TODO(), e, esvc.Spawn)
a.Exit(context.TODO())
// scopes root, d, and e are still active so their service instances should
// still be in the spawned state
require(t, rootsvc.Is(spawned), "expected svc to be running")
require(t, dsvc.Is(spawned), "expected svc to be running")
require(t, esvc.Is(spawned), "expected svc to be running")
// scope a was exited resulting in b and c also exiting. the services
// associated with all three of those scopes should have been reaped.
require(t, asvc.Is(reaped), "expected svc to have been reaped")
require(t, bsvc.Is(reaped), "expected svc to have been reaped")
require(t, csvc.Is(reaped), "expected svc to have been reaped")
}
func TestExitTimeout(t *testing.T) {
s := nls.NewScope()
nls.MustSpawn(context.TODO(), s, func(context.Context) (nls.Reaper, error) {
return func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
}, nil
})
ctx, _ := context.WithDeadline(context.Background(), time.Now())
err := s.Exit(ctx)
require(t, err == context.DeadlineExceeded, "expected context error")
}
func TestMustSpawn(t *testing.T) {
defer func() {
require(t, recover() != nil, "expected MustSpawn to panic on error")
}()
nls.MustSpawn(context.TODO(), nls.NewScope(),
func(context.Context) (nls.Reaper, error) {
return nil, errors.New("testerr")
})
}