Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test: verify bounded transfers in production containers

Richie McIlroyaaadf58d4ce9
diff --git a/.github/workflows/docker-build-media-server.yml b/.github/workflows/docker-build-media-server.yml
index 6a50d159a..ad4ef258c 100644
--- a/.github/workflows/docker-build-media-server.yml
+++ b/.github/workflows/docker-build-media-server.yml
@@ -27,6 +27,7 @@ jobs:
build:
name: Build Docker Image (${{ matrix.platform }})
runs-on: ${{ matrix.runner }}
+ timeout-minutes: 25
strategy:
matrix:
include:
@@ -79,6 +80,8 @@ jobs:
docker run --rm --network none --entrypoint bun "$MEDIA_IMAGE" test \
src/__tests__/lib/recording-verification.integration.test.ts \
src/__tests__/lib/job-manager.test.ts
+ docker run --rm --network none --entrypoint bun "$MEDIA_IMAGE" test \
+ src/__tests__/lib/media-transfer.test.ts
docker run --rm --network none --entrypoint bun "$MEDIA_IMAGE" test \
src/__tests__/routes/recording-verification.test.ts
docker run --rm --network none --entrypoint bun "$MEDIA_IMAGE" test \
@@ -97,6 +100,10 @@ jobs:
--entrypoint bun "$MEDIA_IMAGE" test \
src/__tests__/lib/recording-verification.integration.test.ts \
--test-name-pattern 'streams a complete long recording'
+ docker run --rm --network none --cpus 2 --memory 2g \
+ -e MEDIA_SERVER_TRANSFER_PERFORMANCE_TESTS=1 \
+ --entrypoint bun "$MEDIA_IMAGE" test \
+ src/__tests__/lib/media-transfer.integration.test.ts
- name: Export Digest
if: github.event_name != 'pull_request'
diff --git a/apps/media-server/Dockerfile b/apps/media-server/Dockerfile
index 892587d4f..c46db3cf6 100644
--- a/apps/media-server/Dockerfile
+++ b/apps/media-server/Dockerfile
@@ -12,6 +12,7 @@ RUN bun install --frozen-lockfile --production
COPY apps/media-server/src ./src
RUN bun test src/__tests__/lib/recording-verification.integration.test.ts src/__tests__/lib/job-manager.test.ts \
+ && bun test src/__tests__/lib/media-transfer.test.ts \
&& bun test src/__tests__/routes/recording-verification.test.ts \
&& bun test src/__tests__/routes/video.test.ts \
&& bun test src/__tests__/lib/media-routes-real-world.integration.test.ts \
@@ -21,6 +22,9 @@ RUN MEDIA_SERVER_RECORDING_PERFORMANCE_TESTS=1 bun test \
src/__tests__/lib/recording-verification.integration.test.ts \
--test-name-pattern 'streams a complete long recording'
+RUN MEDIA_SERVER_TRANSFER_PERFORMANCE_TESTS=1 bun test \
+ src/__tests__/lib/media-transfer.integration.test.ts
+
ENV PORT=3456
EXPOSE 3456
diff --git a/apps/media-server/src/__tests__/lib/media-transfer.integration.test.ts b/apps/media-server/src/__tests__/lib/media-transfer.integration.test.ts
new file mode 100644
index 000000000..47672e815
--- /dev/null
+++ b/apps/media-server/src/__tests__/lib/media-transfer.integration.test.ts
@@ -0,0 +1,81 @@
+import { expect, test } from "bun:test";
+import { createHash } from "node:crypto";
+import { mkdtemp, rm, stat } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { downloadDriveRevision } from "../../lib/media-transfer";
+
+test.skipIf(process.env.MEDIA_SERVER_TRANSFER_PERFORMANCE_TESTS !== "1")(
+ "streams and verifies a transfer larger than two GiB with bounded memory",
+ async () => {
+ const directory = await mkdtemp(join(tmpdir(), "cap-large-transfer-"));
+ const path = join(directory, "input.bin");
+ const chunk = Buffer.alloc(1024 ** 2, 0x5a);
+ const size = 2 * 1024 ** 3 + chunk.length;
+ const expected = createHash("sha256");
+ for (let offset = 0; offset < size; offset += chunk.length)
+ expected.update(chunk);
+ const sha256 = expected.digest("hex");
+ let requests = 0;
+ let transferred = 0;
+ let peakRss = process.memoryUsage().rss;
+ const timer = setInterval(() => {
+ peakRss = Math.max(peakRss, process.memoryUsage().rss);
+ }, 25);
+ const started = performance.now();
+ try {
+ const fetcher = Object.assign(
+ async () => {
+ requests++;
+ let sent = 0;
+ return new Response(
+ new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (sent === size) controller.close();
+ else {
+ controller.enqueue(chunk);
+ sent += chunk.length;
+ }
+ },
+ }),
+ { headers: { "Content-Length": String(size) } },
+ );
+ },
+ { preconnect: fetch.preconnect },
+ );
+ await downloadDriveRevision(
+ {
+ version: 1,
+ url: "https://www.googleapis.com/drive/v3/files/test/revisions/test?alt=media",
+ authorization: "Bearer synthetic-test",
+ objectIdentity: '"synthetic-large-revision"',
+ size,
+ sha256,
+ },
+ path,
+ {
+ fetcher,
+ onBytes: (bytes) => {
+ transferred += bytes;
+ },
+ },
+ );
+ const actual = createHash("sha256");
+ for await (const bytes of Bun.file(path).stream()) actual.update(bytes);
+ expect(actual.digest("hex")).toBe(sha256);
+ expect((await stat(path)).size).toBe(size);
+ expect(transferred).toBe(size);
+ expect(requests).toBe(1);
+ expect(peakRss).toBeLessThan(512 * 1024 ** 2);
+ console.info("[large-transfer-verification]", {
+ bytes: size,
+ elapsedMs: Math.round(performance.now() - started),
+ peakRssMB: Math.round(peakRss / 1024 ** 2),
+ });
+ } finally {
+ clearInterval(timer);
+ await rm(directory, { recursive: true, force: true });
+ }
+ },
+ 180_000,
+);