From fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:14:29 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- .../promiseworker/tests/xpcshell/data/worker.js | 13 +++++- .../promiseworker/tests/xpcshell/test_Promise.js | 48 +++++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) (limited to 'toolkit/components/promiseworker/tests/xpcshell') diff --git a/toolkit/components/promiseworker/tests/xpcshell/data/worker.js b/toolkit/components/promiseworker/tests/xpcshell/data/worker.js index 30087bdc4a..94b6dd17ad 100644 --- a/toolkit/components/promiseworker/tests/xpcshell/data/worker.js +++ b/toolkit/components/promiseworker/tests/xpcshell/data/worker.js @@ -12,8 +12,9 @@ importScripts("resource://gre/modules/workers/require.js"); var PromiseWorker = require("resource://gre/modules/workers/PromiseWorker.js"); var worker = new PromiseWorker.AbstractWorker(); -worker.dispatch = function (method, args = []) { - return Agent[method](...args); + +worker.dispatch = async function (method, args = []) { + return await Agent[method](...args); }; worker.postMessage = function (...args) { self.postMessage(...args); @@ -34,6 +35,14 @@ var Agent = { return args; }, + async bounceWithExtraCalls(...args) { + let result = await worker.callMainThread("echo", [ + "Posting something unrelated", + ]); + args.push(result.ok); + return args; + }, + throwError(msg, ...args) { throw new Error(msg); }, diff --git a/toolkit/components/promiseworker/tests/xpcshell/test_Promise.js b/toolkit/components/promiseworker/tests/xpcshell/test_Promise.js index f7581b664f..f9091a2b85 100644 --- a/toolkit/components/promiseworker/tests/xpcshell/test_Promise.js +++ b/toolkit/components/promiseworker/tests/xpcshell/test_Promise.js @@ -16,7 +16,22 @@ const { setTimeout } = ChromeUtils.importESModule( var WORKER_SOURCE_URI = "chrome://promiseworker/content/worker.js"; do_load_manifest("data/chrome.manifest"); -var worker = new BasePromiseWorker(WORKER_SOURCE_URI); + +const UUID = crypto.randomUUID(); + +const SOME_ARRAY = new Uint8Array(4); +for (let i = 0; i < 4; ++i) { + SOME_ARRAY[i] = i; +} + +async function echo(message) { + return new BasePromiseWorker.Meta([message, UUID, SOME_ARRAY.buffer], { + transfers: [SOME_ARRAY.buffer], + }); +} + +var worker = new BasePromiseWorker(WORKER_SOURCE_URI, {}, { echo }); + worker.log = function (...args) { info("Controller: " + args.join(" ")); }; @@ -166,3 +181,34 @@ add_task(async function test_terminate() { "ChromeWorker instances should differ" ); }); + +function cloneArrayBuffer(original) { + const clone = new ArrayBuffer(original.byteLength); + const originalView = new Uint8Array(original); + const cloneView = new Uint8Array(clone); + cloneView.set(originalView); + return clone; +} + +add_task(async function test_bidirectional() { + // Before we transfer the array, we clone it + const arrayCopy = cloneArrayBuffer(SOME_ARRAY.buffer); + + let message = ["test_simple_args", Math.random()]; + + // Checking the array buffer size + Assert.equal( + SOME_ARRAY.buffer.byteLength, + 4, + "The buffer is not detached yet" + ); + let result = await worker.post("bounceWithExtraCalls", message); + + // After the post call, the array was transferred and SOME_ARRAY should be empty + Assert.equal(SOME_ARRAY.buffer.byteLength, 0, "The buffer has been detached"); + + // The echo() function in the worker adds to the message a string, an uuid and has the transferred array + message.push(["Posting something unrelated", UUID, arrayCopy]); + + Assert.deepEqual(result, message); +}); -- cgit v1.2.3