Skip to content

Commit 28e1853

Browse files
committed
test(wasm): a failed reload of the fresh instance is retried by the next call
1 parent 747a485 commit 28e1853

6 files changed

Lines changed: 138 additions & 0 deletions

File tree

‎versions/13/test/wasm-recovery.test.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,27 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => {
4040
assert.equal(process.exitCode, exitCode);
4141
assert.equal((await parse('SELECT 1')).stmts.length, 1);
4242
});
43+
44+
it('retries the load when loading a fresh instance fails', async () => {
45+
const indexPath = require.resolve('../wasm/index.cjs');
46+
const factory = require.cache[require.resolve('../wasm/libpg-query.js')];
47+
const shared = require.cache[indexPath];
48+
const realFactory = factory.exports;
49+
let loads = 0;
50+
factory.exports = (...args) =>
51+
++loads === 2 ? Promise.reject(new Error('simulated load failure')) : realFactory(...args);
52+
delete require.cache[indexPath];
53+
let isolated;
54+
try {
55+
isolated = require(indexPath);
56+
} finally {
57+
factory.exports = realFactory;
58+
require.cache[indexPath] = shared;
59+
}
60+
await isolated.loadModule();
61+
await assert.rejects(isolated.parse('SELECT 1' + '+1'.repeat(100000)));
62+
await new Promise((resolve) => setImmediate(resolve));
63+
assert.equal((await isolated.parse('SELECT 1')).stmts.length, 1);
64+
assert.equal(loads, 3);
65+
});
4366
});

‎versions/14/test/wasm-recovery.test.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,27 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => {
4040
assert.equal(process.exitCode, exitCode);
4141
assert.equal((await parse('SELECT 1')).stmts.length, 1);
4242
});
43+
44+
it('retries the load when loading a fresh instance fails', async () => {
45+
const indexPath = require.resolve('../wasm/index.cjs');
46+
const factory = require.cache[require.resolve('../wasm/libpg-query.js')];
47+
const shared = require.cache[indexPath];
48+
const realFactory = factory.exports;
49+
let loads = 0;
50+
factory.exports = (...args) =>
51+
++loads === 2 ? Promise.reject(new Error('simulated load failure')) : realFactory(...args);
52+
delete require.cache[indexPath];
53+
let isolated;
54+
try {
55+
isolated = require(indexPath);
56+
} finally {
57+
factory.exports = realFactory;
58+
require.cache[indexPath] = shared;
59+
}
60+
await isolated.loadModule();
61+
await assert.rejects(isolated.parse('SELECT 1' + '+1'.repeat(100000)));
62+
await new Promise((resolve) => setImmediate(resolve));
63+
assert.equal((await isolated.parse('SELECT 1')).stmts.length, 1);
64+
assert.equal(loads, 3);
65+
});
4366
});

‎versions/15/test/wasm-recovery.test.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,27 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => {
4040
assert.equal(process.exitCode, exitCode);
4141
assert.equal((await parse('SELECT 1')).stmts.length, 1);
4242
});
43+
44+
it('retries the load when loading a fresh instance fails', async () => {
45+
const indexPath = require.resolve('../wasm/index.cjs');
46+
const factory = require.cache[require.resolve('../wasm/libpg-query.js')];
47+
const shared = require.cache[indexPath];
48+
const realFactory = factory.exports;
49+
let loads = 0;
50+
factory.exports = (...args) =>
51+
++loads === 2 ? Promise.reject(new Error('simulated load failure')) : realFactory(...args);
52+
delete require.cache[indexPath];
53+
let isolated;
54+
try {
55+
isolated = require(indexPath);
56+
} finally {
57+
factory.exports = realFactory;
58+
require.cache[indexPath] = shared;
59+
}
60+
await isolated.loadModule();
61+
await assert.rejects(isolated.parse('SELECT 1' + '+1'.repeat(100000)));
62+
await new Promise((resolve) => setImmediate(resolve));
63+
assert.equal((await isolated.parse('SELECT 1')).stmts.length, 1);
64+
assert.equal(loads, 3);
65+
});
4366
});

