Skip to content

Commit d1f15c3

Browse files
committed
pending result tests
1 parent 5f19243 commit d1f15c3

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

bindings/test/pending.test.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import duckdb from '@duckdb/node-bindings';
2+
import { expect, suite, test } from 'vitest';
3+
import { expectResult } from './utils/expectResult';
4+
import { BIGINT, INTEGER } from './utils/expectedLogicalTypes';
5+
import { data } from './utils/expectedVectors';
6+
import { sleep } from './utils/sleep';
7+
import { withConnection } from './utils/withConnection';
8+
9+
suite('pending', () => {
10+
test('execute', async () => {
11+
await withConnection(async (connection) => {
12+
const prepared = await duckdb.prepare(connection, 'select 11 as a');
13+
try {
14+
const pending = duckdb.pending_prepared(prepared);
15+
try {
16+
const result = await duckdb.execute_pending(pending);
17+
try {
18+
await expectResult(result, {
19+
columns: [
20+
{ name: 'a', logicalType: INTEGER },
21+
],
22+
chunks: [
23+
{ rowCount: 1, vectors: [data(4, [true], [11])]},
24+
],
25+
});
26+
} finally {
27+
duckdb.destroy_result(result);
28+
}
29+
} finally {
30+
duckdb.destroy_pending(pending);
31+
}
32+
} finally {
33+
duckdb.destroy_prepare(prepared);
34+
}
35+
});
36+
});
37+
test('tasks', async () => {
38+
await withConnection(async (connection) => {
39+
const prepared = await duckdb.prepare(connection, 'select count(*) as count from range(10_000)');
40+
try {
41+
const pending = duckdb.pending_prepared(prepared);
42+
try {
43+
let pending_state = duckdb.pending_execute_check_state(pending);
44+
let taskCount = 0;
45+
while (!duckdb.pending_execution_is_finished(pending_state)) {
46+
pending_state = duckdb.pending_execute_task(pending);
47+
taskCount++;
48+
await sleep(0); // yield to allow progress
49+
}
50+
expect(taskCount).toBe(2);
51+
const result = await duckdb.execute_pending(pending);
52+
try {
53+
await expectResult(result, {
54+
columns: [
55+
{ name: 'count', logicalType: BIGINT },
56+
],
57+
chunks: [
58+
{ rowCount: 1, vectors: [data(8, [true], [10_000n])]},
59+
],
60+
});
61+
} finally {
62+
duckdb.destroy_result(result);
63+
}
64+
} finally {
65+
duckdb.destroy_pending(pending);
66+
}
67+
} finally {
68+
duckdb.destroy_prepare(prepared);
69+
}
70+
});
71+
});
72+
test('interrupt', async () => {
73+
await withConnection(async (connection) => {
74+
const prepared = await duckdb.prepare(connection, 'select count(*) as count from range(10_000)');
75+
try {
76+
const pending = duckdb.pending_prepared(prepared);
77+
try {
78+
duckdb.interrupt(connection);
79+
await sleep(0);
80+
81+
const pending_state = duckdb.pending_execute_task(pending);
82+
expect(duckdb.pending_execution_is_finished(pending_state)).toBe(true);
83+
expect(pending_state).toBe(duckdb.PendingState.ERROR);
84+
expect(duckdb.pending_error(pending)).toBe('INTERRUPT Error: Interrupted!');
85+
} finally {
86+
duckdb.destroy_pending(pending);
87+
}
88+
} finally {
89+
duckdb.destroy_prepare(prepared);
90+
}
91+
});
92+
});
93+
// TODO: query progress?
94+
});

bindings/test/query.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,4 @@ suite('query', () => {
276276
}
277277
});
278278
});
279-
// TODO: interrupt
280-
// TODO: query_progress
281279
});

bindings/test/utils/sleep.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export async function sleep(ms: number): Promise<void> {
2+
return new Promise((resolve) => {
3+
setTimeout(resolve, ms);
4+
});
5+
}

0 commit comments

Comments
 (0)