Skip to content

Commit cababcf

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

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

test/mysql.test.lua

Lines changed: 38 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,41 @@ 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+
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+
484519
local test = tap.test('mysql connector')
485-
test:plan(7)
520+
test:plan(8)
486521

487522
test:test('connection old api', test_old_api, conn)
488523
local pool_conn = p:get()
@@ -492,6 +527,8 @@ test:test('concurrent connections', test_conn_concurrent, p)
492527
test:test('int64', test_mysql_int64, p)
493528
test:test('connection pool', test_connection_pool, p)
494529
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)
495532
p:close()
496533

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

0 commit comments

Comments
 (0)