Skip to content

Commit

Permalink
Bug 1651522 - [devtools] Fix browser_dbg-windowless-service-workers-r…
Browse files Browse the repository at this point in the history
…eload.js intermittent. r=devtools-reviewers,nchevobbe

The SW wasn't properly unregistered between tests, probably causing failure in other test using this test page.

Differential Revision: https://phabricator.services.mozilla.com/D191645
  • Loading branch information
ochameau committed Nov 20, 2023
1 parent 0d2c86d commit dd5c0bf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_task(async function () {
await pushPref("devtools.debugger.threads-visible", true);
await pushPref("dom.serviceWorkers.enabled", true);
await pushPref("dom.serviceWorkers.testing.enabled", true);

const dbg = await initDebugger("doc-service-workers.html");

invokeInTab("registerWorker");
Expand Down Expand Up @@ -52,6 +53,39 @@ add_task(async function () {
},
]);

await resume(dbg);

// Help the SW to be immediately destroyed after unregistering it.
// Do not use too low value as it needs to keep running while we do assertions against debugger UI.
// This can be slow on debug builds.
await pushPref("dom.serviceWorkers.idle_timeout", 500);
await pushPref("dom.serviceWorkers.idle_extended_timeout", 500);

const swm = Cc["@mozilla.org/serviceworkers/manager;1"].getService(
Ci.nsIServiceWorkerManager
);
// Unfortunately, swm.getRegistrationByPrincipal doesn't work and throw,
// so go over the live list of all SW to find the matching SW info,
// in order to retrieve its active worker,
// in order to call attach+detachDebugger,
// in order to reset the idle timeout.
const registrations = swm.getAllRegistrations();
for (let i = 0; i < registrations.length; i++) {
const info = registrations.queryElementAt(
i,
Ci.nsIServiceWorkerRegistrationInfo
);
if (info.scriptSpec.endsWith("service-worker.sjs")) {
info.activeWorker.attachDebugger();
info.activeWorker.detachDebugger();
}
}

// Thanks to previous hack, the following call should immediately unregister the SW
invokeInTab("unregisterWorker");

await checkAdditionalThreadCount(dbg, 0);

await waitForRequestsToSettle(dbg);
await removeTab(gBrowser.selectedTab);
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ add_task(async function () {
);
const dbg = createDebuggerContext(toolbox);

await checkWorkerThreads(dbg, 1);
await checkAdditionalThreadCount(dbg, 1);

// The test page will immediately fetch from the service worker if registered.
const onReloaded = reload(dbg);
Expand All @@ -57,7 +57,7 @@ add_task(async function () {

await waitForPaused(dbg);
assertPausedAtSourceAndLine(dbg, workerSource.id, 13);
await checkWorkerThreads(dbg, 1);
await checkAdditionalThreadCount(dbg, 1);

await resume(dbg);
await dbg.actions.removeAllBreakpoints();
Expand All @@ -67,7 +67,7 @@ add_task(async function () {

invokeInTab("unregisterWorker");

await checkWorkerThreads(dbg, 0);
await checkAdditionalThreadCount(dbg, 0);
await waitForRequestsToSettle(dbg);
await removeTab(gBrowser.selectedTab);
});
Expand All @@ -83,7 +83,7 @@ add_task(async function () {
const dbg = createDebuggerContext(toolbox);

invokeInTab("registerWorker");
await checkWorkerThreads(dbg, 1);
await checkAdditionalThreadCount(dbg, 1);
await checkWorkerStatus(dbg, "activated");

const firstTab = gBrowser.selectedTab;
Expand All @@ -94,7 +94,7 @@ add_task(async function () {
const secondTab = await addTab(`${EXAMPLE_URL}doc-service-workers.html`);

await gBrowser.selectTabAtIndex(gBrowser.tabs.indexOf(firstTab));
await checkWorkerThreads(dbg, 2);
await checkAdditionalThreadCount(dbg, 2);

const sources = await waitFor(() => {
const list = dbg.selectors
Expand All @@ -109,7 +109,7 @@ add_task(async function () {

invokeInTab("unregisterWorker");

await checkWorkerThreads(dbg, 0);
await checkAdditionalThreadCount(dbg, 0);
await waitForRequestsToSettle(dbg);
await removeTab(firstTab);
await removeTab(secondTab);
Expand All @@ -134,7 +134,7 @@ add_task(async function () {
const dbg = createDebuggerContext(toolbox);

invokeInTab("registerWorker");
await checkWorkerThreads(dbg, 1);
await checkAdditionalThreadCount(dbg, 1);

await waitForSource(dbg, "service-worker.sjs");
const workerSource = findSource(dbg, "service-worker.sjs");
Expand All @@ -159,16 +159,11 @@ add_task(async function () {
await resume(dbg);
invokeInTab("unregisterWorker");

await checkWorkerThreads(dbg, 0);
await checkAdditionalThreadCount(dbg, 0);
await waitForRequestsToSettle(dbg);
await removeTab(gBrowser.selectedTab);
});

async function checkWorkerThreads(dbg, count) {
await waitUntil(() => dbg.selectors.getThreads().length == count);
ok(true, `Have ${count} threads`);
}

async function checkWorkerStatus(dbg, status) {
/* TODO: Re-Add support for showing service worker status (Bug 1641099)
await waitUntil(() => {
Expand Down
12 changes: 11 additions & 1 deletion devtools/client/debugger/test/mochitest/shared-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -3070,7 +3070,6 @@ async function selectBlackBoxContextMenuItem(dbg, itemName) {

// Test empty panel when source has not function or class symbols
// Test that anonymous functions do not show in the outline panel

function assertOutlineItems(dbg, expectedItems) {
const outlineItems = Array.from(
findAllElementsWithSelector(
Expand All @@ -3084,3 +3083,14 @@ function assertOutlineItems(dbg, expectedItems) {
"The expected items are displayed in the outline panel"
);
}

async function checkAdditionalThreadCount(dbg, count) {
await waitForState(
dbg,
state => {
return dbg.selectors.getThreads().length == count;
},
"Have the expected number of additional threads"
);
ok(true, `Have ${count} threads`);
}

0 comments on commit dd5c0bf

Please sign in to comment.