-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp_hook_test.go
More file actions
122 lines (106 loc) · 3.16 KB
/
Copy pathapp_hook_test.go
File metadata and controls
122 lines (106 loc) · 3.16 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
package wind
import (
"context"
"errors"
"testing"
"time"
)
// TestBeforeStartHookAbort verifies that a failing beforeStart hook aborts
// startup: no server is started and Run returns the hook error.
func TestBeforeStartHookAbort(t *testing.T) {
srv := newMockServer("srv")
hookErr := errors.New("lock acquisition failed")
app := New(
WithServer(srv),
WithBeforeStart(func(ctx context.Context) error { return hookErr }),
)
runErr := make(chan error, 1)
go func() { runErr <- app.Run(context.Background()) }()
select {
case err := <-runErr:
if !errors.Is(err, hookErr) {
t.Fatalf("expected hook error, got %v", err)
}
case <-time.After(2 * time.Second):
t.Fatal("Run did not return after beforeStart hook failure")
}
if srv.startCalled.Load() {
t.Fatal("server must not start when beforeStart hook fails")
}
if !errors.Is(app.Err(), hookErr) {
t.Fatalf("Err() must expose the hook error, got %v", app.Err())
}
}
// TestAfterStartHookOrder verifies that beforeStart and afterStart hooks run
// in registration order, that afterStart runs after the server Start has been
// invoked, and that the shutdown phases still observe beforeStop/afterStop.
func TestAfterStartHookOrder(t *testing.T) {
var order []string
srv := newMockServer("srv")
// Stop the app once afterStart has fired.
var app *App
app = New(
WithServer(srv),
WithBeforeStart(func(ctx context.Context) error {
order = append(order, "beforeStart")
return nil
}),
WithAfterStart(func(ctx context.Context) error {
order = append(order, "afterStart")
go func() {
_ = app.Stop(context.Background())
}()
return nil
}),
WithBeforeStop(func(ctx context.Context) error {
order = append(order, "beforeStop")
return nil
}),
WithAfterStop(func(ctx context.Context) error {
order = append(order, "afterStop")
return nil
}),
)
runErr := make(chan error, 1)
go func() { runErr <- app.Run(context.Background()) }()
select {
case err := <-runErr:
if err != nil {
t.Fatalf("Run returned error: %v", err)
}
case <-time.After(5 * time.Second):
t.Fatal("Run did not finish in time")
}
want := []string{"beforeStart", "afterStart", "beforeStop", "afterStop"}
if len(order) != len(want) {
t.Fatalf("hook order = %v, want %v", order, want)
}
for i := range want {
if order[i] != want[i] {
t.Fatalf("hook order = %v, want %v", order, want)
}
}
}
// TestAfterStartHookFailureTriggersShutdown verifies that a failing afterStart
// hook triggers graceful shutdown and its error is returned from Run.
func TestAfterStartHookFailureTriggersShutdown(t *testing.T) {
srv := newMockServer("srv")
hookErr := errors.New("registry unavailable")
app := New(
WithServer(srv),
WithAfterStart(func(ctx context.Context) error { return hookErr }),
)
runErr := make(chan error, 1)
go func() { runErr <- app.Run(context.Background()) }()
select {
case err := <-runErr:
if !errors.Is(err, hookErr) {
t.Fatalf("expected afterStart hook error, got %v", err)
}
case <-time.After(5 * time.Second):
t.Fatal("Run did not return after afterStart hook failure")
}
if !srv.stopCalled.Load() {
t.Fatal("server must be stopped after afterStart hook failure")
}
}