-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_test.go
More file actions
467 lines (373 loc) · 10.1 KB
/
pool_test.go
File metadata and controls
467 lines (373 loc) · 10.1 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package browserpm
import (
"errors"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestPoolPageState(t *testing.T) {
pp := &poolPage{
createdAt: time.Now(),
}
pp.lastUsed.Store(pp.createdAt.UnixNano())
if pp.getState() != pageIdle {
t.Errorf("expected initial state to be pageIdle, got %d", pp.getState())
}
pp.setState(pageClosing)
if pp.getState() != pageClosing {
t.Errorf("expected state to be pageClosing, got %d", pp.getState())
}
pp.setState(pageClosed)
if pp.getState() != pageClosed {
t.Errorf("expected state to be pageClosed, got %d", pp.getState())
}
}
func TestPoolPageIsAvailable(t *testing.T) {
now := time.Now()
t.Run("idle state is available", func(t *testing.T) {
pp := &poolPage{createdAt: now}
pp.lastUsed.Store(now.UnixNano())
if !pp.isAvailable(now, 0, 0) {
t.Error("expected idle page to be available")
}
})
t.Run("closing state is not available", func(t *testing.T) {
pp := &poolPage{createdAt: now}
pp.lastUsed.Store(now.UnixNano())
pp.setState(pageClosing)
if pp.isAvailable(now, 0, 0) {
t.Error("expected closing page to not be available")
}
})
t.Run("closed state is not available", func(t *testing.T) {
pp := &poolPage{createdAt: now}
pp.lastUsed.Store(now.UnixNano())
pp.setState(pageClosed)
if pp.isAvailable(now, 0, 0) {
t.Error("expected closed page to not be available")
}
})
t.Run("page near TTL grace period is not available", func(t *testing.T) {
ttl := 30 * time.Minute
grace := 5 * time.Minute
pp := &poolPage{createdAt: now.Add(-26 * time.Minute)}
pp.lastUsed.Store(now.UnixNano())
if pp.isAvailable(now, ttl, grace) {
t.Error("expected page near TTL to not be available")
}
})
t.Run("page within TTL is available", func(t *testing.T) {
ttl := 30 * time.Minute
grace := 5 * time.Minute
pp := &poolPage{createdAt: now.Add(-10 * time.Minute)}
pp.lastUsed.Store(now.UnixNano())
if !pp.isAvailable(now, ttl, grace) {
t.Error("expected page within TTL to be available")
}
})
}
func TestPoolPageActiveOps(t *testing.T) {
pp := &poolPage{}
if pp.activeOps.Load() != 0 {
t.Errorf("expected initial activeOps to be 0, got %d", pp.activeOps.Load())
}
pp.activeOps.Add(1)
if pp.activeOps.Load() != 1 {
t.Errorf("expected activeOps to be 1, got %d", pp.activeOps.Load())
}
pp.activeOps.Add(2)
if pp.activeOps.Load() != 3 {
t.Errorf("expected activeOps to be 3, got %d", pp.activeOps.Load())
}
pp.activeOps.Add(-3)
if pp.activeOps.Load() != 0 {
t.Errorf("expected activeOps to be 0 after decrement, got %d", pp.activeOps.Load())
}
}
func TestPoolPageUseCount(t *testing.T) {
pp := &poolPage{}
for i := 0; i < 10; i++ {
pp.useCount.Add(1)
}
if pp.useCount.Load() != 10 {
t.Errorf("expected useCount to be 10, got %d", pp.useCount.Load())
}
}
func TestPoolPageConcurrentOps(t *testing.T) {
pp := &poolPage{}
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
pp.activeOps.Add(1)
pp.useCount.Add(1)
}()
}
wg.Wait()
if pp.activeOps.Load() != 100 {
t.Errorf("expected activeOps to be 100, got %d", pp.activeOps.Load())
}
if pp.useCount.Load() != 100 {
t.Errorf("expected useCount to be 100, got %d", pp.useCount.Load())
}
}
func TestPoolStatsEmpty(t *testing.T) {
cfg := PoolConfig{
MinPages: 1,
MaxPages: 5,
ScheduleStrategy: "round-robin",
}
logger := NewNopLogger()
pool := newPagePool(cfg, nil, logger, nil)
stats := pool.Stats()
if stats.TotalPages != 0 {
t.Errorf("expected total pages to be 0, got %d", stats.TotalPages)
}
if stats.IdlePages != 0 {
t.Errorf("expected idle pages to be 0, got %d", stats.IdlePages)
}
if stats.ActiveOps != 0 {
t.Errorf("expected activeOps to be 0, got %d", stats.ActiveOps)
}
}
func TestPoolActiveOpsConcurrent(t *testing.T) {
pp1 := &poolPage{}
pp2 := &poolPage{}
pp3 := &poolPage{}
cfg := PoolConfig{
MinPages: 1,
MaxPages: 5,
ScheduleStrategy: "round-robin",
}
logger := NewNopLogger()
pool := newPagePool(cfg, nil, logger, nil)
pool.pages = []*poolPage{pp1, pp2, pp3}
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(3)
go func() {
defer wg.Done()
pp1.activeOps.Add(1)
}()
go func() {
defer wg.Done()
pp2.activeOps.Add(1)
}()
go func() {
defer wg.Done()
pp3.activeOps.Add(1)
}()
}
wg.Wait()
total := pool.ActiveOps()
if total != 300 {
t.Errorf("expected total activeOps to be 300, got %d", total)
}
}
func TestPoolSchedulerRoundRobin(t *testing.T) {
pages := make([]*poolPage, 5)
for i := range pages {
pages[i] = &poolPage{id: string(rune('a' + i))}
}
scheduler := NewScheduler("round-robin")
selected := make(map[string]int)
for i := 0; i < 25; i++ {
pp := scheduler.Select(pages)
if pp != nil {
selected[pp.id]++
}
}
for id, count := range selected {
if count != 5 {
t.Errorf("expected each page to be selected 5 times, got %d for %s", count, id)
}
}
}
func TestPoolSchedulerEmpty(t *testing.T) {
scheduler := NewScheduler("round-robin")
pp := scheduler.Select(nil)
if pp != nil {
t.Error("expected nil for empty page list")
}
pp = scheduler.Select([]*poolPage{})
if pp != nil {
t.Error("expected nil for empty page slice")
}
}
func TestPoolSchedulerReset(t *testing.T) {
pages := make([]*poolPage, 3)
for i := range pages {
pages[i] = &poolPage{id: string(rune('a' + i))}
}
scheduler := NewScheduler("round-robin")
for i := 0; i < 5; i++ {
_ = scheduler.Select(pages)
}
scheduler.Reset()
pp := scheduler.Select(pages)
if pp == nil || pp.id != "a" {
t.Error("expected reset to start from first page again")
}
}
func TestPoolIsHealthyEmpty(t *testing.T) {
cfg := PoolConfig{
MinPages: 1,
MaxPages: 5,
ScheduleStrategy: "round-robin",
}
logger := NewNopLogger()
pool := newPagePool(cfg, nil, logger, nil)
if !pool.IsHealthy() {
t.Error("expected empty pool to be healthy")
}
}
func TestPoolPageTTLTransition(t *testing.T) {
now := time.Now()
ttl := 30 * time.Minute
grace := 5 * time.Minute
pp := &poolPage{createdAt: now.Add(-15 * time.Minute)}
pp.lastUsed.Store(now.UnixNano())
if !pp.isAvailable(now, ttl, grace) {
t.Error("expected page within TTL to be available")
}
pp = &poolPage{createdAt: now.Add(-26 * time.Minute)}
pp.lastUsed.Store(now.UnixNano())
if pp.isAvailable(now, ttl, grace) {
t.Error("expected page near TTL grace period to not be available")
}
pp = &poolPage{createdAt: now.Add(-35 * time.Minute)}
pp.lastUsed.Store(now.UnixNano())
if pp.isAvailable(now, ttl, grace) {
t.Error("expected page past TTL to not be available")
}
}
func TestPoolPageStateTransitions(t *testing.T) {
pp := &poolPage{createdAt: time.Now()}
pp.lastUsed.Store(time.Now().UnixNano())
if pp.getState() != pageIdle {
t.Errorf("expected initial state to be pageIdle, got %d", pp.getState())
}
pp.setState(pageClosing)
if pp.getState() != pageClosing {
t.Errorf("expected state to be pageClosing, got %d", pp.getState())
}
pp.setState(pageClosed)
if pp.getState() != pageClosed {
t.Errorf("expected state to be pageClosed, got %d", pp.getState())
}
pp.setState(pageIdle)
if pp.getState() != pageIdle {
t.Errorf("expected state to return to pageIdle, got %d", pp.getState())
}
}
func TestPoolPageLastUsed(t *testing.T) {
pp := &poolPage{createdAt: time.Now()}
pp.lastUsed.Store(pp.createdAt.UnixNano())
initialTime := pp.lastUsed.Load()
time.Sleep(10 * time.Millisecond)
newTime := time.Now().UnixNano()
pp.lastUsed.Store(newTime)
if pp.lastUsed.Load() == initialTime {
t.Error("expected lastUsed to be updated")
}
if pp.lastUsed.Load() != newTime {
t.Error("expected lastUsed to match new time")
}
}
func TestPoolPageConcurrentStateChange(t *testing.T) {
pp := &poolPage{createdAt: time.Now()}
pp.lastUsed.Store(pp.createdAt.UnixNano())
var wg sync.WaitGroup
var stateChanges atomic.Int64
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
state := pageState(i % 3)
pp.setState(state)
stateChanges.Add(1)
}(i)
}
wg.Wait()
if stateChanges.Load() != 100 {
t.Errorf("expected 100 state changes, got %d", stateChanges.Load())
}
}
func TestPoolConfigDefaults(t *testing.T) {
cfg, err := NewConfig()
if err != nil {
t.Fatalf("NewConfig: %v", err)
}
if cfg.Pool.MinPages <= 0 {
t.Error("expected default MinPages to be positive")
}
if cfg.Pool.MaxPages < cfg.Pool.MinPages {
t.Error("expected MaxPages >= MinPages")
}
if cfg.Pool.TTL <= 0 {
t.Error("expected TTL to be positive")
}
if cfg.Pool.GracePeriod <= 0 {
t.Error("expected GracePeriod to be positive")
}
}
func TestPoolErrorWrapping(t *testing.T) {
originalErr := errors.New("original error")
wrapped := WrapError(originalErr, ErrInternal, "pool error")
if wrapped.Code != ErrInternal {
t.Errorf("expected error code ErrInternal, got %v", wrapped.Code)
}
if wrapped.Cause != originalErr {
t.Error("expected cause to be original error")
}
var bpmErr *BpmError
if !errors.As(wrapped, &bpmErr) {
t.Error("expected error to be BpmError")
}
}
func TestPoolPageZeroTTLGrace(t *testing.T) {
now := time.Now()
pp := &poolPage{createdAt: now.Add(-1 * time.Hour)}
pp.lastUsed.Store(now.UnixNano())
if !pp.isAvailable(now, 0, 0) {
t.Error("expected page to be available with zero TTL/grace")
}
if !pp.isAvailable(now, 0, 10*time.Second) {
t.Error("expected page to be available with zero TTL")
}
if !pp.isAvailable(now, 10*time.Second, 0) {
t.Error("expected page to be available with zero grace")
}
}
func TestPoolSchedulerConcurrent(t *testing.T) {
pages := make([]*poolPage, 10)
for i := range pages {
pages[i] = &poolPage{id: string(rune('0' + i))}
}
scheduler := NewScheduler("round-robin")
var wg sync.WaitGroup
var mu sync.Mutex
selected := make(map[string]int)
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
pp := scheduler.Select(pages)
if pp != nil {
mu.Lock()
selected[pp.id]++
mu.Unlock()
}
}()
}
wg.Wait()
totalSelected := 0
for _, count := range selected {
totalSelected += count
}
if totalSelected != 100 {
t.Errorf("expected 100 selections, got %d", totalSelected)
}
}