Skip to content

Commit bc3b81c

Browse files
test: improve detection of un-closed sqlite handles
Manually invoke the garbage collector after each test to trigger exceptions on un-closed databases and statements. Otherwise, the test suite may pass even if there are un-closed handles, or crash without a clear error message depending on the compiler and build settings.
1 parent a643bcb commit bc3b81c

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

test/parallel/test-sqlite-database-async.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
2+
// --expose-gc
23
const { skipIfSQLiteMissing } = require('../common');
34
skipIfSQLiteMissing();
45
const tmpdir = require('../common/tmpdir');
56
const { existsSync } = require('node:fs');
67
const { join } = require('node:path');
78
const { Database, Statement } = require('node:sqlite');
8-
const { suite, test } = require('node:test');
9+
const { afterEach, suite, test } = require('node:test');
910

1011
tmpdir.refresh();
1112

@@ -14,6 +15,12 @@ function nextDb() {
1415
return join(tmpdir.path, `database-${cnt++}.db`);
1516
}
1617

18+
if (typeof global.gc === 'function') {
19+
afterEach(() => {
20+
global.gc(); // trigger exceptions on non-closed databases/statements
21+
});
22+
}
23+
1724
suite('Database() constructor', { timeout: 1000 }, () => {
1825
test('throws if called without new', (t) => {
1926
t.assert.throws(() => {

test/parallel/test-sqlite-statement-async.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
// Flags: --expose-gc
21
'use strict';
2+
// --expose-gc
33
const { skipIfSQLiteMissing } = require('../common');
44
skipIfSQLiteMissing();
55
const tmpdir = require('../common/tmpdir');
66
const { join } = require('node:path');
77
const { Database, Statement } = require('node:sqlite');
8-
const { suite, test } = require('node:test');
9-
let cnt = 0;
8+
const { afterEach, suite, test } = require('node:test');
109

1110
tmpdir.refresh();
1211

12+
let cnt = 0;
1313
function nextDb() {
1414
return join(tmpdir.path, `database-${cnt++}.db`);
1515
}
1616

17+
if (typeof global.gc === 'function') {
18+
afterEach(() => {
19+
global.gc(); // trigger exceptions on non-closed databases/statements
20+
});
21+
}
22+
1723
suite('Statement() constructor', () => {
1824
test('Statement cannot be constructed directly', (t) => {
1925
t.assert.throws(() => {
@@ -204,7 +210,7 @@ suite('Statement.prototype.run()', () => {
204210

205211
test('SQLite throws when trying to bind too many parameters', async (t) => {
206212
const db = new Database(nextDb());
207-
t.after(() => { db.close(); });
213+
t.after(async () => { await db.close(); });
208214
const setup = await db.exec(
209215
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
210216
);
@@ -240,6 +246,7 @@ suite('Statement.prototype.run()', () => {
240246

241247
test('returns correct metadata when using RETURNING', async (t) => {
242248
const db = new Database(':memory:');
249+
t.after(async () => { await db.close(); });
243250
const setup = await db.exec(
244251
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER NOT NULL) STRICT;'
245252
);
@@ -408,8 +415,8 @@ suite.skip('Statement.prototype.setReturnArrays()', () => {
408415
});
409416
});
410417

411-
suite.skip('Statement.prototype.get() with array output', () => {
412-
test('returns array row when setReturnArrays is true', async (t) => {
418+
suite('Statement.prototype.get() with array output', () => {
419+
test.skip('returns array row when setReturnArrays is true', async (t) => {
413420
const db = new Database(nextDb());
414421
t.after(() => { db.close(); });
415422
const setup = await db.exec(`
@@ -431,16 +438,17 @@ suite.skip('Statement.prototype.get() with array output', () => {
431438
test('returns array rows with BigInts when both flags are set', async (t) => {
432439
const expected = [1n, 9007199254740992n];
433440
const db = new Database(nextDb());
434-
t.after(() => { db.close(); });
441+
t.after(async () => { await db.close(); });
435442
const setup = await db.exec(`
436443
CREATE TABLE big_data(id INTEGER, big_num INTEGER);
437444
INSERT INTO big_data VALUES (1, 9007199254740992);
438445
`);
439446
t.assert.strictEqual(setup, undefined);
440447

441-
const query = db.prepare('SELECT id, big_num FROM big_data');
442-
query.setReturnArrays(true);
443-
query.setReadBigInts(true);
448+
using query = await db.prepare('SELECT id, big_num FROM big_data', {
449+
returnArrays: true,
450+
readBigInts: true
451+
});
444452

445453
const row = await query.get();
446454
t.assert.deepStrictEqual(row, expected);

0 commit comments

Comments
 (0)