Skip to content

Commit 812ba43

Browse files
committed
test(runtime): keep comparers calling through the worker's teardown
The spec stopped its comparer threads at the first dropped call, but terminate()'s TerminateExecution can land on a comparer and yield a 0 before the worker starts tearing down, so the spec could pass without a call overlapping DestroyRuntime. Each comparer now stops on its own after 20 dropped calls in a row, which only happens once the worker is gone; the calls it had in flight are the ones that overlap the teardown. That also bounds the discard warnings the comparers log after the Java detach. Each call passes a new Object, so it registers the argument from the comparer thread and exercises generateNewObjectId's foreign-thread path. afterEach terminates the worker and stops the comparers even when jasmine times the spec out, and the poll chain stops with the spec. Waits are 5 s each, well inside the 30 s spec timeout. Without multithreaded JS the spec is pending rather than silently passing.
1 parent 029ff02 commit 812ba43

2 files changed

Lines changed: 53 additions & 17 deletions

File tree

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,22 @@ describe("Runtime-bound Java->JS dispatch", function () {
1818
}
1919

2020
var originalTimeout;
21+
// Set by a spec whose comparer threads and polls must not outlive it, even
22+
// when jasmine times it out.
23+
var comparingWorker = null;
24+
var specEnded = false;
2125
beforeEach(function () {
2226
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
2327
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
28+
specEnded = false;
2429
});
2530
afterEach(function () {
31+
specEnded = true;
32+
if (comparingWorker !== null) {
33+
comparingWorker.terminate();
34+
comparingWorker = null;
35+
Holder.stopComparing(5000);
36+
}
2637
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
2738
Holder.clear();
2839
});
@@ -97,19 +108,19 @@ describe("Runtime-bound Java->JS dispatch", function () {
97108
it("answers or drops calls other threads make while the worker terminates", function (done) {
98109
if (!multithreaded) {
99110
// Calls are posted to the worker's looper in this mode, not run under its lock.
100-
done();
101-
return;
111+
pending("needs android.enableMultithreadedJavascript");
102112
}
103113

104-
var worker;
114+
// Each wait well inside the spec timeout, so a hang fails with its cause.
105115
function waitFor(condition, what, then) {
106-
var deadline = Date.now() + 10000;
116+
var deadline = Date.now() + 5000;
107117
(function poll() {
118+
if (specEnded) {
119+
return;
120+
}
108121
if (condition()) {
109122
then();
110123
} else if (Date.now() > deadline) {
111-
worker.terminate();
112-
Holder.stopComparing(10000);
113124
fail("timed out waiting for " + what);
114125
done();
115126
} else {
@@ -118,17 +129,23 @@ describe("Runtime-bound Java->JS dispatch", function () {
118129
})();
119130
}
120131

121-
startWorker(function (started) {
122-
worker = started;
123-
Holder.startComparing("comparable", 4);
132+
comparingWorker = startWorker(function (worker) {
133+
if (specEnded) {
134+
return;
135+
}
136+
// A comparer stops once it has had 20 dropped calls in a row: by then the worker
137+
// has been torn down, and the calls it had in flight overlapped that teardown.
138+
Holder.startComparing("comparable", 4, 20);
124139
waitFor(function () {
125140
return Holder.hasCompared(42);
126141
}, "a call answered by the live worker", function () {
127142
worker.terminate();
128143
waitFor(function () {
129-
return Holder.hasCompared(0);
130-
}, "a call dropped after terminate()", function () {
131-
var results = Array.prototype.slice.call(Holder.stopComparing(10000));
144+
return Holder.comparersFinished();
145+
}, "every comparer to see its calls dropped", function () {
146+
comparingWorker = null;
147+
var results = Array.prototype.slice.call(Holder.stopComparing(5000));
148+
expect(results).toContain(0);
132149
results.forEach(function (result) {
133150
expect([42, 0]).toContain(result);
134151
});

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,30 @@ public static boolean hasCompared(int result) {
5757
return compared.contains(result);
5858
}
5959

60-
// Keeps calling compare(key) from `threads` new threads until stopComparing().
61-
public static synchronized void startComparing(final String key, int threads) {
60+
/*
61+
* Keeps calling compareTo on objects[key] from `threads` new threads, each with a new
62+
* object as the argument (so the call registers it from that thread), until
63+
* stopComparing() or until the thread has had `zerosToStop` results of 0 in a row.
64+
*/
65+
public static synchronized void startComparing(final String key, int threads, final int zerosToStop) {
6266
compared.clear();
6367
comparing = true;
6468
for (int i = 0; i < threads; i++) {
6569
Thread thread = new Thread(new Runnable() {
6670
@Override
71+
@SuppressWarnings("unchecked")
6772
public void run() {
68-
while (comparing) {
73+
Comparable<Object> comparable = (Comparable<Object>) objects.get(key);
74+
int zeros = 0;
75+
while (comparing && zeros < zerosToStop) {
76+
int result;
6977
try {
70-
compared.add(compare(key));
78+
result = comparable.compareTo(new Object());
7179
} catch (Throwable t) {
72-
compared.add(THREW);
80+
result = THREW;
7381
}
82+
compared.add(result);
83+
zeros = result == 0 ? zeros + 1 : 0;
7484
}
7585
}
7686
}, "RuntimeBoundHolder-comparer-" + i);
@@ -79,6 +89,15 @@ public void run() {
7989
}
8090
}
8191

92+
public static synchronized boolean comparersFinished() {
93+
for (Thread thread : comparers) {
94+
if (thread.isAlive()) {
95+
return false;
96+
}
97+
}
98+
return true;
99+
}
100+
82101
// Stops the comparing threads, waiting up to timeoutMillis for them; returns every
83102
// distinct result, with THREW for a throw and STUCK if a thread is still running.
84103
public static synchronized int[] stopComparing(long timeoutMillis) throws InterruptedException {

0 commit comments

Comments
 (0)