Skip to content

Commit bf13b78

Browse files
committed
fix(tests): improve database locking test by removing busy wait and adding delay for lock establishment
1 parent 92f43a6 commit bf13b78

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

test/multi-process.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ describe("Multi-Process Database Access", () => {
344344
// Use longer timeouts on slower CI platforms
345345
const multiplier = getTimingMultiplier();
346346
const lockHoldTime = 1000 * multiplier;
347-
const writerDelay = 200 * multiplier;
348347
const setupDb = new DatabaseSync(dbPath);
349348
setupDb.exec(`
350349
CREATE TABLE lock_test (
@@ -381,19 +380,15 @@ describe("Multi-Process Database Access", () => {
381380
const writerScriptTemplate = `
382381
const { DatabaseSync } = require(${JSON.stringify(path.resolve(getDirname(), "../dist/index.cjs"))});
383382
384-
// Wait a bit to ensure lock is held
385-
const delay = ${writerDelay};
386-
const start = Date.now();
387-
while (Date.now() - start < delay) {
388-
// Busy wait
389-
}
390-
391383
const db = new DatabaseSync(\${dbPath}, { timeout: 100 }); // Short timeout
392384
try {
385+
// Try to start a transaction immediately - this should fail if lock is held
386+
db.exec("BEGIN IMMEDIATE");
393387
db.exec("UPDATE lock_test SET value = 111 WHERE id = 1");
388+
db.exec("COMMIT");
394389
console.log("WRITE_SUCCESS");
395390
} catch (error) {
396-
if (error.message.includes("locked") || error.message.includes("busy")) {
391+
if (error.message.includes("locked") || error.message.includes("busy") || error.message.includes("SQLITE_BUSY")) {
397392
console.log("DATABASE_LOCKED");
398393
} else {
399394
console.error("UNEXPECTED_ERROR:", error.message);
@@ -429,6 +424,9 @@ describe("Multi-Process Database Access", () => {
429424
}, 10);
430425
});
431426

427+
// Add a small delay to ensure lock is fully established
428+
await new Promise((resolve) => setTimeout(resolve, 100 * multiplier));
429+
432430
// Try to write while locked
433431
const writerResult = await execFile("node", [
434432
"-e",

0 commit comments

Comments
 (0)