summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_utils_catch.js
blob: 590d04527f864f1c5c239d4f86ddacf565527413 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const { Service } = ChromeUtils.importESModule(
  "resource://services-sync/service.sys.mjs"
);

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

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

    throwy() {
      return this._catch(async function () {
        rightThis = this == obj;
        didCall = true;
        throw new Error("covfefe");
      })();
    },

    callbacky() {
      return this._catch(
        async function () {
          rightThis = this == obj;
          didCall = true;
          throw new Error("covfefe");
        },
        async function (ex) {
          wasCovfefe = ex && ex.message == "covfefe";
        }
      )();
    },

    lockedy() {
      return this._catch(async function () {
        rightThis = this == obj;
        didCall = true;
        Utils.throwLockException(null);
      })();
    },

    lockedy_chained() {
      return this._catch(async function () {
        rightThis = this == obj;
        didCall = true;
        Utils.throwLockException(null);
      })();
    },
  };

  _("Make sure a normal call will call and return");
  rightThis = didCall = didThrow = wasLocked = false;
  ret = await obj.func();
  Assert.equal(ret, 5);
  Assert.ok(rightThis);
  Assert.ok(didCall);
  Assert.ok(!didThrow);
  Assert.equal(wasCovfefe, undefined);
  Assert.ok(!wasLocked);

  _(
    "Make sure catch/throw results in debug call and caller doesn't need to handle exception"
  );
  rightThis = didCall = didThrow = wasLocked = false;
  ret = await obj.throwy();
  Assert.equal(ret, undefined);
  Assert.ok(rightThis);
  Assert.ok(didCall);
  Assert.ok(didThrow);
  Assert.equal(wasCovfefe, undefined);
  Assert.ok(!wasLocked);

  _("Test callback for exception testing.");
  rightThis = didCall = didThrow = wasLocked = false;
  ret = await obj.callbacky();
  Assert.equal(ret, undefined);
  Assert.ok(rightThis);
  Assert.ok(didCall);
  Assert.ok(didThrow);
  Assert.ok(wasCovfefe);
  Assert.ok(!wasLocked);

  _("Test the lock-aware catch that Service uses.");
  obj._catch = Service._catch;
  rightThis = didCall = didThrow = wasLocked = false;
  wasCovfefe = undefined;
  ret = await obj.lockedy();
  Assert.equal(ret, undefined);
  Assert.ok(rightThis);
  Assert.ok(didCall);
  Assert.ok(didThrow);
  Assert.equal(wasCovfefe, undefined);
  Assert.ok(wasLocked);

  _("Test the lock-aware catch that Service uses with a chained promise.");
  rightThis = didCall = didThrow = wasLocked = false;
  wasCovfefe = undefined;
  ret = await obj.lockedy_chained();
  Assert.equal(ret, undefined);
  Assert.ok(rightThis);
  Assert.ok(didCall);
  Assert.ok(didThrow);
  Assert.equal(wasCovfefe, undefined);
  Assert.ok(wasLocked);
});