peppa/packages/testkit/test/gitea-timeout.test.ts

58 lines
2.6 KiB
TypeScript

// PEP-282: a request to Gitea times out by the time the process was responsive. A synchronous
// pause (git of the mock, SQLite, module loading on a loaded CI runner) must not abort a
// request whose answer is already there; a Gitea that never answers is still cut off.
import assert from "node:assert/strict";
import { createServer, type Server } from "node:http";
import type { AddressInfo } from "node:net";
import { after, before, describe, test } from "node:test";
import { GiteaClient, GiteaError, responsiveTimeout } from "@peppa/core";
const busy = (ms: number) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
describe("Таймаут запроса к Gitea не съедает пауза процесса", () => {
let server: Server;
let url = "";
before(async () => {
server = createServer((req, res) => {
if (req.url!.includes("/hang")) return; // accepts and never answers
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ id: 1, login: "peppa-bot" }));
});
await new Promise<void>((r) => server.listen(0, "127.0.0.1", r));
url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
process.env.PEPPA_GITEA_TIMEOUT_MS = "300";
});
after(async () => {
delete process.env.PEPPA_GITEA_TIMEOUT_MS;
server.closeAllConnections();
await new Promise<void>((r) => server.close(() => r()));
});
test("Scenario: Процесс занят, ответ уже пришёл", async () => {
const gitea = new GiteaClient(url, "t");
await gitea.me(); // a kept-alive connection, as in a long test file
const pending = gitea.me();
busy(800); // the process is busy longer than the whole timeout
assert.equal((await pending).login, "peppa-bot");
// The wall-clock timer this replaces loses the same race (the cause of the CI failures).
const wall = fetch(`${url}/api/v1/user`, { signal: AbortSignal.timeout(300) }).then((r) => r.text());
busy(800);
await assert.rejects(wall, { name: "TimeoutError" });
});
test("Scenario: Gitea не отвечает", async () => {
const gitea = new GiteaClient(url, "t");
const t0 = Date.now();
await assert.rejects(gitea.request("GET", "/hang"), (e: Error) => e.name === "TimeoutError" && !(e instanceof GiteaError));
const took = Date.now() - t0;
assert.ok(took >= 250 && took < 2000, `cut off after ${took} ms`);
});
test("done() stops counting: no abort after the answer", async () => {
const t = responsiveTimeout(100);
t.done();
await new Promise((r) => setTimeout(r, 250));
assert.equal(t.signal.aborted, false);
});
});