-
Notifications
You must be signed in to change notification settings - Fork 0
/
plumber_test.go
342 lines (315 loc) · 8.17 KB
/
plumber_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package plumber_test
import (
"context"
"errors"
"fmt"
"net/http"
"sync"
"testing"
"time"
"github.com/getoutreach/plumber"
"gotest.tools/v3/assert"
)
type Config struct {
helloMessage plumber.D[string]
}
func (c *Config) HelloMessage() *plumber.D[string] {
return c.helloMessage.Const("Hello message")
}
type HTTP struct {
Server plumber.R[*http.Server]
HelloHandler plumber.D[http.HandlerFunc]
EchoHandler plumber.D[http.HandlerFunc]
}
type App struct {
Config *Config
HTTP *HTTP
D1 plumber.D[int]
D2 plumber.D[string]
d3 plumber.D[int]
D4 plumber.R[int]
}
func (a *App) D3() *plumber.D[int] {
return a.d3.Resolve(func(r *plumber.Resolution[int]) {
r.Require(&a.D2, a.D3()).Then(func() {
r.Resolve(0)
})
})
}
func TestDefineOnce(t *testing.T) {
type dep struct {
D1 int
D2 int
}
a := struct {
D1 plumber.D[int]
D2 plumber.D[int]
Resolved plumber.D[*dep]
}{}
a.D1.Const(1)
a.D1.Const(2)
a.D2.Define(func() int { return 3 })
a.D2.Define(func() int { return 4 })
assert.Equal(t, a.D1.Must(), 1)
assert.Equal(t, a.D2.Must(), 3)
}
func TestRequireOk(t *testing.T) {
type dep struct {
D1 int
D2 int
}
a := struct {
D1 plumber.D[int]
D2 plumber.D[int]
Service plumber.D[*dep]
}{}
a.D1.Const(1)
a.D2.Const(2)
a.Service.Resolve(func(r *plumber.Resolution[*dep]) {
r.Require(&a.D1, &a.D2).Then(func() {
r.Resolve(&dep{
D1: a.D1.Instance(),
D2: a.D2.Instance(),
})
})
})
v, err := a.Service.InstanceError()
assert.NilError(t, err)
assert.Equal(t, v.D1, 1)
assert.Equal(t, v.D2, 2)
}
func TestRequireNotOk(t *testing.T) {
type notresolved struct{}
type middle struct{}
a := struct {
D1 plumber.D[int]
D2 plumber.D[int]
NotResolved plumber.D[*notresolved]
Middle plumber.D[middle]
}{}
a.D1.Const(1)
a.Middle.Resolve(func(r *plumber.Resolution[middle]) {
r.Require(&a.NotResolved).Then(func() {
r.Resolve(middle{})
})
})
a.D2.Resolve(func(r *plumber.Resolution[int]) {
r.Require(&a.D1, &a.Middle).Then(func() {
r.Resolve(1)
})
})
v, err := a.D2.InstanceError()
assert.Equal(t, v, 0)
//nolint: lll //Why: error is long
assert.Error(t, err, `dependency not resolved, int requires plumber_test.middle (dependency not resolved, plumber_test.middle requires *plumber_test.notresolved (instance *plumber_test.notresolved not resolved))`)
}
func TestRequireNotOkError(t *testing.T) {
type notresolved struct{}
type middle struct{}
a := struct {
D2 plumber.D[int]
WithError plumber.D[*notresolved]
Middle plumber.D[middle]
}{}
a.WithError.Resolve(func(r *plumber.Resolution[*notresolved]) {
r.Require().Then(func() {
r.ResolveError(nil, errors.New("Error"))
})
})
a.Middle.Resolve(func(r *plumber.Resolution[middle]) {
r.Require(&a.WithError).Then(func() {
r.Resolve(middle{})
})
})
a.D2.Resolve(func(r *plumber.Resolution[int]) {
r.Require(&a.Middle).Then(func() {
r.Resolve(1)
})
})
v, err := a.D2.InstanceError()
assert.Equal(t, v, 0)
//nolint: lll //Why: error is long
assert.Error(t, err, `dependency not resolved, int requires plumber_test.middle (dependency not resolved, plumber_test.middle requires *plumber_test.notresolved (Error))`)
}
func TestRequireNotOkCycle(t *testing.T) {
a := struct {
D1 plumber.D[int]
D2 plumber.D[int]
}{}
a.D1.Const(1)
a.D2.Resolve(func(r *plumber.Resolution[int]) {
r.Require(&a.D1, &a.D2).Then(func() {
r.Resolve(1)
})
})
v, err := a.D2.InstanceError()
assert.Equal(t, v, 0)
assert.Error(t, err, "dependency not resolved, int requires int (circular dependency)")
}
func TestRequireConcurrentOk(t *testing.T) {
type concurrent struct{}
a := struct {
D1 plumber.D[int]
D2 plumber.D[int]
Concurrent plumber.D[concurrent]
}{}
a.D1.Const(1)
a.D2.Resolve(func(r *plumber.Resolution[int]) {
r.Require(&a.D1, &a.Concurrent).Then(func() {
r.Resolve(1)
})
})
a.Concurrent.Define(func() concurrent {
time.Sleep(100 * time.Millisecond)
return concurrent{}
})
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
v, err := a.D2.InstanceError()
assert.NilError(t, err)
assert.Equal(t, v, 1)
}()
go func() {
defer wg.Done()
_, err := a.Concurrent.InstanceError()
assert.NilError(t, err)
}()
wg.Wait()
}
func TestExamplePipeline(t *testing.T) {
a := &App{
Config: &Config{},
HTTP: &HTTP{},
}
a.Config.helloMessage.Const("Message")
fitHTTP(a)
a.D1.Const(1)
a.D2.Const("a")
a.D2.DefineError(func() (string, error) {
return "tesait", nil
})
a.D2.Define(func() string {
return "tesait"
})
a.D4.Resolve(func(r *plumber.ResolutionR[int]) {
r.Require(&a.D2).Then(func() {
r.ResolveAdapter(0, plumber.Closer(func(context.Context) error {
return nil
}))
})
})
fmt.Println("test", a.D1.Must(), a.D2.Must())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := plumber.Start(ctx,
// Serial pipeline. Task are started sequentially and closed in reverse order.
plumber.Pipeline(
plumber.Closer(func(ctx context.Context) error {
fmt.Println("pipeline is closing")
return nil
}),
plumber.GracefulRunner(func(ctx context.Context) error {
fmt.Println("Task 1 starting")
<-ctx.Done()
return nil
}, func(ctx context.Context) error {
fmt.Println("Task 1 closing")
return nil
}),
// The parallel pipeline all task are stared and closed in parallel.
plumber.Parallel(
plumber.NewRunner(func(ctx context.Context) error {
fmt.Println("Task 2 starting")
<-ctx.Done()
return nil
}),
plumber.NewRunner(func(ctx context.Context) error {
fmt.Println("Task 3 starting")
<-ctx.Done()
return nil
}),
plumber.Looper(func(ctx context.Context, l *plumber.Loop) error {
l.Ready()
tick := time.Tick(500 * time.Millisecond)
for {
select {
case <-tick:
// Work
fmt.Println("Work")
case closeDone := <-l.Closing():
fmt.Println("Close is requested")
closeDone.Success()
// Graceful shutdown
return nil
case <-ctx.Done():
// Cancel / Timeout
return ctx.Err()
}
}
}),
),
// Dependency graph based runner
&a.D4,
&a.HTTP.Server,
),
// The pipeline needs to finish startup phase within 30 seconds. If not, run context is canceled. Close is initiated.
plumber.Readiness(2*time.Second),
// The pipeline needs to gracefully close with 120 seconds. If not, internal run and close contexts are canceled.
plumber.CloseTimeout(2*time.Second),
// The pipeline will run for 120 seconds then will be closed gracefully.
plumber.TTL(2*time.Second),
// When given signals will be received pipeline will be closed gracefully.
plumber.SignalCloser(),
)
if err != nil {
fmt.Printf("Pipeline error: %v\n", err)
return
}
}
func fitHTTP(a *App) {
a.HTTP.EchoHandler.Define(func() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "back")
}
})
a.HTTP.HelloHandler.Resolve(func(r *plumber.Resolution[http.HandlerFunc]) {
r.Require(
a.Config.HelloMessage(),
).Then(func() {
message := a.Config.HelloMessage().Instance()
r.Resolve(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "message:"+message)
})
})
})
a.HTTP.Server.Resolve(func(r *plumber.ResolutionR[*http.Server]) {
r.Require(
&a.HTTP.HelloHandler,
&a.HTTP.EchoHandler,
).Then(func() {
httpServer := &http.Server{
Addr: ":9090",
}
http.HandleFunc("/hello", a.HTTP.HelloHandler.Instance())
http.HandleFunc("/echo", a.HTTP.EchoHandler.Instance())
r.ResolveAdapter(httpServer, plumber.GracefulRunner(func(ctx context.Context) error {
// ready is async to give time to server start
fmt.Println("HTTP server is starting")
if err := httpServer.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
err = fmt.Errorf("HTTP server ListenAndServe Error: %w", err)
return err
}
fmt.Println("HTTP server is closed")
return nil
}, func(ctx context.Context) error {
if err := httpServer.Shutdown(ctx); err != nil {
return fmt.Errorf("HTTP Server Shutdown Error: %w", err)
}
fmt.Println("Closed HTTP server")
return nil
}))
})
})
}