|
1 | 1 | const query = require("../"); |
2 | 2 | const { describe, it, before, after, beforeEach, afterEach } = require('node:test'); |
3 | 3 | const assert = require('node:assert/strict'); |
| 4 | +const { execFileSync } = require('node:child_process'); |
4 | 5 |
|
5 | 6 | describe("Query Scanning", () => { |
6 | 7 | before(async () => { |
@@ -364,6 +365,34 @@ describe("scan input limits", () => { |
364 | 365 | assert.equal(query.scanSync("SELECT 1;\n".repeat(1000000)).tokens.length, 3000000); |
365 | 366 | assert.throws(() => query.scanSync("SELECT 1;\n".repeat(2000000)), /out of WASM memory/); |
366 | 367 | }); |
| 368 | + |
| 369 | + // About 50 MB (18.6M tokens) used to make libpg_query write the token protobuf |
| 370 | + // through a NULL pointer, abort, and break the next sync call. Runs in a child |
| 371 | + // process because the abort corrupts the instance. |
| 372 | + it("throws out of WASM memory at 50 MB, then sync and async calls work", () => { |
| 373 | + const script = `const query = require(${JSON.stringify(require.resolve("../"))}); |
| 374 | + (async () => { |
| 375 | + await query.parse("SELECT 1"); |
| 376 | + let message = "no error"; |
| 377 | + try { |
| 378 | + query.scanSync("SELECT a, b FROM t WHERE c = 1;\\n".repeat(1690000)); |
| 379 | + } catch (error) { |
| 380 | + message = error.message; |
| 381 | + } |
| 382 | + const sync = query.scanSync("SELECT 1").tokens.length; |
| 383 | + const async = (await query.scan("SELECT 1")).tokens.length; |
| 384 | + process.stdout.write("\\nresult=" + JSON.stringify({ message, sync, async })); |
| 385 | + })();`; |
| 386 | + const output = execFileSync(process.execPath, ["-e", script], { |
| 387 | + encoding: "utf8", |
| 388 | + stdio: ["ignore", "pipe", "ignore"], |
| 389 | + maxBuffer: 256 * 1024 * 1024, |
| 390 | + }); |
| 391 | + const result = JSON.parse(output.match(/^result=(.*)$/m)[1]); |
| 392 | + assert.match(result.message, /out of WASM memory/); |
| 393 | + assert.equal(result.sync, 2); |
| 394 | + assert.equal(result.async, 2); |
| 395 | + }); |
367 | 396 | }); |
368 | 397 |
|
369 | 398 | describe("scan control characters", () => { |
|
0 commit comments