async-disposal-during-throw-functions-and-scopes.js (2648B)
1 // |jit-test| skip-if: !getBuildConfiguration("explicit-resource-management"); --enable-explicit-resource-management 2 3 load(libdir + "asserts.js"); 4 5 class CustomError extends Error {} 6 7 { 8 const disposed = []; 9 let caught = false; 10 async function testDisposedWhenFunctionThrows() { 11 await using a = { 12 [Symbol.asyncDispose]: () => disposed.push("a") 13 }; 14 { 15 await using b = { 16 [Symbol.asyncDispose]: () => disposed.push("b") 17 }; 18 throw new CustomError(); 19 } 20 await using c = { 21 [Symbol.asyncDispose]: () => disposed.push("c") 22 }; 23 } 24 testDisposedWhenFunctionThrows().catch((e) => { 25 caught = true; 26 assertEq(e instanceof CustomError, true); 27 }); 28 drainJobQueue(); 29 assertArrayEq(disposed, ["b", "a"]); 30 assertEq(caught, true); 31 } 32 33 { 34 const disposed = []; 35 let caught = false; 36 async function testDisposedWhenAsyncDisposalInAwaitUsingMethodThrows() { 37 await using a = { 38 [Symbol.asyncDispose]: () => disposed.push("a") 39 }; 40 { 41 await using b = { 42 [Symbol.asyncDispose]: () => { 43 disposed.push("b"); 44 throw new CustomError(); 45 } 46 }; 47 } 48 } 49 testDisposedWhenAsyncDisposalInAwaitUsingMethodThrows().catch((e) => { 50 caught = true; 51 assertEq(e instanceof CustomError, true); 52 }); 53 drainJobQueue(); 54 assertArrayEq(disposed, ["b", "a"]); 55 assertEq(caught, true); 56 } 57 58 { 59 const disposed = []; 60 let caught = false; 61 async function testDisposedWhenSyncDisposalInAwaitUsingMethodThrows() { 62 await using a = { 63 [Symbol.asyncDispose]: () => disposed.push("a") 64 }; 65 { 66 await using b = { 67 [Symbol.dispose]: () => { 68 disposed.push("b"); 69 throw new CustomError(); 70 } 71 }; 72 } 73 } 74 testDisposedWhenSyncDisposalInAwaitUsingMethodThrows().catch((e) => { 75 caught = true; 76 assertEq(e instanceof CustomError, true); 77 }); 78 drainJobQueue(); 79 assertArrayEq(disposed, ["b", "a"]); 80 assertEq(caught, true); 81 } 82 83 { 84 const disposed = []; 85 async function testDisposedWhenSyncDisposalInUsingMethodThrows() { 86 await using a = { 87 [Symbol.asyncDispose]: () => disposed.push("a") 88 }; 89 { 90 await using b = { 91 [Symbol.dispose]: () => { 92 disposed.push("b"); 93 } 94 }; 95 using c = { 96 [Symbol.dispose]: () => { 97 disposed.push("c"); 98 throw new CustomError(); 99 } 100 } 101 } 102 } 103 testDisposedWhenSyncDisposalInUsingMethodThrows().catch((e) => { 104 caught = true; 105 assertEq(e instanceof CustomError, true); 106 }); 107 drainJobQueue(); 108 assertArrayEq(disposed, ["c", "b", "a"]); 109 assertEq(caught, true); 110 }