File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ const tests = [
88 'deadlock' ,
99 'recursive-deadlock' ,
1010 'thread-main' ,
11+ 'signal' ,
1112] ;
1213
1314( async ( ) => {
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments