Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Fail a worker load whose reply has no asset (part of #776)

Nguyen Xuan An46f61ee0d6cf
diff --git a/src/streaming.js b/src/streaming.js
index 01178b98..5cd8e5b6 100644
--- a/src/streaming.js
+++ b/src/streaming.js
@@ -121,6 +121,8 @@ export function createWorkerLoader({ createWorker, dispose } = {}) {
fail(new TypeError(`The worker for "${id}" must answer { asset, bytes } or { error }, got ${describe(reply)}.`));
} else if (reply.error !== undefined) {
fail(new Error(String(reply.error)));
+ } else if (reply.asset === undefined) {
+ fail(new TypeError(`The worker for "${id}" answered without an asset; answer { asset, bytes } or { error }.`));
} else {
succeed({ asset: reply.asset, bytes: reply.bytes });
}
@@ -271,7 +273,7 @@ export function createStreaming(options = {}) {
emit('evict', record.id, level, { reason, bytes: resident.bytes });
}
- /** Free least recently used assets nobody wants until `neededBytes` fit. Returns whether they fit. */
+ /** Free assets nobody wants, in the chosen eviction order, until `neededBytes` fit. Returns whether they fit. */
function makeRoom(neededBytes) {
let used = bytesInUse();
if (used + neededBytes <= budgetBytes) return true;
diff --git a/tests/streaming.test.js b/tests/streaming.test.js
index 4ce48507..5d85c923 100644
--- a/tests/streaming.test.js
+++ b/tests/streaming.test.js
@@ -308,6 +308,13 @@ test('a createWorker that throws fails the load', async () => {
await assert.rejects(loader.load({ id: 'a', format: 'tile', source: 'a.bin', level: 0, signal: new AbortController().signal }), /no worker/);
});
+test('a worker reply without an asset fails the load', async () => {
+ const { workers, createWorker } = fakeWorkerFactory(worker => worker.onmessage({ data: { bytes: 64 } }));
+ const loader = createWorkerLoader({ createWorker });
+ await assert.rejects(loader.load({ id: 'a', format: 'tile', source: 'a.bin', level: 0, signal: new AbortController().signal }), /answered without an asset/);
+ assert.ok(workers[0].terminated);
+});
+
test('an unreadable worker reply fails the load and frees the slot', async () => {
const { workers, createWorker } = fakeWorkerFactory(worker => worker.onmessageerror({}));
const streaming = createStreaming({ budgetBytes: 1000, loaders: { tile: createWorkerLoader({ createWorker }) } });