@@ -7,6 +7,8 @@ local mysql = require('mysql')
7
7
local json = require (' json' )
8
8
local tap = require (' tap' )
9
9
local fiber = require (' fiber' )
10
+ local fio = require (' fio' )
11
+ local ffi = require (' ffi' )
10
12
11
13
local host , port , user , password , db = string.match (os.getenv (' MYSQL' ) or ' ' ,
12
14
" ([^:]*):([^:]*):([^:]*):([^:]*):([^:]*)" )
@@ -481,8 +483,41 @@ local function test_connection_reset(test, pool)
481
483
assert (pool .queue :is_full (), ' test case postcondition fails' )
482
484
end
483
485
486
+ local function is_linux ()
487
+ local os_name
488
+ if jit and jit .os then
489
+ os_name = jit .os
490
+ end
491
+ os_name = (os_name ):lower ()
492
+ if os_name :match (' linux' ) then
493
+ return true
494
+ end
495
+ return false
496
+ end
497
+
498
+ local function test_underlying_conn_closed_during_gc (test )
499
+ test :plan (1 )
500
+ local fh = fio .open (' /dev/zero' , {' O_RDONLY' })
501
+ if fh == nil then error (err ) end
502
+ local handle = fh .fh
503
+ fh :close ()
504
+ local conn , err = mysql .connect ({ host = host , port = port , user = user ,
505
+ password = password , db = db })
506
+ if conn == nil then error (err ) end
507
+
508
+ -- Somehow we lost the connection handle.
509
+ conn = nil
510
+ collectgarbage ()
511
+ ffi .cdef ([[ int fcntl(int fd, int cmd, ...); ]] )
512
+ if is_linux () then
513
+ test :ok (ffi .C .fcntl (handle , 1 ) == - 1 , ' descriptor is closed' )
514
+ else
515
+ test :ok (true , ' OS is not linux' )
516
+ end
517
+ end
518
+
484
519
local test = tap .test (' mysql connector' )
485
- test :plan (7 )
520
+ test :plan (8 )
486
521
487
522
test :test (' connection old api' , test_old_api , conn )
488
523
local pool_conn = p :get ()
@@ -492,6 +527,8 @@ test:test('concurrent connections', test_conn_concurrent, p)
492
527
test :test (' int64' , test_mysql_int64 , p )
493
528
test :test (' connection pool' , test_connection_pool , p )
494
529
test :test (' connection reset' , test_connection_reset , p )
530
+ test :test (' test_underlying_conn_closed_during_gc' ,
531
+ test_underlying_conn_closed_during_gc , p )
495
532
p :close ()
496
533
497
534
os.exit (test :check () and 0 or 1 )
0 commit comments