Workers for chunk generation !

This commit is contained in:
2026-10-11 01:15:14 -03:00
parent 3d2b235f64
commit cd3b39c3d7
24 changed files with 1119 additions and 91 deletions
+64
View File
@@ -10,6 +10,7 @@ import { create_mod } from "$/tools/new_mod.ts";
import { load_bmods } from "$/server/load_bmods.ts";
import { start_host } from "$/server/host.ts";
import { copy_dir } from "./helpers.ts";
import { PROTOCOL_VERSION } from "$/common/protocol.ts";
const ENGINE_TEXTURES = ["engine:missing", ...Array.from({ length: 9 }, (_, i) => `engine:break_${i}`)];
@@ -260,6 +261,69 @@ Deno.test("each texture's border in the atlas repeats its edge, so lookups at th
]);
});
Deno.test({
name: "a player joining a real host gets chunks the worldgen workers made, and /tps says they're in use",
// the host's server and its workers outlive the sanitizers until they're shut down
sanitizeOps: false,
sanitizeResources: false,
fn: async () => {
const { sources, folder } = await packed_mods();
const build = Deno.makeTempDirSync({ prefix: "bworld_build_" });
Deno.mkdirSync(`${build}/assets/textures`, { recursive: true });
Deno.writeTextFileSync(
`${build}/assets/textures/index.json`,
JSON.stringify(ENGINE_TEXTURES.slice(1).map((id) => id.split(":")[1])),
);
const host = await start_host({
build_dir: build,
server_mods_dir: folder,
world_dir: `${build}/world`,
seed: "workers-seed",
worldgen_workers: 2,
on_fatal: (message) => {
throw new Error(message);
},
});
const server = Deno.serve({ port: 0, onListen: () => {} }, host.handle);
try {
const socket = new WebSocket(`ws://localhost:${server.addr.port}/ws`);
socket.binaryType = "arraybuffer";
let chunks = 0;
const said: string[] = [];
const done = new Promise<void>((resolve, reject) => {
const timer = setTimeout(() => reject(new Error(`only ${chunks} chunks came`)), 60_000);
socket.onmessage = (event) => {
if (typeof event.data !== "string") {
chunks++;
return;
}
const message = JSON.parse(event.data);
if (message.type === "welcome") socket.send(JSON.stringify({ type: "ready", view_distance: 4 }));
if (message.type === "chunk_batch_finished") {
socket.send(JSON.stringify({ type: "chunk_batch_received", chunks_per_tick: 64 }));
if (chunks >= 49) socket.send(JSON.stringify({ type: "chat", text: "/tps" }));
}
if (message.type === "chat") said.push(message.text);
if (said.some((text) => text.startsWith("Worldgen:"))) {
clearTimeout(timer);
resolve();
}
};
});
socket.onopen = () =>
socket.send(JSON.stringify({ type: "hello", name: "alice", protocol: PROTOCOL_VERSION }));
await done;
socket.close();
assert(chunks >= 49, `${chunks} chunks`);
assertStringIncludes(said.find((text) => text.startsWith("Worldgen:"))!, "Worldgen: 2 workers");
} finally {
await server.shutdown();
await host.shutdown();
for (const dir of [sources, folder, build]) Deno.removeSync(dir, { recursive: true });
}
},
});
// write_bmod without anything optional still makes a file read_bmod takes
Deno.test("a mod with only data packs and reads", () => {
const bmod = {