services/sync/tests/unit/test_utils_catch.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.

michael@0 1 Cu.import("resource://services-sync/util.js");
michael@0 2 Cu.import("resource://services-sync/service.js");
michael@0 3
michael@0 4 function run_test() {
michael@0 5 _("Make sure catch when copied to an object will correctly catch stuff");
michael@0 6 let ret, rightThis, didCall, didThrow, wasTen, wasLocked;
michael@0 7 let obj = {
michael@0 8 catch: Utils.catch,
michael@0 9 _log: {
michael@0 10 debug: function(str) {
michael@0 11 didThrow = str.search(/^Exception: /) == 0;
michael@0 12 },
michael@0 13 info: function(str) {
michael@0 14 wasLocked = str.indexOf("Cannot start sync: already syncing?") == 0;
michael@0 15 }
michael@0 16 },
michael@0 17
michael@0 18 func: function() this.catch(function() {
michael@0 19 rightThis = this == obj;
michael@0 20 didCall = true;
michael@0 21 return 5;
michael@0 22 })(),
michael@0 23
michael@0 24 throwy: function() this.catch(function() {
michael@0 25 rightThis = this == obj;
michael@0 26 didCall = true;
michael@0 27 throw 10;
michael@0 28 })(),
michael@0 29
michael@0 30 callbacky: function() this.catch(function() {
michael@0 31 rightThis = this == obj;
michael@0 32 didCall = true;
michael@0 33 throw 10;
michael@0 34 }, function(ex) {
michael@0 35 wasTen = (ex == 10)
michael@0 36 })(),
michael@0 37
michael@0 38 lockedy: function() this.catch(function() {
michael@0 39 rightThis = this == obj;
michael@0 40 didCall = true;
michael@0 41 throw("Could not acquire lock.");
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 = didThrow = wasLocked = false;
michael@0 47 ret = obj.func();
michael@0 48 do_check_eq(ret, 5);
michael@0 49 do_check_true(rightThis);
michael@0 50 do_check_true(didCall);
michael@0 51 do_check_false(didThrow);
michael@0 52 do_check_eq(wasTen, undefined);
michael@0 53 do_check_false(wasLocked);
michael@0 54
michael@0 55 _("Make sure catch/throw results in debug call and caller doesn't need to handle exception");
michael@0 56 rightThis = didCall = didThrow = wasLocked = false;
michael@0 57 ret = obj.throwy();
michael@0 58 do_check_eq(ret, undefined);
michael@0 59 do_check_true(rightThis);
michael@0 60 do_check_true(didCall);
michael@0 61 do_check_true(didThrow);
michael@0 62 do_check_eq(wasTen, undefined);
michael@0 63 do_check_false(wasLocked);
michael@0 64
michael@0 65 _("Test callback for exception testing.");
michael@0 66 rightThis = didCall = didThrow = wasLocked = false;
michael@0 67 ret = obj.callbacky();
michael@0 68 do_check_eq(ret, undefined);
michael@0 69 do_check_true(rightThis);
michael@0 70 do_check_true(didCall);
michael@0 71 do_check_true(didThrow);
michael@0 72 do_check_true(wasTen);
michael@0 73 do_check_false(wasLocked);
michael@0 74
michael@0 75 _("Test the lock-aware catch that Service uses.");
michael@0 76 obj.catch = Service._catch;
michael@0 77 rightThis = didCall = didThrow = wasLocked = false;
michael@0 78 wasTen = undefined;
michael@0 79 ret = obj.lockedy();
michael@0 80 do_check_eq(ret, undefined);
michael@0 81 do_check_true(rightThis);
michael@0 82 do_check_true(didCall);
michael@0 83 do_check_true(didThrow);
michael@0 84 do_check_eq(wasTen, undefined);
michael@0 85 do_check_true(wasLocked);
michael@0 86 }

mercurial