-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope_test.go
More file actions
199 lines (145 loc) · 4.36 KB
/
scope_test.go
File metadata and controls
199 lines (145 loc) · 4.36 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
package needle
import (
"context"
"sync/atomic"
"testing"
)
func TestScope_Singleton(t *testing.T) {
t.Parallel()
c := New()
var callCount atomic.Int32
_ = Provide(
c, func(ctx context.Context, r Resolver) (*testCounter, error) {
callCount.Add(1)
return &testCounter{id: int(callCount.Load())}, nil
},
)
first, _ := Invoke[*testCounter](c)
second, _ := Invoke[*testCounter](c)
third, _ := Invoke[*testCounter](c)
if first != second || second != third {
t.Error("singleton should return same instance")
}
if callCount.Load() != 1 {
t.Errorf("expected provider to be called once, got %d", callCount.Load())
}
}
func TestScope_Transient(t *testing.T) {
t.Parallel()
c := New()
var callCount atomic.Int32
_ = Provide(
c, func(ctx context.Context, r Resolver) (*testCounter, error) {
callCount.Add(1)
return &testCounter{id: int(callCount.Load())}, nil
}, WithScope(Transient),
)
ctx := context.Background()
first, _ := InvokeCtx[*testCounter](ctx, c)
second, _ := InvokeCtx[*testCounter](ctx, c)
third, _ := InvokeCtx[*testCounter](ctx, c)
if first == second || second == third {
t.Error("transient should return different instances")
}
if first.id == second.id || second.id == third.id {
t.Error("transient instances should have different ids")
}
if callCount.Load() != 3 {
t.Errorf("expected provider to be called 3 times, got %d", callCount.Load())
}
}
func TestScope_Request(t *testing.T) {
t.Parallel()
c := New()
var callCount atomic.Int32
_ = Provide(
c, func(ctx context.Context, r Resolver) (*testCounter, error) {
callCount.Add(1)
return &testCounter{id: int(callCount.Load())}, nil
}, WithScope(Request),
)
ctx1 := WithRequestScope(context.Background())
ctx2 := WithRequestScope(context.Background())
first1, _ := InvokeCtx[*testCounter](ctx1, c)
second1, _ := InvokeCtx[*testCounter](ctx1, c)
first2, _ := InvokeCtx[*testCounter](ctx2, c)
second2, _ := InvokeCtx[*testCounter](ctx2, c)
if first1 != second1 {
t.Error("same request scope should return same instance")
}
if first2 != second2 {
t.Error("same request scope should return same instance")
}
if first1 == first2 {
t.Error("different request scopes should return different instances")
}
if callCount.Load() != 2 {
t.Errorf("expected provider to be called 2 times, got %d", callCount.Load())
}
}
func TestScope_Request_NoScope(t *testing.T) {
t.Parallel()
c := New()
_ = Provide(
c, func(ctx context.Context, r Resolver) (*testCounter, error) {
return &testCounter{id: 1}, nil
}, WithScope(Request),
)
ctx := context.Background()
_, err := InvokeCtx[*testCounter](ctx, c)
if err == nil {
t.Error("expected error when request scope not in context")
}
}
func TestScope_Pooled(t *testing.T) {
t.Parallel()
c := New()
var callCount atomic.Int32
_ = Provide(
c, func(ctx context.Context, r Resolver) (*testCounter, error) {
callCount.Add(1)
return &testCounter{id: int(callCount.Load())}, nil
}, WithPoolSize(2),
)
ctx := context.Background()
first, _ := InvokeCtx[*testCounter](ctx, c)
second, _ := InvokeCtx[*testCounter](ctx, c)
if callCount.Load() != 2 {
t.Errorf("expected 2 new instances, got %d", callCount.Load())
}
c.Release("*github.com/danpasecinic/needle.testCounter", first)
c.Release("*github.com/danpasecinic/needle.testCounter", second)
third, _ := InvokeCtx[*testCounter](ctx, c)
fourth, _ := InvokeCtx[*testCounter](ctx, c)
if callCount.Load() != 2 {
t.Errorf("expected no new instances after release, got %d total calls", callCount.Load())
}
if third != first && third != second {
t.Error("pooled should reuse released instance")
}
if fourth != first && fourth != second {
t.Error("pooled should reuse released instance")
}
}
func TestScope_Pooled_Overflow(t *testing.T) {
t.Parallel()
c := New()
var callCount atomic.Int32
_ = Provide(
c, func(ctx context.Context, r Resolver) (*testCounter, error) {
callCount.Add(1)
return &testCounter{id: int(callCount.Load())}, nil
}, WithPoolSize(1),
)
ctx := context.Background()
first, _ := InvokeCtx[*testCounter](ctx, c)
second, _ := InvokeCtx[*testCounter](ctx, c)
c.Release("*github.com/danpasecinic/needle.testCounter", first)
released := c.Release("*github.com/danpasecinic/needle.testCounter", second)
if released {
t.Error("second release should fail (pool full)")
}
}
type testCounter struct {
id int
}