Skip to content

Commit 44f3c8c

Browse files
test(worker): stop the close-then-spin worker on cleanup and fit the spec in its timeout
A run where terminate() failed left the worker spinning past the spec. The loop now also checks a stop flag the spec raises in afterEach, the spec gets its own Jasmine timeout with a shorter start deadline, and the start-deadline branch fails through an expectation rather than the fail() global this Jasmine does not provide.
1 parent 921209f commit 44f3c8c

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

‎test-app/app/src/main/assets/app/tests/testWorkerTerminateAfterClose.js‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,52 @@
11
describe("Worker terminate after close", function () {
2+
var START_DEADLINE = 3000;
23
var SETTLE_AFTER = 300;
4+
// [0] counts the worker's loop iterations; [1] stops the loop, so a worker
5+
// that terminate() failed to stop does not outlive the spec.
6+
var shared;
7+
var timers = [];
8+
var originalTimeout;
9+
10+
beforeEach(function () {
11+
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
12+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
13+
});
14+
15+
afterEach(function () {
16+
timers.forEach(clearTimeout);
17+
timers = [];
18+
if (shared) {
19+
Atomics.store(shared, 1, 1);
20+
shared = null;
21+
}
22+
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
23+
});
24+
25+
function later(fn, ms) {
26+
timers.push(setTimeout(fn, ms));
27+
}
328

429
it("stops a worker that keeps running after it called close()", function (done) {
5-
var counter = new Int32Array(new SharedArrayBuffer(4));
30+
shared = new Int32Array(new SharedArrayBuffer(8));
31+
var counter = shared;
632
var worker = new Worker("./workerCloseThenSpinWorker.js");
733
worker.postMessage(counter.buffer);
834
var started = Date.now();
935

1036
(function waitForSpin() {
1137
if (Atomics.load(counter, 0) === 0) {
12-
if (Date.now() - started > 5000) {
13-
fail("the worker never started running");
38+
if (Date.now() - started > START_DEADLINE) {
39+
expect("the worker never started running").toBeNull();
1440
done();
1541
return;
1642
}
17-
setTimeout(waitForSpin, 20);
43+
later(waitForSpin, 20);
1844
return;
1945
}
2046
worker.terminate();
21-
setTimeout(function () {
47+
later(function () {
2248
var afterTerminate = Atomics.load(counter, 0);
23-
setTimeout(function () {
49+
later(function () {
2450
expect(Atomics.load(counter, 0)).toBe(afterTerminate);
2551
done();
2652
}, SETTLE_AFTER);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// close() lets the running callback finish, so this one never returns unless
2-
// terminate() interrupts it.
2+
// terminate() interrupts it, or the spec raises the stop flag to clean up.
33
onmessage = function (event) {
4-
var counter = new Int32Array(event.data);
4+
var shared = new Int32Array(event.data);
55
close();
6-
for (;;) {
7-
Atomics.add(counter, 0, 1);
6+
while (Atomics.load(shared, 1) === 0) {
7+
Atomics.add(shared, 0, 1);
88
}
99
};

0 commit comments

Comments
 (0)