Visitar URL original
fix(worker): leave the inspector pause loop when the worker is terminating by adrian-niculescu · Pull Request #2071 · NativeScript/android · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
adrian-niculescu committed Oct 6, 2026
commit 44f3c8cf3dc9843f8dddb6d259a6484b322cd644
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
describe("Worker terminate after close", function () {
var START_DEADLINE = 3000;
var SETTLE_AFTER = 300;
// [0] counts the worker's loop iterations; [1] stops the loop, so a worker
// that terminate() failed to stop does not outlive the spec.
var shared;
var timers = [];
var originalTimeout;

beforeEach(function () {
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
});

afterEach(function () {
timers.forEach(clearTimeout);
timers = [];
if (shared) {
Atomics.store(shared, 1, 1);
shared = null;
}
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});

function later(fn, ms) {
timers.push(setTimeout(fn, ms));
}

it("stops a worker that keeps running after it called close()", function (done) {
var counter = new Int32Array(new SharedArrayBuffer(4));
shared = new Int32Array(new SharedArrayBuffer(8));
var counter = shared;
var worker = new Worker("./workerCloseThenSpinWorker.js");
worker.postMessage(counter.buffer);
var started = Date.now();

(function waitForSpin() {
if (Atomics.load(counter, 0) === 0) {
if (Date.now() - started > 5000) {
fail("the worker never started running");
if (Date.now() - started > START_DEADLINE) {
expect("the worker never started running").toBeNull();
done();
return;
}
setTimeout(waitForSpin, 20);
later(waitForSpin, 20);
return;
}
worker.terminate();
setTimeout(function () {
later(function () {
var afterTerminate = Atomics.load(counter, 0);
setTimeout(function () {
later(function () {
expect(Atomics.load(counter, 0)).toBe(afterTerminate);
done();
}, SETTLE_AFTER);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// close() lets the running callback finish, so this one never returns unless
// terminate() interrupts it.
// terminate() interrupts it, or the spec raises the stop flag to clean up.
onmessage = function (event) {
var counter = new Int32Array(event.data);
var shared = new Int32Array(event.data);
close();
for (;;) {
Atomics.add(counter, 0, 1);
while (Atomics.load(shared, 1) === 0) {
Atomics.add(shared, 0, 1);
}
};
Loading