Repository navigation
Expand file tree
/
Copy pathoptions_test.go
More file actions
298 lines (257 loc) · 8.06 KB
/
Copy pathoptions_test.go
File metadata and controls
298 lines (257 loc) · 8.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
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
/*
Copyright 2026 The ARCORIS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pool
import (
"testing"
"arcoris.dev/pool/internal/testutil"
)
// optionsHookCalls records which resolved hooks were actually executed.
//
// The tests use it both to prove that resolve() itself stays side-effect free
// and to verify that resolved callbacks still dispatch to the original hooks.
type optionsHookCalls struct {
new int
reset int
reuse int
drop int
}
// optionsTestObject models a pointer-like pooled value.
//
// Tests mutate its fields through Reset and OnDrop hooks so they can verify
// that resolve() preserves hook behaviour and that default hooks stay inert.
type optionsTestObject struct {
count int
payload []byte
resetSeen bool
dropSeen bool
}
// optionsTestValue exercises Options with a value-typed T.
//
// Keeping a non-pointer fixture in the suite makes the default-policy tests
// prove that value instantiations behave correctly as well.
type optionsTestValue struct {
N int
}
func TestOptionsResolvePanicsWhenNewIsNil(t *testing.T) {
testutil.AssertPanicMessage(
t,
"Options.resolve() with nil New",
func() {
var opts Options[*optionsTestObject]
_ = opts.resolve()
},
"pool: Options.New must not be nil",
)
}
func TestOptionsResolveDoesNotInvokeHooks(t *testing.T) {
calls := optionsHookCalls{}
opts := Options[*optionsTestObject]{
New: func() *optionsTestObject {
calls.new++
return &optionsTestObject{}
},
Reset: func(*optionsTestObject) {
calls.reset++
},
Reuse: func(*optionsTestObject) bool {
calls.reuse++
return true
},
OnDrop: func(*optionsTestObject) {
calls.drop++
},
}
_ = opts.resolve()
if calls != (optionsHookCalls{}) {
t.Fatalf("resolve() invoked hooks eagerly: got %+v, want all counters to remain zero", calls)
}
}
func TestOptionsResolveInstallsDefaultsForValueType(t *testing.T) {
resolved := Options[optionsTestValue]{
New: func() optionsTestValue {
return optionsTestValue{N: 41}
},
}.resolve()
if got := resolved.newFn(); got != (optionsTestValue{N: 41}) {
t.Fatalf("resolved newFn() = %+v, want %+v", got, optionsTestValue{N: 41})
}
value := optionsTestValue{N: 99}
resolved.resetFn(value)
if value != (optionsTestValue{N: 99}) {
t.Fatalf("default resetFn mutated value-typed input: got %+v, want %+v", value, optionsTestValue{N: 99})
}
if !resolved.reuseFn(optionsTestValue{N: -1}) {
t.Fatal("default reuseFn returned false for value-typed input, want true")
}
resolved.dropFn(optionsTestValue{N: 7})
}
func TestOptionsResolveInstallsDefaultsForPointerType(t *testing.T) {
expected := &optionsTestObject{count: 5, payload: []byte("abc")}
resolved := Options[*optionsTestObject]{
New: func() *optionsTestObject {
return expected
},
}.resolve()
if got := resolved.newFn(); got != expected {
t.Fatalf("resolved newFn() pointer = %p, want %p", got, expected)
}
object := &optionsTestObject{count: 77, payload: []byte("payload")}
beforeCount := object.count
beforePayload := string(object.payload)
resolved.resetFn(object)
if object.count != beforeCount || string(object.payload) != beforePayload {
t.Fatalf(
"default resetFn mutated pointer-backed input: got count=%d payload=%q, want count=%d payload=%q",
object.count,
string(object.payload),
beforeCount,
beforePayload,
)
}
if !resolved.reuseFn(object) {
t.Fatal("default reuseFn returned false for pointer-typed input, want true")
}
resolved.dropFn(object)
}
func TestOptionsResolvePreservesCustomHooks(t *testing.T) {
calls := optionsHookCalls{}
marker := &optionsTestObject{count: 10, payload: []byte("marker")}
resolved := Options[*optionsTestObject]{
New: func() *optionsTestObject {
calls.new++
return marker
},
Reset: func(v *optionsTestObject) {
calls.reset++
v.count = 0
v.payload = v.payload[:0]
v.resetSeen = true
},
Reuse: func(v *optionsTestObject) bool {
calls.reuse++
return v.count <= 64
},
OnDrop: func(v *optionsTestObject) {
calls.drop++
v.dropSeen = true
},
}.resolve()
if got := resolved.newFn(); got != marker {
t.Fatalf("resolved newFn() pointer = %p, want %p", got, marker)
}
if calls.new != 1 {
t.Fatalf("custom newFn call count = %d, want 1", calls.new)
}
reusable := &optionsTestObject{count: 32, payload: []byte("retain")}
if !resolved.reuseFn(reusable) {
t.Fatal("custom reuseFn rejected reusable object, want true")
}
resolved.resetFn(reusable)
if calls.reuse != 1 {
t.Fatalf("custom reuseFn call count after accepted path = %d, want 1", calls.reuse)
}
if calls.reset != 1 {
t.Fatalf("custom resetFn call count after accepted path = %d, want 1", calls.reset)
}
if reusable.count != 0 {
t.Fatalf("custom resetFn left count = %d, want 0", reusable.count)
}
if len(reusable.payload) != 0 {
t.Fatalf("custom resetFn left payload length = %d, want 0", len(reusable.payload))
}
if !reusable.resetSeen {
t.Fatal("custom resetFn did not mark resetSeen")
}
if reusable.dropSeen {
t.Fatal("drop callback ran on accepted object, want it to remain untouched")
}
dropped := &optionsTestObject{count: 128, payload: []byte("drop")}
if resolved.reuseFn(dropped) {
t.Fatal("custom reuseFn accepted dropped object, want false")
}
resolved.dropFn(dropped)
if calls.reuse != 2 {
t.Fatalf("custom reuseFn call count after dropped path = %d, want 2", calls.reuse)
}
if calls.drop != 1 {
t.Fatalf("custom dropFn call count = %d, want 1", calls.drop)
}
if !dropped.dropSeen {
t.Fatal("custom dropFn did not mark dropSeen")
}
}
func TestOptionsResolveAllowsMixingCustomAndDefaultHooks(t *testing.T) {
resetCalls := 0
reuseCalls := 0
resolved := Options[*optionsTestObject]{
New: func() *optionsTestObject {
return &optionsTestObject{}
},
Reset: func(v *optionsTestObject) {
resetCalls++
v.count = 0
},
Reuse: func(v *optionsTestObject) bool {
reuseCalls++
return v.count == 0
},
}.resolve()
object := &optionsTestObject{count: 0}
if !resolved.reuseFn(object) {
t.Fatal("custom reuseFn rejected accepted object, want true")
}
resolved.resetFn(object)
if object.count != 0 {
t.Fatalf("custom resetFn left count = %d, want 0", object.count)
}
if resetCalls != 1 {
t.Fatalf("custom resetFn call count = %d, want 1", resetCalls)
}
if reuseCalls != 1 {
t.Fatalf("custom reuseFn call count = %d, want 1", reuseCalls)
}
resolved.dropFn(&optionsTestObject{count: 99})
}
func TestDefaultPolicies(t *testing.T) {
// These tests pin the standalone helper contracts directly so regressions in
// resolve() can be separated from regressions in the default policy helpers.
t.Run("noopReset leaves object unchanged", func(t *testing.T) {
object := &optionsTestObject{count: 11, payload: []byte("keep")}
beforeCount := object.count
beforePayload := string(object.payload)
noopReset(object)
if object.count != beforeCount || string(object.payload) != beforePayload {
t.Fatalf(
"noopReset mutated object: got count=%d payload=%q, want count=%d payload=%q",
object.count,
string(object.payload),
beforeCount,
beforePayload,
)
}
})
t.Run("alwaysReuse returns true for value and pointer inputs", func(t *testing.T) {
if !alwaysReuse(0) {
t.Fatal("alwaysReuse(0) = false, want true")
}
if !alwaysReuse(&optionsTestObject{count: 1}) {
t.Fatal("alwaysReuse(pointer) = false, want true")
}
})
t.Run("noopDrop accepts pointer and value inputs", func(_ *testing.T) {
// noopDrop has no observable state change; the contract is simply that it
// accepts any T without panicking.
noopDrop(&optionsTestObject{count: 1})
noopDrop(optionsTestValue{N: 2})
})
}