summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_utils_catch.js
blob: a10e5eb0d804f85925a70a2eb75f2c4215fddfc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Cu.import("resource://services-sync/util.js");
Cu.import("resource://services-sync/service.js");

function run_test() {
  _("Make sure catch when copied to an object will correctly catch stuff");
  let ret, rightThis, didCall, didThrow, wasTen, wasLocked;
  let obj = {
    catch: Utils.catch,
    _log: {
      debug: function(str) {
        didThrow = str.search(/^Exception: /) == 0;
      },
      info: function(str) {
        wasLocked = str.indexOf("Cannot start sync: already syncing?") == 0;
      }
    },

    func: function() this.catch(function() {
      rightThis = this == obj;
      didCall = true;
      return 5;
    })(),

    throwy: function() this.catch(function() {
      rightThis = this == obj;
      didCall = true;
      throw 10;
    })(),

    callbacky: function() this.catch(function() {
      rightThis = this == obj;
      didCall = true;
      throw 10;
    }, function(ex) {
      wasTen = (ex == 10)
    })(),

    lockedy: function() this.catch(function() {
      rightThis = this == obj;
      didCall = true;
      throw("Could not acquire lock.");
    })()
  };

  _("Make sure a normal call will call and return");
  rightThis = didCall = didThrow = wasLocked = false;
  ret = obj.func();
  do_check_eq(ret, 5);
  do_check_true(rightThis);
  do_check_true(didCall);
  do_check_false(didThrow);
  do_check_eq(wasTen, undefined);
  do_check_false(wasLocked);

  _("Make sure catch/throw results in debug call and caller doesn't need to handle exception");
  rightThis = didCall = didThrow = wasLocked = false;
  ret = obj.throwy();
  do_check_eq(ret, undefined);
  do_check_true(rightThis);
  do_check_true(didCall);
  do_check_true(didThrow);
  do_check_eq(wasTen, undefined);
  do_check_false(wasLocked);

  _("Test callback for exception testing.");
  rightThis = didCall = didThrow = wasLocked = false;
  ret = obj.callbacky();
  do_check_eq(ret, undefined);
  do_check_true(rightThis);
  do_check_true(didCall);
  do_check_true(didThrow);
  do_check_true(wasTen);
  do_check_false(wasLocked);

  _("Test the lock-aware catch that Service uses.");
  obj.catch = Service._catch;
  rightThis = didCall = didThrow = wasLocked = false;
  wasTen = undefined;
  ret = obj.lockedy();
  do_check_eq(ret, undefined);
  do_check_true(rightThis);
  do_check_true(didCall);
  do_check_true(didThrow);
  do_check_eq(wasTen, undefined);
  do_check_true(wasLocked);
}