fune/toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtask_shouldprocessupdates.js
Nick Alexander ca22dc040c Bug 1695797 - In background task mode, only process updates if we're the sole instance running. r=mhowell,application-update-reviewers,dthayer,bytesized
This commit does three main things:

1) It allows to configure the global singleton `nsUpdateSyncManager`
with an `nsIFile` rather than having it use the ambient XPCOM
directory service.  This allows to initialize the
`nsUpdateSyncManager` very early: before processing updates and long
before XPCOM is initialized.  This in turn allows us to determine if
other instances early enough to skip processing updates when
appropriate.

When this initialization path is followed, i.e., in Firefox but not
`xpcshell`, the `xpcom-startup` notification will be received but no
action taken, since the singleton will already exist.

There is a classic time-of-check, time-of-use race window in this
implementation: an instance may be launched immediately after we check
for other instances.  In practice this will result in behaviour that
is alreay possible: two independent instances both processing updates.
It is expected that the updater itself will exclude one of the
instances using its existing mutex.

2) It updates an existing background task test to use an explicit
`nsIFile` rather than the existing directory service method.  This
exercises the newer API.  There are other tests that might benefit,
but there's no harm in remaining with the previous approach, since
both are required.

3) It adds a new background task test to verify that update processing
is skipped if we're not the sole instance running.

Differential Revision: https://phabricator.services.mozilla.com/D106994
2021-03-06 05:40:39 +00:00

37 lines
1.7 KiB
JavaScript

/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* vim: sw=4 ts=4 sts=4 et
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
add_task(async function test_backgroundtask_shouldprocessupdates() {
// The task returns 80 if !ShouldProcessUpdates(), 81 otherwise. xpcshell
// itself counts as an instance, so the background task will see it and think
// another instance is running. N.b.: this isn't as robust as it could be:
// running Firefox instances and parallel tests interact here (mostly
// harmlessly).
//
// Since the behaviour under test (ShouldProcessUpdates()) happens at startup,
// we can't easily change the lock location of the background task.
let exitCode = await do_backgroundtask("shouldprocessupdates", {
extraArgs: ["--test-process-updates"],
});
Assert.equal(80, exitCode);
// If we change our lock location, the background task won't see our instance
// running.
let file = do_get_profile();
file.append("customExePath");
let syncManager = Cc["@mozilla.org/updates/update-sync-manager;1"].getService(
Ci.nsIUpdateSyncManager
);
syncManager.resetLock(file);
// The task returns 80 if !ShouldProcessUpdates(), 81 if
// ShouldProcessUpdates(). Since we've changed the xpcshell executable name,
// the background task won't see us and think another instance is running.
// N.b.: this generally races with other parallel tests and in the future it
// could be made more robust.
exitCode = await do_backgroundtask("shouldprocessupdates");
Assert.equal(81, exitCode);
});