Repository navigation
Expand file tree
/
Copy pathiris_builder_init_test.go
More file actions
133 lines (116 loc) · 3.06 KB
/
Copy pathiris_builder_init_test.go
File metadata and controls
133 lines (116 loc) · 3.06 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
package iris_test
import (
stdContext "context"
"fmt"
"strings"
"testing"
"github.com/kataras/iris/v14"
)
type warmupService struct{ inits int }
func (s *warmupService) Init(ctx stdContext.Context) error {
if ctx == nil {
return fmt.Errorf("nil context")
}
s.inits++
return nil
}
type failingInitService struct{}
func (failingInitService) Init(stdContext.Context) error {
return fmt.Errorf("warmup failed")
}
// Services implementing Initializer are initialized exactly once by Build,
// after dependency injection, even when registered twice or built twice.
func TestBuilderRunsInitializersOnce(t *testing.T) {
svc := &warmupService{}
b := iris.NewBuilder().
NoPrefix().
AllowOrigin("none").
Compression(false).
LogRequests(false).
NoHealth().
Services(svc, svc).
RouterMiddlewares().
Middlewares()
b.Build()
if svc.inits != 1 {
t.Fatalf("expected exactly one Init call after Build, got %d", svc.inits)
}
b.Build() // building again must not re-initialize.
if svc.inits != 1 {
t.Fatalf("expected Init to run once across builds, got %d", svc.inits)
}
}
// Constructor-registered services get their outputs initialized too.
func TestBuilderRunsInitializerOnConstructorOutput(t *testing.T) {
var captured *warmupService
ctor := func() *warmupService {
captured = &warmupService{}
return captured
}
iris.NewBuilder().
NoPrefix().
AllowOrigin("none").
Compression(false).
LogRequests(false).
NoHealth().
Services(ctor).
RouterMiddlewares().
Middlewares().
Build()
if captured == nil {
t.Fatal("expected the constructor to run during registration")
}
if captured.inits != 1 {
t.Fatalf("expected the constructor output to be initialized once, got %d", captured.inits)
}
}
// A failing Init stops the application from building, like every other
// builder misconfiguration.
func TestBuilderInitializerFailurePanics(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("expected Build to panic on Init failure")
}
if !strings.Contains(fmt.Sprint(r), "warmup failed") {
t.Fatalf("unexpected panic value: %v", r)
}
}()
iris.NewBuilder().
NoPrefix().
AllowOrigin("none").
Compression(false).
LogRequests(false).
NoHealth().
Services(&failingInitService{}).
RouterMiddlewares().
Middlewares().
Build()
}
// A function with no service to provide is a mistake, not a shutdown closer:
// it used to be swallowed and registered as one.
func TestBuilderServicesRejectsZeroOutputFunctions(t *testing.T) {
for _, dep := range []any{func() {}, func() error { return nil }} {
func() {
defer func() {
r := recover()
if r == nil {
t.Fatalf("expected Services(%T) to panic", dep)
}
msg := fmt.Sprint(r)
if !strings.Contains(msg, "iris: builder: Services:") ||
!strings.Contains(msg, "provides no service") ||
!strings.Contains(msg, "Deferrables") {
t.Fatalf("unexpected panic value for %T: %v", dep, r)
}
}()
iris.NewBuilder().
NoPrefix().
AllowOrigin("none").
Compression(false).
LogRequests(false).
NoHealth().
Services(dep)
}()
}
}