forked from metarhia/web-locks
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ const tests = [ | |
'deadlock', | ||
'recursive-deadlock', | ||
'thread-main', | ||
'signal', | ||
]; | ||
|
||
(async () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'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'); | ||
}; |