Skip to content

Commit

Permalink
[service-workers] Use asynchronous cleanup
Browse files Browse the repository at this point in the history
Previously, many tests un-registered service workers only after all
assertions had been satisfied. This meant that failing tests would not
un-register workers. In order to account for workers that persisted from
previous test failures, the tests included "setup" code to defensively
un-register such workers.

The `Test#add_cleanup` method was recently extended to support
asynchronous "clean up" operations [1]. Use that API to schedule service
worker un-registration so that it occurs regardless of the result of
each test.

[1] #8748
  • Loading branch information
jugglinmike committed Dec 3, 2018
1 parent 073eaac commit e065b16
Show file tree
Hide file tree
Showing 32 changed files with 201 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

return service_worker_unregister_and_register(t, script, scope)
.then(function(r) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

registration = r;

return wait_for_state(t, registration.installing, 'activated');
})
.then(function() {
Expand All @@ -28,7 +33,6 @@
})
.then(function(result) {
assert_equals(result, 'OK');
return service_worker_unregister_and_done(t, scope);
});
}, 'Post loopback messages');

Expand All @@ -41,6 +45,10 @@

return service_worker_unregister_and_register(t, script1, scope)
.then(function(r) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

registration = r;
return wait_for_state(t, registration.installing, 'activated');
})
Expand Down Expand Up @@ -69,7 +77,6 @@
.then(function(result) {
assert_equals(result, 'OK');
frame.remove();
return service_worker_unregister_and_done(t, scope);
});
}, 'Post messages among service workers');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

return service_worker_unregister_and_register(t, script, scope)
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

return wait_for_state(t, registration.installing, 'redundant');
})
.then(function() {
Expand All @@ -21,7 +25,6 @@
result,
undefined,
'After unregister(), the registration should not found');
return service_worker_unregister_and_done(t, scope);
});
}, 'Unregister on script evaluation');

Expand All @@ -31,6 +34,10 @@

return service_worker_unregister_and_register(t, script, scope)
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

return wait_for_state(t, registration.installing, 'redundant');
})
.then(function() {
Expand All @@ -41,7 +48,6 @@
result,
undefined,
'After unregister(), the registration should not found');
return service_worker_unregister_and_done(t, scope);
});
}, 'Unregister on installing event');

Expand All @@ -51,6 +57,10 @@

return service_worker_unregister_and_register(t, script, scope)
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

return wait_for_state(t, registration.installing, 'redundant');
})
.then(function() {
Expand All @@ -61,7 +71,6 @@
result,
undefined,
'After unregister(), the registration should not found');
return service_worker_unregister_and_done(t, scope);
});
}, 'Unregister on activate event');

Expand All @@ -74,6 +83,10 @@

return service_worker_unregister_and_register(t, script, scope)
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

return wait_for_state(t, registration.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
Expand Down Expand Up @@ -120,7 +133,6 @@

frame.remove();
new_frame.remove();
return service_worker_unregister_and_done(t, scope);
})
}, 'Unregister controlling service worker');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

return service_worker_unregister_and_register(t, script, scope)
.then(function(r) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

registration = r;
return wait_for_state(t, registration.installing, 'activated');
})
Expand All @@ -38,7 +42,6 @@
'events seen by the worker');
frame1.remove();
frame2.remove();
return service_worker_unregister_and_done(t, scope);
});
}, 'Update a registration on ServiceWorkerGlobalScope');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@

async function doAsyncTest(t, scope) {
let reg = await service_worker_unregister_and_register(t, worker, scope);

t.add_cleanup(() => service_worker_unregister(t, scope));

await wait_for_state(t, reg.installing, 'activated');

// Load the scope as a frame. We expect this in turn to have a nested
Expand Down Expand Up @@ -96,7 +99,6 @@
}

frame.remove();
await service_worker_unregister_and_done(t, scope);
}

promise_test(async function(t) {
Expand Down Expand Up @@ -126,6 +128,9 @@
const scope = 'resources/about-blank-replacement-uncontrolled-nested-frame.html';

let reg = await service_worker_unregister_and_register(t, worker, scope);

t.add_cleanup(() => service_worker_unregister(t, scope));

await wait_for_state(t, reg.installing, 'activated');

// Load the scope as a frame. We expect this in turn to have a nested
Expand All @@ -147,7 +152,6 @@
'nested frame should not be controlled');

frame.remove();
await service_worker_unregister_and_done(t, scope);
}, 'Initial about:blank is controlled, exposed to clients.matchAll(), and ' +
'final Client is not controlled by a service worker.');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

return service_worker_unregister_and_register(t, script, scope)
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

var sw = registration.installing;

