Skip to content

Commit a0fcdf4

Browse files
committed
test: add test for check that underlying connection is closed
Closes #31
1 parent 6f785f7 commit a0fcdf4

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

test/mysql.test.lua

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ local mysql = require('mysql')
77
local json = require('json')
88
local tap = require('tap')
99
local fiber = require('fiber')
10+
local fio = require('fio')
11+
local ffi = require('ffi')
1012

1113
local host, port, user, password, db = string.match(os.getenv('MYSQL') or '',
1214
"([^:]*):([^:]*):([^:]*):([^:]*):([^:]*)")
@@ -481,8 +483,42 @@ local function test_connection_reset(test, pool)
481483
assert(pool.queue:is_full(), 'test case postcondition fails')
482484
end
483485

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+
if jit.os == 'Linux' then
501+
local fh = fio.open('/dev/zero', {'O_RDONLY'})
502+
if fh == nil then error(err) end
503+
local handle = fh.fh
504+
fh:close()
505+
local conn, err = mysql.connect({ host = host, port = port, user = user,
506+
password = password, db = db })
507+
if conn == nil then error(err) end
508+
509+
-- Somehow we lost the connection handle.
510+
conn = nil
511+
collectgarbage()
512+
ffi.cdef([[ int fcntl(int fd, int cmd, ...); ]])
513+
local F_GETFD = 1
514+
test:ok(ffi.C.fcntl(handle, F_GETFD) == -1, 'descriptor is closed')
515+
else
516+
test:skip(1, 'wrong OS')
517+
end
518+
end
519+
484520
local test = tap.test('mysql connector')
485-
test:plan(7)
521+
test:plan(8)
486522

487523
test:test('connection old api', test_old_api, conn)
488524
local pool_conn = p:get()
@@ -492,6 +528,8 @@ test:test('concurrent connections', test_conn_concurrent, p)
492528
test:test('int64', test_mysql_int64, p)
493529
test:test('connection pool', test_connection_pool, p)
494530
test:test('connection reset', test_connection_reset, p)
531+
test:test('test_underlying_conn_closed_during_gc',
532+
test_underlying_conn_closed_during_gc, p)
495533
p:close()
496534

497535
os.exit(test:check() and 0 or 1)

0 commit comments

Comments
 (0)