Skip to content

Commit b29ff10

Browse files
committed
test(runtime): terminate the worker on a timeout and copy results safely
A spec that timed out before terminating its worker left the worker running into later specs. stopComparing copies the results through a snapshot, since a comparer still running after its join deadline can add one while they are copied.
1 parent ab97c89 commit b29ff10

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@ describe("Runtime-bound Java->JS dispatch", function () {
101101
return;
102102
}
103103

104+
var worker;
104105
function waitFor(condition, what, then) {
105106
var deadline = Date.now() + 10000;
106107
(function poll() {
107108
if (condition()) {
108109
then();
109110
} else if (Date.now() > deadline) {
111+
worker.terminate();
110112
Holder.stopComparing(10000);
111113
fail("timed out waiting for " + what);
112114
done();
@@ -116,7 +118,8 @@ describe("Runtime-bound Java->JS dispatch", function () {
116118
})();
117119
}
118120

119-
startWorker(function (worker) {
121+
startWorker(function (started) {
122+
worker = started;
120123
Holder.startComparing("comparable", 4);
121124
waitFor(function () {
122125
return Holder.hasCompared(42);

‎test-app/app/src/main/java/com/tns/tests/RuntimeBoundHolder.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ public static synchronized int[] stopComparing(long timeoutMillis) throws Interr
9191
}
9292
}
9393
comparers.clear();
94-
int[] results = new int[compared.size()];
95-
int i = 0;
96-
for (int result : compared) {
97-
results[i++] = result;
94+
// A thread still running can add results while this copies them.
95+
Integer[] snapshot = compared.toArray(new Integer[0]);
96+
int[] results = new int[snapshot.length];
97+
for (int i = 0; i < snapshot.length; i++) {
98+
results[i] = snapshot[i];
9899
}
99100
return results;
100101
}

0 commit comments

Comments
 (0)