Repository navigation
Expand file tree
/
Copy pathdi_test.go
More file actions
233 lines (210 loc) · 6.07 KB
/
Copy pathdi_test.go
File metadata and controls
233 lines (210 loc) · 6.07 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
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
package di_test
import (
"context"
"errors"
"strings"
"sync"
"sync/atomic"
"testing"
"golang.yandex/di"
)
type Config struct{ DSN string }
type DB struct{ dsn string }
type Repo struct{ db *DB }
type Reader interface{ Read() string }
func (r *Repo) Read() string { return r.db.dsn }
type Handler struct{ name string }
func newApp() *di.Scope {
s := di.New()
s.Value(Config{DSN: "pg://primary"})
s.Provide(func(s *di.Scope) *DB { return &DB{dsn: s.Get[Config]().DSN} })
s.Provide(func(s *di.Scope) *Repo { return &Repo{db: s.Get[*DB]()} })
return s
}
func TestResolveChain(t *testing.T) {
s := newApp()
r, err := s.Resolve[*Repo]()
if err != nil {
t.Fatal(err)
}
if r.Read() != "pg://primary" {
t.Fatalf("got %q", r.Read())
}
if s.Get[*DB]() != r.db {
t.Fatal("singleton was built twice")
}
}
func TestGroups(t *testing.T) {
s := di.New()
s.Provide(func(*di.Scope) Handler { return Handler{"users"} }).Group()
s.Provide(func(*di.Scope) Handler { return Handler{"orders"} }).Group()
child := s.Child("child")
child.Provide(func(*di.Scope) Handler { return Handler{"admin"} }).Group()
got := child.All[Handler]()
if len(got) != 3 {
t.Fatalf("got %v", got)
}
if len(s.All[Handler]()) != 2 {
t.Fatal("parent must not see child group members")
}
}
func TestMaybe(t *testing.T) {
s := newApp()
if _, ok := s.Maybe[string](); ok {
t.Fatal("string is not provided")
}
if db, ok := s.Maybe[*DB](); !ok || db.dsn != "pg://primary" {
t.Fatal("DB should be found")
}
}
func TestMissingDependency(t *testing.T) {
s := di.New()
s.Provide(func(s *di.Scope) *Repo { return &Repo{db: s.Get[*DB]()} })
_, err := s.Resolve[*Repo]()
if !errors.Is(err, di.ErrNotProvided) {
t.Fatalf("want ErrNotProvided, got %v", err)
}
for _, want := range []string{"di_test.DB", "needed by", "di_test.Repo", "di_test.go:"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("error %q lacks %q", err, want)
}
}
}
func TestCycle(t *testing.T) {
type A struct{ b any }
type B struct{ a any }
s := di.New()
s.Provide(func(s *di.Scope) *A { return &A{s.Get[*B]()} })
s.Provide(func(s *di.Scope) *B { return &B{s.Get[*A]()} })
_, err := s.Resolve[*A]()
if !errors.Is(err, di.ErrCycle) {
t.Fatalf("want ErrCycle, got %v", err)
}
}
func TestTopLevelGetPanicsWithError(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("expected panic")
}
if err, ok := r.(error); !ok || !errors.Is(err, di.ErrNotProvided) {
t.Fatalf("panic value should be the error, got %#v", r)
}
}()
di.New().Get[*DB]()
}
func TestConstructorPanicBecomesError(t *testing.T) {
s := di.New()
s.Provide(func(*di.Scope) *DB { panic("boom") })
_, err := s.Resolve[*DB]()
if err == nil || !strings.Contains(err.Error(), "panic: boom") {
t.Fatalf("got %v", err)
}
}
func TestChildOverride(t *testing.T) {
s := newApp()
req := s.Child("request")
req.Value(&DB{dsn: "sqlite://memory"})
req.Provide(func(s *di.Scope) *Repo { return &Repo{db: s.Get[*DB]()} })
if got := req.Get[*Repo]().Read(); got != "sqlite://memory" {
t.Fatalf("child: %q", got)
}
if got := s.Get[*Repo]().Read(); got != "pg://primary" {
t.Fatalf("root: %q", got)
}
}
func TestChildSeesParentSingletons(t *testing.T) {
s := newApp()
child := s.Child("child")
if child.Get[*DB]() != s.Get[*DB]() {
t.Fatal("child must reuse the parent's singleton")
}
}
func TestLifecycleOrder(t *testing.T) {
var log []string
s := di.New()
s.Provide(func(*di.Scope) *DB { return &DB{} }).
OnStart(func(context.Context, *DB) error { log = append(log, "start db"); return nil }).
OnStop(func(context.Context, *DB) error { log = append(log, "stop db"); return nil })
s.Provide(func(s *di.Scope) *Repo { return &Repo{db: s.Get[*DB]()} }).Eager().
OnStart(func(context.Context, *Repo) error { log = append(log, "start repo"); return nil }).
OnStop(func(context.Context, *Repo) error { log = append(log, "stop repo"); return nil })
if err := s.Start(t.Context()); err != nil {
t.Fatal(err)
}
if err := s.Stop(t.Context()); err != nil {
t.Fatal(err)
}
want := "start db,start repo,stop repo,stop db"
if got := strings.Join(log, ","); got != want {
t.Fatalf("order %q, want %q", got, want)
}
}
func TestStopCollectsErrors(t *testing.T) {
s := di.New()
e1, e2 := errors.New("e1"), errors.New("e2")
s.Provide(func(*di.Scope) *DB { return &DB{} }).OnStop(func(context.Context, *DB) error { return e1 })
s.Provide(func(s *di.Scope) *Repo { return &Repo{db: s.Get[*DB]()} }).OnStop(func(context.Context, *Repo) error { return e2 })
s.Get[*Repo]()
err := s.Stop(t.Context())
if !errors.Is(err, e1) || !errors.Is(err, e2) {
t.Fatalf("want both errors, got %v", err)
}
}
func TestStartReportsMissingEagerDependency(t *testing.T) {
s := di.New()
s.Provide(func(s *di.Scope) *Repo { return &Repo{db: s.Get[*DB]()} }).Eager()
if err := s.Start(t.Context()); !errors.Is(err, di.ErrNotProvided) {
t.Fatalf("got %v", err)
}
}
func TestModifyAfterFreezePanics(t *testing.T) {
s := di.New()
b := s.Value(&DB{})
s.Get[*DB]()
defer func() {
if r := recover(); r == nil || !strings.Contains(r.(string), "modified after") {
t.Fatalf("got %v", r)
}
}()
b.Eager()
}
func TestLateRegistrationAfterFreeze(t *testing.T) {
s := di.New()
s.Value(&DB{dsn: "a"})
s.Get[*DB]()
s.Value(Config{DSN: "b"})
if s.Get[Config]().DSN != "b" {
t.Fatal("registration after first resolve must be visible")
}
}
func TestMethodValueInference(t *testing.T) {
s := di.New()
s.Value(&DB{dsn: "x"})
var get func() *DB = s.Get // Go 1.27 infers T from the target function type
if get().dsn != "x" {
t.Fatal("bad")
}
var resolve func() (*DB, error) = s.Resolve
if _, err := resolve(); err != nil {
t.Fatal(err)
}
}
func TestConcurrentResolutionBuildsOnce(t *testing.T) {
s := di.New()
var builds atomic.Int32
s.Provide(func(*di.Scope) *DB { builds.Add(1); return &DB{} })
s.Provide(func(s *di.Scope) *Repo { return &Repo{db: s.Get[*DB]()} })
var wg sync.WaitGroup
for range 64 {
wg.Go(func() {
if _, err := s.Resolve[*Repo](); err != nil {
t.Error(err)
}
})
}
wg.Wait()
if builds.Load() != 1 {
t.Fatalf("built %d times", builds.Load())
}
}