return new Promise(t.step_func(function(resolve) {
Expand All @@ -23,9 +27,6 @@
});
}));
})
.then(function() {
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'installed event should be fired before activating service worker');

Expand Down
9 changes: 6 additions & 3 deletions service-workers/service-worker/claim-fetch.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@

// Register a service worker.
.then(() => service_worker_unregister_and_register(t, script, scope))
.then(r => worker = r.installing)
.then(() => wait_for_state(t, worker, 'activated'))
.then(r => {
t.add_cleanup(() => service_worker_unregister(t, scope));

worker = r.installing;

return wait_for_state(t, worker, 'activated');
})
// Let the service worker claim the iframe.
.then(() => {
var channel = new MessageChannel();
Expand Down Expand Up @@ -62,7 +66,6 @@

// Cleanup this testcase.
.then(() => frame.remove())
.then(() => service_worker_unregister_and_done(t, scope));
}, 'fetch() should be intercepted after the client is claimed.')

</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
return service_worker_unregister_and_register(
t, init_worker_url, init_scope)
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, init_scope);
});

return wait_for_state(t, registration.installing, 'activated');
})
.then(function() {
Expand All @@ -35,6 +39,10 @@
{scope: claim_scope});
})
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, claim_scope);
});

claim_worker = registration.installing;
claim_registration = registration;
return wait_for_state(t, registration.installing, 'activated');
Expand Down Expand Up @@ -67,9 +75,6 @@
frame1.remove();
frame2.remove();
return claim_registration.unregister();
})
.then(function() {
return service_worker_unregister_and_done(t, init_scope);
});
}, 'Test claim client which is not using registration');

Expand All @@ -86,6 +91,10 @@
claim_worker_url, {scope: claim_scope});
})
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, claim_scope);
});

claim_worker = registration.installing;
return wait_for_state(t, registration.installing, 'activated');
})
Expand All @@ -94,6 +103,10 @@
installing_worker_url, {scope: scope});
})
.then(function() {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

var channel = new MessageChannel();
var saw_message = new Promise(function(resolve) {
channel.port1.onmessage = t.step_func(function(e) {
Expand All @@ -111,10 +124,6 @@
'Frame should not be claimed when a longer-matched ' +
'registration exists');
frame.remove();
return service_worker_unregister(t, claim_scope);
})
.then(function() {
return service_worker_unregister_and_done(t, scope);
});
}, 'Test claim client when there\'s a longer-matched registration not ' +
'already used by the page');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@
'fetch() should not be intercepted.'))
// Register a service worker.
.then(() => service_worker_unregister_and_register(t, script, scope))
.then(r => worker = r.installing)
.then(() => wait_for_state(t, worker, 'activated'))
.then(r => {
t.add_cleanup(() => service_worker_unregister(t, scope));

worker = r.installing;

return wait_for_state(t, worker, 'activated')
})
// Let the service worker claim the iframe and the shared worker.
.then(() => {
var channel = new MessageChannel();
Expand Down Expand Up @@ -60,8 +64,7 @@
'fetch() in the shared worker should be intercepted.'))

// Cleanup this testcase.
.then(() => frame.remove())
.then(() => service_worker_unregister_and_done(t, scope));
.then(() => frame.remove());
}, 'fetch() in SharedWorker should be intercepted after the client is claimed.')

</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
var worker, sw_registration, frame;
return service_worker_unregister_and_register(t, url1, scope)
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

return wait_for_state(t, registration.installing, 'activated');
})
.then(function() {
Expand Down Expand Up @@ -50,9 +54,6 @@
'Frame1 controller scriptURL should be changed to url2');
frame.remove();
return sw_registration.unregister();
})
.then(function() {
return service_worker_unregister_and_done(t, scope);
});
}, 'Test worker claims client which is using another registration');

Expand All @@ -63,6 +64,10 @@
var frame, worker;
return service_worker_unregister_and_register(t, url1, scope)
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

return wait_for_state(t, registration.installing, 'activated');
})
.then(function() {
Expand Down Expand Up @@ -91,7 +96,6 @@
})
.then(function() {
frame.remove();
return service_worker_unregister_and_done(t, scope);
});
}, 'Test for the waiting worker claims a client which is using the the ' +
'same registration');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
return service_worker_unregister_and_register(
t, 'resources/clients-matchall-worker.js', scope)
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

return wait_for_state(t, registration.installing, 'activated');
})
.then(function() { return with_iframe(iframe_url); })
Expand All @@ -66,16 +70,18 @@
})
.then(function() {
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
});
}, 'Verify matchAll() with window client type');

promise_test(function(t) {
var frame;
return service_worker_unregister_and_register(
t, 'resources/clients-matchall-worker.js', scope)
.then(function(registration) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});

return wait_for_state(t, registration.installing, 'activated');
})
.then(function() { return with_iframe(iframe_url); })
Expand Down Expand Up @@ -112,7 +118,6 @@
})
.then(function() {
frame.remove();
return service_worker_unregister_and_done(t, scope);
});
}, 'Verify matchAll() with {window, sharedworker, worker} client types');

Expand Down
Loading

0 comments on commit e065b16

Please sign in to comment.