Skip to content

Commit

Permalink
more thorough stress test (#72)
Browse files Browse the repository at this point in the history
* more thorough stress test

* full throttle
  • Loading branch information
mmomtchev authored Jan 19, 2023
1 parent 2d9058b commit 6384504
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
17 changes: 15 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@types/node": "^18.11.0",
"@typescript-eslint/eslint-plugin": "^5.40.1",
"@typescript-eslint/parser": "^5.40.1",
"async-await-queue": "^1.2.1",
"benny": "^3.7.1",
"chai": "^4.3.6",
"chai-spies": "^1.0.0",
Expand All @@ -93,4 +94,4 @@
"@mmomtchev/node-pre-gyp-github": "^2.0.6",
"node-addon-api": "^5.0.0"
}
}
}
2 changes: 1 addition & 1 deletion test/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('callAsync', () => {
it('basic test', (done) => {
const q = spawnWorker('lambda x: x + 4.2');
q.then((r) => {
assert.strictEqual(r, 8.4);
assert.closeTo(r as number, 4.2 * 102, 0.1);
done();
}).catch((err) => done(err));
});
Expand Down
21 changes: 17 additions & 4 deletions test/worker-stress-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { Worker } = require('worker_threads');
const path = require('path');
const Queue = require('async-await-queue');

const queue = new Queue(24, 0);

// Make sure the dynamic library is loaded from the main thread
// https://github.com/mmomtchev/pymport/issues/69
Expand All @@ -26,10 +29,20 @@ async function main() {
console.log('start');
// eslint-disable-next-line no-constant-condition
while (true) {
const r = await spawnWorker('lambda x: x + 4.2');
if (r !== 8.4) throw new Error('r=' + r);
i++;
if (i % 10 == 0) console.log('deca', i / 10);
const me = Symbol();
queue.wait(me)
.then(() => spawnWorker('lambda x: x + 4.2'))
.then((r) => {
if (Math.abs(r - 4.2 * 102) > 0.1) throw new Error('r=' + r);
i++;
if (i % 10 == 0) console.log('deca', i / 10);
})
.finally(() => queue.end(me));
const { waiting } = queue.stat();
if (waiting > 100) {
const throttle = Symbol();
await queue.wait(throttle, -1).finally(() => queue.end(throttle));
}
}
}

Expand Down
10 changes: 7 additions & 3 deletions test/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ if (!parentPort) throw new Error('This worker must be spawned from another test'

const script = workerData;
const fn = pyval(script);
fn.callAsync(4.2).then((result) => {
parentPort.postMessage(result.toJS());
});

let q = fn.callAsync(4.2);
for (let i = 0; i < 100; i++) {
q = q.then((r) => fn.callAsync(r));
}

q.then((r) => parentPort.postMessage(r.toJS()));

0 comments on commit 6384504

Please sign in to comment.