Skip to content

Commit

Permalink
test: add signal test
Browse files Browse the repository at this point in the history
Refs: #25
  • Loading branch information
anton-iskryzhytskyi committed Mar 20, 2020
1 parent 33801eb commit 0cdab79
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const tests = [
'deadlock',
'recursive-deadlock',
'thread-main',
'signal',
];

(async () => {
Expand Down
33 changes: 33 additions & 0 deletions test/signal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const assert = require('assert').strict;
const { locks, AbortController } = require('..');

const sleep = msec =>
new Promise(resolve => {
setTimeout(() => {
resolve();
}, msec);
});

module.exports = async () => {
const startTs = Date.now();

const abortController = new AbortController();
const options = { mode: 'exclusive', signal: abortController.signal };
let hasErrorOccurred = false;

setTimeout(() => abortController.abort(), 500);

await locks
.request('A', options, async () => {
await sleep(1000);
})
.catch(() => {
hasErrorOccurred = true;
});

const endTs = Date.now();
assert.strictEqual(endTs - startTs < 1000, true, 'Lock should be aborted');
assert.strictEqual(hasErrorOccurred, true, 'Error should occur');
};

0 comments on commit 0cdab79

Please sign in to comment.