@@ -209,15 +209,15 @@ func TestOpen(t *testing.T) {
209
209
assert .EqualValues (t , 6 , state .lastID .Load ())
210
210
211
211
// SetCapacity
212
- err = p .SetCapacity (ctx , 3 )
213
- require . NoError ( t , err )
212
+ p .SetCapacity (3 )
213
+
214
214
assert .EqualValues (t , 3 , state .open .Load ())
215
215
assert .EqualValues (t , 6 , state .lastID .Load ())
216
216
assert .EqualValues (t , 3 , p .Capacity ())
217
217
assert .EqualValues (t , 3 , p .Available ())
218
218
219
- err = p .SetCapacity (ctx , 6 )
220
- require . NoError ( t , err )
219
+ p .SetCapacity (6 )
220
+
221
221
assert .EqualValues (t , 6 , p .Capacity ())
222
222
assert .EqualValues (t , 6 , p .Available ())
223
223
@@ -266,13 +266,8 @@ func TestShrinking(t *testing.T) {
266
266
require .NoError (t , err )
267
267
resources [i ] = r
268
268
}
269
- done := make (chan bool )
270
- go func () {
271
- err := p .SetCapacity (ctx , 3 )
272
- require .NoError (t , err )
273
269
274
- done <- true
275
- }()
270
+ p .SetCapacity (3 )
276
271
expected := map [string ]any {
277
272
"Capacity" : 3 ,
278
273
"Available" : - 1 , // negative because we've borrowed past our capacity
@@ -294,27 +289,59 @@ func TestShrinking(t *testing.T) {
294
289
assert .Equal (t , expected , stats )
295
290
}
296
291
}
297
- // There are already 2 resources available in the pool.
298
- // So, returning one should be enough for SetCapacity to complete.
292
+
293
+ p .put (resources [0 ], resources [0 ].generation )
294
+ assert .Equal (t , map [string ]any {
295
+ "Capacity" : 3 ,
296
+ "Available" : 0 ,
297
+ "Active" : 4 ,
298
+ "InUse" : 3 ,
299
+ "WaitCount" : 0 ,
300
+ "WaitTime" : time .Duration (0 ),
301
+ "IdleTimeout" : 1 * time .Second ,
302
+ "IdleClosed" : 0 ,
303
+ "MaxLifetimeClosed" : 0 ,
304
+ }, p .StatsJSON ())
305
+
306
+ p .put (resources [1 ], resources [1 ].generation )
307
+ assert .Equal (t , map [string ]any {
308
+ "Capacity" : 3 ,
309
+ "Available" : 1 ,
310
+ "Active" : 4 ,
311
+ "InUse" : 2 ,
312
+ "WaitCount" : 0 ,
313
+ "WaitTime" : time .Duration (0 ),
314
+ "IdleTimeout" : 1 * time .Second ,
315
+ "IdleClosed" : 0 ,
316
+ "MaxLifetimeClosed" : 0 ,
317
+ }, p .StatsJSON ())
318
+
319
+ p .put (resources [2 ], resources [2 ].generation )
320
+ assert .Equal (t , map [string ]any {
321
+ "Capacity" : 3 ,
322
+ "Available" : 2 ,
323
+ "Active" : 4 ,
324
+ "InUse" : 1 ,
325
+ "WaitCount" : 0 ,
326
+ "WaitTime" : time .Duration (0 ),
327
+ "IdleTimeout" : 1 * time .Second ,
328
+ "IdleClosed" : 0 ,
329
+ "MaxLifetimeClosed" : 0 ,
330
+ }, p .StatsJSON ())
331
+
299
332
p .put (resources [3 ], resources [3 ].generation )
300
- <- done
301
- // Return the rest of the resources
302
- for i := 0 ; i < 3 ; i ++ {
303
- p .put (resources [i ], resources [i ].generation )
304
- }
305
- stats := p .StatsJSON ()
306
- expected = map [string ]any {
333
+ assert .Equal (t , map [string ]any {
307
334
"Capacity" : 3 ,
308
335
"Available" : 3 ,
309
336
"Active" : 3 ,
310
337
"InUse" : 0 ,
311
338
"WaitCount" : 0 ,
312
339
"WaitTime" : time .Duration (0 ),
313
340
"IdleTimeout" : 1 * time .Second ,
314
- "IdleClosed" : 0 ,
341
+ "IdleClosed" : 1 ,
315
342
"MaxLifetimeClosed" : 0 ,
316
- }
317
- assert . Equal ( t , expected , stats )
343
+ }, p . StatsJSON ())
344
+
318
345
assert .EqualValues (t , 3 , state .open .Load ())
319
346
320
347
// Ensure no deadlock if SetCapacity is called after we start
@@ -330,36 +357,36 @@ func TestShrinking(t *testing.T) {
330
357
require .NoError (t , err )
331
358
resources [i ] = r
332
359
}
360
+
361
+ wg := sync.WaitGroup {}
333
362
// This will wait because pool is empty
363
+ wg .Add (1 )
334
364
go func () {
335
365
r , err := p .Get (ctx , nil )
336
366
require .NoError (t , err )
337
367
p .put (r , r .generation )
338
- done <- true
368
+ wg . Done ()
339
369
}()
340
370
341
- // This will also wait
342
- go func () {
343
- err := p .SetCapacity (ctx , 2 )
344
- require .NoError (t , err )
345
- done <- true
346
- }()
371
+ p .SetCapacity (2 )
372
+
347
373
time .Sleep (10 * time .Millisecond )
348
374
349
375
// This should not hang
350
376
for i := 0 ; i < 3 ; i ++ {
351
377
p .put (resources [i ], resources [i ].generation )
352
378
}
353
- <- done
354
- <- done
379
+ wg . Wait ()
380
+
355
381
assert .EqualValues (t , 2 , p .Capacity ())
356
382
assert .EqualValues (t , 2 , p .Available ())
357
383
assert .EqualValues (t , 1 , p .Metrics .WaitCount ())
358
384
assert .EqualValues (t , p .Metrics .WaitCount (), len (state .waits ))
359
385
assert .EqualValues (t , 2 , state .open .Load ())
360
386
361
387
// Test race condition of SetCapacity with itself
362
- err = p .SetCapacity (ctx , 3 )
388
+ p .SetCapacity (3 )
389
+
363
390
require .NoError (t , err )
364
391
for i := 0 ; i < 3 ; i ++ {
365
392
var r * Pooled [* TestConn ]
@@ -372,35 +399,34 @@ func TestShrinking(t *testing.T) {
372
399
require .NoError (t , err )
373
400
resources [i ] = r
374
401
}
402
+
375
403
// This will wait because pool is empty
404
+ wg .Add (1 )
376
405
go func () {
377
406
r , err := p .Get (ctx , nil )
378
407
require .NoError (t , err )
379
408
p .put (r , r .generation )
380
- done <- true
409
+ wg . Done ()
381
410
}()
382
411
time .Sleep (10 * time .Millisecond )
383
412
384
413
// This will wait till we Put
385
- go func () {
386
- err := p .SetCapacity (ctx , 2 )
387
- require .NoError (t , err )
388
- }()
414
+ p .SetCapacity (2 )
415
+
389
416
time .Sleep (10 * time .Millisecond )
390
- go func () {
391
- err := p .SetCapacity (ctx , 4 )
392
- require .NoError (t , err )
393
- }()
417
+
418
+ p .SetCapacity (4 )
419
+
394
420
time .Sleep (10 * time .Millisecond )
395
421
396
422
// This should not hang
397
423
for i := 0 ; i < 3 ; i ++ {
398
424
p .put (resources [i ], resources [i ].generation )
399
425
}
400
- <- done
426
+ wg . Wait ()
401
427
402
428
assert .Panics (t , func () {
403
- _ = p .SetCapacity (ctx , - 1 )
429
+ p .SetCapacity (- 1 )
404
430
})
405
431
406
432
assert .EqualValues (t , 4 , p .Capacity ())
0 commit comments