diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /services/sync/tests/unit/test_utils_catch.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'services/sync/tests/unit/test_utils_catch.js')
-rw-r--r-- | services/sync/tests/unit/test_utils_catch.js | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/services/sync/tests/unit/test_utils_catch.js b/services/sync/tests/unit/test_utils_catch.js new file mode 100644 index 0000000000..590d04527f --- /dev/null +++ b/services/sync/tests/unit/test_utils_catch.js @@ -0,0 +1,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); +}); |