diff --git a/test.js b/test.js index 8f48423..08768dc 100644 --- a/test.js +++ b/test.js @@ -8,6 +8,7 @@ const tests = [ 'deadlock', 'recursive-deadlock', 'thread-main', + 'options', ]; (async () => { diff --git a/test/options.js b/test/options.js new file mode 100644 index 0000000..30528a6 --- /dev/null +++ b/test/options.js @@ -0,0 +1,38 @@ +'use strict'; + +const assert = require('assert').strict; + +const { locks, AbortController } = require('..'); + +const TIMEOUT = 100; + +module.exports = async () => { + const abortController = new AbortController(); + await locks.request('DEFAULT', async lock => { + assert.strictEqual(lock.mode, 'exclusive'); + }); + + await locks.request('EXCLUSIVE', { mode: 'exclusive' }, async lock => { + assert.strictEqual(lock.mode, 'exclusive'); + }); + + await locks.request('SHARED', { mode: 'shared' }, async lock => { + assert.strictEqual(lock.mode, 'shared'); + }); + + await locks.request( + 'WITH_ABORT', + () => + new Promise(resolve => { + setTimeout(() => resolve(), TIMEOUT); + }) + ); + + locks + .request('WITH_ABORT', { signal: abortController.signal }, async () => {}) + .catch(error => { + assert.strictEqual(error.message, 'The request was aborted'); + }); + + abortController.abort(); +};