services/sync/tests/unit/test_utils_lock.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial