Files
Netcatty/application/state/sftp/transferConnectionPool.test.ts
T
bincxz 3f706abfeb feat(ssh): unify transport leases and converge transfer keep-alive
Introduce a main-process SSH transport registry (borrow/return leases,
ControlPersist-style idle park) shared by shell, SFTP, transfer, and
port-forward. Transfer pool becomes channel concurrency only; remove the
separate transfer-pool idle TTL UI in favor of a single SSH keep-alive
setting. Register dedicated SFTP dials, reuse parked transports, and fix
TTL 0 (park until quit), jump-host endpoint keys, and remote unforwardIn.
2026-07-28 18:57:14 +08:00

206 lines
6.3 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import {
buildTransferPoolKey,
createTransferConnectionPool,
DEFAULT_TRANSFER_CONNECTIONS_PER_HOST,
} from "./transferConnectionPool.ts";
test("buildTransferPoolKey includes endpoint when hostname is known", () => {
assert.equal(
buildTransferPoolKey({ hostId: "h1", hostname: "vault.example", port: 22, username: "root" }),
"host:h1|ep:vault.example:22:root:ssh:nosudo",
);
// Same hostId with session override must not share the vault pool key.
assert.equal(
buildTransferPoolKey({ hostId: "h1", hostname: "override.example", port: 2222, username: "ubuntu" }),
"host:h1|ep:override.example:2222:ubuntu:ssh:nosudo",
);
assert.equal(
buildTransferPoolKey({ hostname: "ci.example", port: 22, username: "root" }),
"ep:ci.example:22:root:ssh:nosudo",
);
assert.equal(buildTransferPoolKey({ hostId: "h1" }), "host:h1");
});
test("pool opens at most maxPerHost channels and multiplexes when busy", async () => {
let opens = 0;
const closed: string[] = [];
const pool = createTransferConnectionPool({
maxPerHost: 2,
closeSession: async (id) => { closed.push(id); },
});
const open = async () => {
opens += 1;
return `sftp-${opens}`;
};
const a = await pool.acquire("host:a", "t1", open);
const b = await pool.acquire("host:a", "t2", open);
assert.equal(opens, 2);
assert.notEqual(a.sftpId, b.sftpId);
// Third transfer reuses least-loaded connection (both size 1 → first by age).
const c = await pool.acquire("host:a", "t3", open);
assert.equal(opens, 2);
assert.ok(c.sftpId === a.sftpId || c.sftpId === b.sftpId);
a.release();
b.release();
c.release();
// Last holder closes each channel immediately (SSH park owns keep-alive).
assert.equal(closed.length, 2);
assert.equal(pool.getStats("host:a").connections, 0);
const d = await pool.acquire("host:a", "t4", open);
assert.equal(opens, 3);
d.release();
assert.equal(closed.length, 3);
});
test("different hosts get independent channel pools", async () => {
let opens = 0;
const pool = createTransferConnectionPool({ maxPerHost: 1 });
const open = async () => {
opens += 1;
return `sftp-${opens}`;
};
const a = await pool.acquire("host:a", "t1", open);
const b = await pool.acquire("host:b", "t2", open);
assert.equal(opens, 2);
assert.notEqual(a.sftpId, b.sftpId);
a.release();
b.release();
});
test("release of last holder closes the channel without waiting for idle TTL", async () => {
const closed: string[] = [];
const pool = createTransferConnectionPool({
maxPerHost: 2,
closeSession: async (id) => { closed.push(id); },
});
const open = async () => "sftp-busy";
const lease = await pool.acquire("host:x", "t1", open);
assert.equal(await pool.closeIdle(), 0, "busy slot must not close while held");
lease.release();
assert.equal(closed.length, 1);
assert.equal(pool.getStats("host:x").connections, 0);
// closeIdle is a defensive sweep; nothing left.
assert.equal(await pool.closeIdle(), 0);
});
test("default max per host is FileZilla-like (2)", () => {
assert.equal(DEFAULT_TRANSFER_CONNECTIONS_PER_HOST, 2);
});
test("concurrent acquires do not exceed maxPerHost", async () => {
let opens = 0;
let inFlightOpens = 0;
let maxInFlightOpens = 0;
const pool = createTransferConnectionPool({ maxPerHost: 2 });
const open = async () => {
inFlightOpens += 1;
maxInFlightOpens = Math.max(maxInFlightOpens, inFlightOpens);
await new Promise((r) => setTimeout(r, 10));
opens += 1;
inFlightOpens -= 1;
return `sftp-${opens}`;
};
const leases = await Promise.all([
pool.acquire("host:a", "t1", open),
pool.acquire("host:a", "t2", open),
pool.acquire("host:a", "t3", open),
pool.acquire("host:a", "t4", open),
]);
assert.equal(opens, 2);
assert.ok(maxInFlightOpens <= 2);
const ids = new Set(leases.map((l) => l.sftpId));
assert.equal(ids.size, 2);
for (const lease of leases) lease.release();
});
test("busy first channel causes a second open (FileZilla style)", async () => {
let opens = 0;
const pool = createTransferConnectionPool({ maxPerHost: 2 });
const open = async () => {
opens += 1;
return `sftp-${opens}`;
};
const first = await pool.acquire("host:a", "t1", open);
assert.equal(opens, 1);
// First is still held → open a second dedicated channel.
const second = await pool.acquire("host:a", "t2", open);
assert.equal(opens, 2);
assert.notEqual(first.sftpId, second.sftpId);
first.release();
second.release();
});
test("discard removes a dead session so next acquire reopens", async () => {
let opens = 0;
const closed: string[] = [];
const pool = createTransferConnectionPool({
maxPerHost: 1,
closeSession: async (id) => { closed.push(id); },
});
const open = async () => {
opens += 1;
return `sftp-${opens}`;
};
const a = await pool.acquire("host:a", "t1", open);
assert.equal(opens, 1);
a.discard();
assert.equal(closed.length, 1);
assert.equal(pool.getStats("host:a").connections, 0);
const b = await pool.acquire("host:a", "t2", open);
assert.equal(opens, 2);
assert.notEqual(a.sftpId, b.sftpId);
b.release();
});
test("closeIdle detaches leftover idle slots before closing", async () => {
let opens = 0;
let closeStarted = 0;
let releaseClose!: () => void;
const closeGate = new Promise<void>((resolve) => { releaseClose = resolve; });
const pool = createTransferConnectionPool({
maxPerHost: 1,
closeSession: async () => {
closeStarted += 1;
await closeGate;
},
});
const open = async () => {
opens += 1;
return `sftp-${opens}`;
};
// Manually inject an idle leftover by discarding holders via internal release
// path: acquire then release closes immediately, so inject by not releasing
// and using closeIdle only works if holders===0. Force via release.
const a = await pool.acquire("host:a", "t1", open);
assert.equal(opens, 1);
// Release closes async; wait a tick then ensure pool empty.
a.release();
await new Promise((r) => setTimeout(r, 0));
assert.ok(closeStarted >= 1);
releaseClose();
});
test("setIdleTtlMs is a no-op for unified transport park", () => {
const pool = createTransferConnectionPool();
pool.setIdleTtlMs(60_000);
assert.equal(pool.getIdleTtlMs(), 0);
});