@@ -171,7 +171,7 @@ local function test_mysql_int64(t, p)
171
171
end
172
172
173
173
local function test_connection_pool (test , pool )
174
- test :plan (6 )
174
+ test :plan (11 )
175
175
176
176
-- {{{ Case group: all connections are consumed initially.
177
177
@@ -302,13 +302,13 @@ local function test_connection_pool(test, pool)
302
302
303
303
-- Put the connection back and verify that the pool is full.
304
304
pool :put (conn )
305
- test :ok (pool .queue :is_full (), ' a broken connection was given back' )
305
+ test :ok (pool .queue :is_full (), ' a connection was given back' )
306
306
end )
307
307
308
308
-- Case: the same, but loss and collect a connection after
309
309
-- put.
310
310
test :test (' get, put and loss a connection' , function (test )
311
- test :plan (2 )
311
+ test :plan (1 )
312
312
313
313
assert (pool .size >= 1 , ' test case precondition fails' )
314
314
@@ -321,14 +321,7 @@ local function test_connection_pool(test, pool)
321
321
collectgarbage (' collect' )
322
322
323
323
-- Verify that the pool is full.
324
- test :ok (pool .queue :is_full (), ' a broken connection was given back' )
325
-
326
- -- Verify the pool will not be populated by a connection's
327
- -- GC callback. Otherwise :put() will hang.
328
- local item = pool .queue :get ()
329
- pool .queue :put (item )
330
- test :ok (true , ' GC callback does not put back a connection that was ' ..
331
- ' put manually' )
324
+ test :ok (pool .queue :is_full (), ' a connection was given back' )
332
325
end )
333
326
334
327
-- Case: get a connection, broke it and put back.
@@ -350,7 +343,7 @@ local function test_connection_pool(test, pool)
350
343
-- Case: the same, but loss and collect a connection after
351
344
-- put.
352
345
test :test (' get, broke, put and loss a connection' , function (test )
353
- test :plan (3 )
346
+ test :plan (2 )
354
347
355
348
assert (pool .size >= 1 , ' test case precondition fails' )
356
349
@@ -366,24 +359,10 @@ local function test_connection_pool(test, pool)
366
359
367
360
-- Verify that the pool is full
368
361
test :ok (pool .queue :is_full (), ' a broken connection was given back' )
369
-
370
- -- Verify the pool will not be populated by a connection's
371
- -- GC callback. Otherwise :put() will hang.
372
- local item = pool .queue :get ()
373
- pool .queue :put (item )
374
- test :ok (true , ' GC callback does not put back a connection that was ' ..
375
- ' put manually' )
376
362
end )
377
363
378
- --[[
379
-
380
- -- It is unclear for now whether putting of closed connection
381
- -- should be allowed. The second case, where GC collects lost
382
- -- connection after :put(), does not work at the moment. See
383
- -- gh-33.
384
-
385
- -- Case: get a connection, close it and put back.
386
- test:test('get, close and put a connection', function(test)
364
+ -- Case: get a connection and close it.
365
+ test :test (' get and close a connection' , function (test )
387
366
test :plan (1 )
388
367
389
368
assert (pool .size >= 1 , ' test case precondition fails' )
@@ -392,39 +371,78 @@ local function test_connection_pool(test, pool)
392
371
local conn = pool :get ()
393
372
conn :close ()
394
373
395
- -- Put a connection back and verify that the pool is full.
396
- pool:put(conn)
397
- test:ok(pool.queue:is_full(), 'a broken connection was given back')
374
+ test :ok (pool .queue :is_full (), ' a connection was given back' )
398
375
end )
399
376
400
377
-- Case: the same, but loss and collect a connection after
401
- -- put .
402
- test:test('get, close, put and loss a connection', function(test)
403
- test:plan(2 )
378
+ -- close .
379
+ test :test (' get, close and loss a connection' , function (test )
380
+ test :plan (1 )
404
381
405
382
assert (pool .size >= 1 , ' test case precondition fails' )
406
383
407
384
-- Get a connection and close it.
408
385
local conn = pool :get ()
409
386
conn :close ()
410
387
411
- -- Put the connection back, loss it and trigger GC.
412
- pool:put(conn)
413
- conn = nil
388
+ conn = nil -- luacheck: no unused
414
389
collectgarbage (' collect' )
415
390
416
391
-- Verify that the pool is full
417
392
test :ok (pool .queue :is_full (), ' a broken connection was given back' )
393
+ end )
394
+
395
+ -- Case: get a connection, close and put it back.
396
+ test :test (' get, close and put a connection' , function (test )
397
+ test :plan (2 )
418
398
419
- -- Verify the pool will not be populated by a connection's
420
- -- GC callback. Otherwise :put() will hang.
421
- local item = pool.queue:get()
422
- pool.queue:put(item)
423
- test:ok(true, 'GC callback does not put back a connection that was ' ..
424
- 'put manually')
399
+ assert (pool .size >= 1 , ' test case precondition fails' )
400
+
401
+ -- Get a connection.
402
+ local conn = pool :get ()
403
+
404
+ conn :close ()
405
+ test :ok (pool .queue :is_full (), ' a connection was given back' )
406
+
407
+ -- Put must throw an error.
408
+ local res = pcall (pool .put , pool , conn )
409
+ test :ok (not res , ' an error is thrown on "put" after "close"' )
410
+ end )
411
+
412
+ -- Case: close the same connection twice.
413
+ test :test (' close a connection twice' , function (test )
414
+ test :plan (2 )
415
+
416
+ assert (pool .size >= 1 , ' test case precondition fails' )
417
+
418
+ local conn = pool :get ()
419
+ conn :close ()
420
+ test :ok (pool .queue :is_full (), ' a connection was given back' )
421
+
422
+ local res = pcall (conn .close , conn )
423
+ test :ok (not res , ' an error is thrown on double "close"' )
425
424
end )
426
425
427
- --]]
426
+ -- Case: put the same connection twice.
427
+ test :test (' put a connection twice' , function (test )
428
+ test :plan (3 )
429
+
430
+ assert (pool .size >= 2 , ' test case precondition fails' )
431
+
432
+ local conn_1 = pool :get ()
433
+ local conn_2 = pool :get ()
434
+ pool :put (conn_1 )
435
+
436
+ test :ok (not pool .queue :is_full (),
437
+ ' the same connection has not been "put" twice' )
438
+
439
+ local res = pcall (pool .put , pool , conn_1 )
440
+ test :ok (not res , ' an error is thrown on double "put"' )
441
+
442
+ pool :put (conn_2 )
443
+ test :ok (pool .queue :is_full (),
444
+ ' all connections were returned to the pool' )
445
+ end )
428
446
429
447
assert (pool .queue :is_full (), ' case group postcondition fails' )
430
448
@@ -469,7 +487,6 @@ test:plan(7)
469
487
test :test (' connection old api' , test_old_api , conn )
470
488
local pool_conn = p :get ()
471
489
test :test (' connection old api via pool' , test_old_api , pool_conn )
472
- p :put (pool_conn )
473
490
test :test (' garbage collection' , test_gc , p )
474
491
test :test (' concurrent connections' , test_conn_concurrent , p )
475
492
test :test (' int64' , test_mysql_int64 , p )
0 commit comments