test_utils_lock.js (1912B)
1 _("Make sure lock prevents calling with a shared lock"); 2 3 // Utility that we only use here. 4 5 function do_check_begins(thing, startsWith) { 6 if (!(thing && thing.indexOf && thing.indexOf(startsWith) == 0)) { 7 do_throw(thing + " doesn't begin with " + startsWith); 8 } 9 } 10 11 add_task(async function run_test() { 12 let ret, rightThis, didCall; 13 let state, lockState, lockedState, unlockState; 14 let obj = { 15 _lock: Utils.lock, 16 lock() { 17 lockState = ++state; 18 if (this._locked) { 19 lockedState = ++state; 20 return false; 21 } 22 this._locked = true; 23 return true; 24 }, 25 unlock() { 26 unlockState = ++state; 27 this._locked = false; 28 }, 29 30 func() { 31 return this._lock("Test utils lock", async function () { 32 rightThis = this == obj; 33 didCall = true; 34 return 5; 35 })(); 36 }, 37 38 throwy() { 39 return this._lock("Test utils lock throwy", async function () { 40 rightThis = this == obj; 41 didCall = true; 42 return this.throwy(); 43 })(); 44 }, 45 }; 46 47 _("Make sure a normal call will call and return"); 48 rightThis = didCall = false; 49 state = 0; 50 ret = await obj.func(); 51 Assert.equal(ret, 5); 52 Assert.ok(rightThis); 53 Assert.ok(didCall); 54 Assert.equal(lockState, 1); 55 Assert.equal(unlockState, 2); 56 Assert.equal(state, 2); 57 58 _("Make sure code that calls locked code throws"); 59 ret = null; 60 rightThis = didCall = false; 61 try { 62 ret = await obj.throwy(); 63 do_throw("throwy internal call should have thrown!"); 64 } catch (ex) { 65 // Should throw an Error, not a string. 66 do_check_begins(ex.message, "Could not acquire lock"); 67 } 68 Assert.equal(ret, null); 69 Assert.ok(rightThis); 70 Assert.ok(didCall); 71 _("Lock should be called twice so state 3 is skipped"); 72 Assert.equal(lockState, 4); 73 Assert.equal(lockedState, 5); 74 Assert.equal(unlockState, 6); 75 Assert.equal(state, 6); 76 });