services/sync/tests/unit/test_utils_lock.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial