Skip to content

Commit da485c0

Browse files
test: add signal test
Refs: metarhia#25
1 parent 33801eb commit da485c0

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const tests = [
88
'deadlock',
99
'recursive-deadlock',
1010
'thread-main',
11+
'signal',
1112
];
1213

1314
(async () => {

test/signal.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const assert = require('assert').strict;
4+
const { locks, AbortController } = require('..');
5+
6+
const sleep = msec =>
7+
new Promise(resolve => {
8+
setTimeout(() => {
9+
resolve();
10+
}, msec);
11+
});
12+
13+
module.exports = async () => {
14+
const startTs = Date.now();
15+
const abortController = new AbortController();
16+
const options = { mode: 'exclusive', signal: abortController.signal };
17+
let hasErrorOccurred = false;
18+
setTimeout(() => abortController.abort(), 500);
19+
20+
await locks
21+
.request('A', options, async () => {
22+
await sleep(1000);
23+
})
24+
.catch(() => {
25+
hasErrorOccurred = true;
26+
});
27+
28+
const endTs = Date.now();
29+
assert.strictEqual(endTs - startTs < 1000, true, 'Lock should be aborted');
30+
assert.strictEqual(hasErrorOccurred, true, 'Error should occur');
31+
};

0 commit comments

Comments
 (0)