Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 25 additions & 0 deletions test-app/app/src/main/assets/app/tests/multithreadedJavascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Whether this runtime runs a Java->JS call on the Java thread that makes it, which is what
// the app's android.enableMultithreadedJavascript turns on. Found by trying it once: without
// it, a call from a background Java thread is posted to the runtime's own thread instead.
// The result is reported on the main looper, so Jasmine never continues on another thread.
var detected;

module.exports = function multithreadedJavascript() {
if (detected === undefined) {
detected = new Promise(function (resolve) {
var mainThread = java.lang.Thread.currentThread().getId();
var mainHandler = new android.os.Handler(android.os.Looper.getMainLooper());
new java.lang.Thread(new java.lang.Runnable({
run: function () {
var enabled = java.lang.Thread.currentThread().getId() !== mainThread;
mainHandler.post(new java.lang.Runnable({
run: function () {
resolve(enabled);
}
}));
}
})).start();
});
}
return detected;
};
11 changes: 11 additions & 0 deletions test-app/app/src/main/assets/app/tests/testEventLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ describe("event loop foreground tasks", function () {
// The ordered lane rides the Java MessageQueue, so these callbacks must be
// strict macrotasks: after the current turn's microtasks, FIFO with timers.
describe("event loop ordered macrotasks", function () {
let multithreaded;
beforeAll(function (done) {
require("./multithreadedJavascript")().then(function (enabled) {
multithreaded = enabled;
done();
});
});

it("__ns__queueMacrotask runs the callback asynchronously", function (done) {
let ran = false;
__ns__queueMacrotask(() => {
Expand Down Expand Up @@ -108,6 +116,9 @@ describe("event loop ordered macrotasks", function () {
});

it("runs on the main thread when posted from a background JS thread", function (done) {
if (!multithreaded) {
pending("needs android.enableMultithreadedJavascript");
}
const mainThreadId = java.lang.Thread.currentThread().getId();
new java.lang.Thread(new java.lang.Runnable({
run() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
describe("Test multithreaded javascript ", () => {
let multithreaded;
beforeAll(done => {
require("./multithreadedJavascript")().then(enabled => {
multithreaded = enabled;
done();
});
});

it("Should execute callbacks on specified native thread", done => {
if (!multithreaded) {
pending("needs android.enableMultithreadedJavascript");
}
const currentThreadId = java.lang.Thread.currentThread().getId();
new java.lang.Thread(new java.lang.Runnable({
run() {
Expand Down