1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_utils_lock.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,75 @@ 1.4 +_("Make sure lock prevents calling with a shared lock"); 1.5 +Cu.import("resource://services-sync/util.js"); 1.6 + 1.7 +// Utility that we only use here. 1.8 + 1.9 +function do_check_begins(thing, startsWith) { 1.10 + if (!(thing && thing.indexOf && (thing.indexOf(startsWith) == 0))) 1.11 + do_throw(thing + " doesn't begin with " + startsWith); 1.12 +} 1.13 + 1.14 +function run_test() { 1.15 + let ret, rightThis, didCall; 1.16 + let state, lockState, lockedState, unlockState; 1.17 + let obj = { 1.18 + _lock: Utils.lock, 1.19 + lock: function() { 1.20 + lockState = ++state; 1.21 + if (this._locked) { 1.22 + lockedState = ++state; 1.23 + return false; 1.24 + } 1.25 + this._locked = true; 1.26 + return true; 1.27 + }, 1.28 + unlock: function() { 1.29 + unlockState = ++state; 1.30 + this._locked = false; 1.31 + }, 1.32 + 1.33 + func: function() this._lock("Test utils lock", 1.34 + function() { 1.35 + rightThis = this == obj; 1.36 + didCall = true; 1.37 + return 5; 1.38 + })(), 1.39 + 1.40 + throwy: function() this._lock("Test utils lock throwy", 1.41 + function() { 1.42 + rightThis = this == obj; 1.43 + didCall = true; 1.44 + this.throwy(); 1.45 + })() 1.46 + }; 1.47 + 1.48 + _("Make sure a normal call will call and return"); 1.49 + rightThis = didCall = false; 1.50 + state = 0; 1.51 + ret = obj.func(); 1.52 + do_check_eq(ret, 5); 1.53 + do_check_true(rightThis); 1.54 + do_check_true(didCall); 1.55 + do_check_eq(lockState, 1); 1.56 + do_check_eq(unlockState, 2); 1.57 + do_check_eq(state, 2); 1.58 + 1.59 + _("Make sure code that calls locked code throws"); 1.60 + ret = null; 1.61 + rightThis = didCall = false; 1.62 + try { 1.63 + ret = obj.throwy(); 1.64 + do_throw("throwy internal call should have thrown!"); 1.65 + } 1.66 + catch(ex) { 1.67 + // Should throw an Error, not a string. 1.68 + do_check_begins(ex, "Could not acquire lock"); 1.69 + } 1.70 + do_check_eq(ret, null); 1.71 + do_check_true(rightThis); 1.72 + do_check_true(didCall); 1.73 + _("Lock should be called twice so state 3 is skipped"); 1.74 + do_check_eq(lockState, 4); 1.75 + do_check_eq(lockedState, 5); 1.76 + do_check_eq(unlockState, 6); 1.77 + do_check_eq(state, 6); 1.78 +}