Skip to content

Commit cb52978

Browse files
🧪 add test for query timeout in wasm database engine (#171)
The `executeQuery` method in `src/core/sqlite-db.ts` contains logic to throw an error if row iteration (`stmt.step()`) exceeds the configured `queryTimeout`. However, this specific code path lacked unit test coverage. This commit adds a test case to `tests/unit/sqlite-db.test.ts` within the `WasmDatabaseEngine` suite. The test simulates a slow query by mocking `Date.now()` globally inside a `try...finally` block, ensuring it artificially increments past the 100ms timeout configured on the test engine instance. It accurately asserts that the `assert.rejects` catches the specific timeout error message: `Query execution timed out after 100ms`. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 2e88f3c commit cb52978

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

tests/unit/sqlite-db.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,50 @@ describe('WasmDatabaseEngine', () => {
127127
assert.strictEqual(countYellow, 1);
128128
});
129129
});
130+
131+
describe('executeQuery', () => {
132+
it('should timeout long running queries', async () => {
133+
// Create a specific engine instance with a short timeout
134+
const result = await createDatabaseEngine({
135+
content: null,
136+
maxSize: 0,
137+
readOnlyMode: false,
138+
queryTimeout: 100 // 100ms timeout
139+
});
140+
const timeoutEngine = result.operations;
141+
142+
await timeoutEngine.executeQuery("CREATE TABLE timeout_test (id INTEGER PRIMARY KEY, value TEXT)");
143+
await timeoutEngine.insertRow('timeout_test', { id: 1, value: 'test1' });
144+
await timeoutEngine.insertRow('timeout_test', { id: 2, value: 'test2' });
145+
146+
const originalDateNow = Date.now;
147+
let callCount = 0;
148+
149+
try {
150+
Date.now = () => {
151+
// First call establishes startTime, subsequent calls simulate elapsed time
152+
if (callCount === 0) {
153+
callCount++;
154+
return 1000;
155+
}
156+
callCount++;
157+
// Return a time far in the future to trigger timeout
158+
return 1000 + 200;
159+
};
160+
161+
// This query will hit the while(stmt.step()) loop
162+
await assert.rejects(
163+
async () => {
164+
await timeoutEngine.executeQuery("SELECT * FROM timeout_test");
165+
},
166+
(err: any) => {
167+
assert.strictEqual(err.message, "Query failed: Query execution timed out after 100ms");
168+
return true;
169+
}
170+
);
171+
} finally {
172+
Date.now = originalDateNow;
173+
}
174+
});
175+
});
130176
});

0 commit comments

Comments
 (0)