‎versions/16/test/wasm-recovery.test.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,27 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => {
4040
assert.equal(process.exitCode, exitCode);
4141
assert.equal((await parse('SELECT 1')).stmts.length, 1);
4242
});
43+
44+
it('retries the load when loading a fresh instance fails', async () => {
45+
const indexPath = require.resolve('../wasm/index.cjs');
46+
const factory = require.cache[require.resolve('../wasm/libpg-query.js')];
47+
const shared = require.cache[indexPath];
48+
const realFactory = factory.exports;
49+
let loads = 0;
50+
factory.exports = (...args) =>
51+
++loads === 2 ? Promise.reject(new Error('simulated load failure')) : realFactory(...args);
52+
delete require.cache[indexPath];
53+
let isolated;
54+
try {
55+
isolated = require(indexPath);
56+
} finally {
57+
factory.exports = realFactory;
58+
require.cache[indexPath] = shared;
59+
}
60+
await isolated.loadModule();
61+
await assert.rejects(isolated.parse('SELECT 1' + '+1'.repeat(100000)));
62+
await new Promise((resolve) => setImmediate(resolve));
63+
assert.equal((await isolated.parse('SELECT 1')).stmts.length, 1);
64+
assert.equal(loads, 3);
65+
});
4366
});

‎versions/17/test/wasm-recovery.test.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,27 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => {
4040
assert.equal(process.exitCode, exitCode);
4141
assert.equal((await parse('SELECT 1')).stmts.length, 1);
4242
});
43+
44+
it('retries the load when loading a fresh instance fails', async () => {
45+
const indexPath = require.resolve('../wasm/index.cjs');
46+
const factory = require.cache[require.resolve('../wasm/libpg-query.js')];
47+
const shared = require.cache[indexPath];
48+
const realFactory = factory.exports;
49+
let loads = 0;
50+
factory.exports = (...args) =>
51+
++loads === 2 ? Promise.reject(new Error('simulated load failure')) : realFactory(...args);
52+
delete require.cache[indexPath];
53+
let isolated;
54+
try {
55+
isolated = require(indexPath);
56+
} finally {
57+
factory.exports = realFactory;
58+
require.cache[indexPath] = shared;
59+
}
60+
await isolated.loadModule();
61+
await assert.rejects(isolated.parse('SELECT 1' + '+1'.repeat(100000)));
62+
await new Promise((resolve) => setImmediate(resolve));
63+
assert.equal((await isolated.parse('SELECT 1')).stmts.length, 1);
64+
assert.equal(loads, 3);
65+
});
4366
});

‎versions/18/test/wasm-recovery.test.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,27 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => {
4040
assert.equal(process.exitCode, exitCode);
4141
assert.equal((await parse('SELECT 1')).stmts.length, 1);
4242
});
43+
44+
it('retries the load when loading a fresh instance fails', async () => {
45+
const indexPath = require.resolve('../wasm/index.cjs');
46+
const factory = require.cache[require.resolve('../wasm/libpg-query.js')];
47+
const shared = require.cache[indexPath];
48+
const realFactory = factory.exports;
49+
let loads = 0;
50+
factory.exports = (...args) =>
51+
++loads === 2 ? Promise.reject(new Error('simulated load failure')) : realFactory(...args);
52+
delete require.cache[indexPath];
53+
let isolated;
54+
try {
55+
isolated = require(indexPath);
56+
} finally {
57+
factory.exports = realFactory;
58+
require.cache[indexPath] = shared;
59+
}
60+
await isolated.loadModule();
61+
await assert.rejects(isolated.parse('SELECT 1' + '+1'.repeat(100000)));
62+
await new Promise((resolve) => setImmediate(resolve));
63+
assert.equal((await isolated.parse('SELECT 1')).stmts.length, 1);
64+
assert.equal(loads, 3);
65+
});
4366
});

0 commit comments

Comments
 (0)