Skip to content
Merged
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
81 changes: 66 additions & 15 deletions src/daemon/relay-gateway.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { request as httpRequest } from "node:http";
import { createServer } from "node:net";
import { readdir } from "node:fs/promises";
import { connect, createServer } from "node:net";
import { join } from "node:path";

import { afterEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, onTestFinished } from "vitest";

import {
GatewayRelay,
Expand Down Expand Up @@ -513,10 +515,45 @@ describe("a gateway relaying to a worker", { timeout: 15_000 }, () => {
holder,
leaseId: grant.lease.id,
newLease,
secret,
stranger,
};
}

// why: a loaded machine can hand a request to the worker late; holding a connection's bytes back makes the upload reach the worker late, so the test must wait for it before it probes the name.
async function lateProxy(port: number, holdMs: number) {
const sockets = new Set<{ destroy: () => void }>();
const server = createServer((client) => {
sockets.add(client);
client.pause();
setTimeout(() => {
const upstream = connect(port, "127.0.0.1");
sockets.add(upstream);
client.pipe(upstream).pipe(client);
client.resume();
client.on("error", () => upstream.destroy());
upstream.on("error", () => client.destroy());
}, holdMs);
});
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
onTestFinished(
() =>
new Promise<void>((resolve) => {
server.close(() => resolve());
for (const socket of sockets) socket.destroy();
}),
);
return (server.address() as { port: number }).port;
}

// why: `feed.write` resolving only means fetch took the first byte; the worker's store holds the name once its `.upload-<id>` file exists.
async function uploadsOnWorker(directory: string) {
const root = join(directory, "lease-files");
const folders = await readdir(root).catch(() => []);
const entries = await Promise.all(folders.map((folder) => readdir(join(root, folder))));
return entries.flat().filter((entry) => entry.startsWith(".upload-")).length;
}

it("through a gateway, a lease holder's PUT, GET, list and DELETE reach the lease's worker and answer as on a worker", async () => {
const { call, channelsOpened, declare } = await fileRig({
fileBytes: 32 * MIB,
Expand Down Expand Up @@ -546,25 +583,12 @@ describe("a gateway relaying to a worker", { timeout: 15_000 }, () => {
),
);
const stored = await (await call("")).json();
const busy = new TransformStream<Uint8Array>();
const holding = call("/busy.bin", {
body: busy.readable,
duplex: "half",
method: "PUT",
} as RequestInit);
const feed = busy.writable.getWriter();
await feed.write(new Uint8Array(1));
await poll(async () => (await call("/busy.bin", { method: "PUT", body: "x" })).status).toBe(
409,
);
const missing = await call("/missing.bin");
const oversized = await call("/large.bin", {
body: patternStream(33 * MIB, 6),
duplex: "half",
method: "PUT",
} as RequestInit);
await feed.close();
await holding;
const opened = channelsOpened();
const tooLarge = await declare("/huge.bin", 2 * 1024 * MIB + 1);

Expand Down Expand Up @@ -595,6 +619,33 @@ describe("a gateway relaying to a worker", { timeout: 15_000 }, () => {
expect(channelsOpened()).toBe(opened);
}, 120_000);

it("through a gateway, a PUT of a name whose upload is still arriving gets 409 FILE_BUSY, once that upload has reached the worker late and is still arriving", async () => {
const rig = await fileRig({ fileBytes: 32 * MIB, leaseBytes: 512 * MIB, silenceMs: 330_000 });
const late = await lateProxy(rig.port, 500);
const busy = new TransformStream<Uint8Array>();
const holding = fetch(
`http://127.0.0.1:${String(late)}/v1/leases/${rig.leaseId}/files/busy.bin`,
{
body: busy.readable,
duplex: "half",
headers: { authorization: `Bearer ${rig.secret}` },
method: "PUT",
} as RequestInit,
);
const feed = busy.writable.getWriter();
await feed.write(new Uint8Array(1));
await poll(() => uploadsOnWorker(rig.worker.directory)).toBe(1);

const second = await rig.call("/busy.bin", { body: "x", method: "PUT" });
await feed.close();
const first = await holding;

expect(second.status).toBe(409);
expect(await second.json()).toMatchObject({ error: { code: "FILE_BUSY", name: "busy.bin" } });
expect(first.status).toBe(201);
expect(await first.json()).toEqual({ name: "busy.bin", size: 1 });
}, 30_000);

it("through a gateway, a caller that does not hold the lease gets 404 UNKNOWN_LEASE, and a lease whose worker is gone gets 503 WORKER_UNREACHABLE", async () => {
const rig = await fileRig({ fileBytes: 32 * MIB, leaseBytes: MIB, silenceMs: 330_000 });
const asStranger = await rig.stranger();
Expand Down
Loading