Keep tracker shares alive for 6 minutes, then let them go
Both default trackers closed the socket of a peer that announced only once, after about 2 and 3 minutes, and the card then wrongly said no tracker was reachable. Sender and receiver now announce again on the interval the tracker names (at least 30 s apart, whatever it names), the receiver with its same offer. A share, or a receiver looking for one, gives up after 6 minutes without a download: its tracker sockets close and the card says nobody came. A peer connected by then still gets its turn, the transfer itself has no time limit. The sender's code shows until when it is valid and is marked no longer valid, without Copy, once it expired or the share failed. A receiver that lost every tracker now closes its peer connections too, and the expiry timer goes with the session when it is cancelled, fails or the download starts. Tests fast-forward the page clock: re-announcing and expiry on both sides against mocked trackers, no timer or connection left after cancelled and failed sessions, and a live download after the deadline passed during the security check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a35bd71ca7
commit
2167b528b2
5 files changed
+242
-21
No files matched your search
+138
-2
@@ -687,6 +687,34 @@ for (const name of Object.keys(BROWSERS)) {
|
||||
});
|
||||
});
|
||||
|
||||
// The 6 minutes only limit finding each other, a receiver connected by then still gets the file.
|
||||
test(`live tracker: ${name} a receiver connected before the expiry downloads after it`, async () => {
|
||||
const [sender, receiver] = await Promise.all([open(name), open(name)]);
|
||||
await Promise.all([sender, receiver].map((page) => page.clock.install()));
|
||||
const source = await payload(randomBytes(SIZE));
|
||||
|
||||
await withLogs(sender, receiver, async () => {
|
||||
await sender.setInputFiles('input[type=file]', source);
|
||||
await MODES.tracker!(sender, receiver);
|
||||
await expect(receiver.locator('#check-local')).toBeVisible({ timeout: 45_000 });
|
||||
await Promise.all([sender, receiver].map((page) => page.clock.fastForward('06:00')));
|
||||
for (const page of [sender, receiver]) {
|
||||
await expect.poll(() => sockets.get(page)!.filter((ws) => !ws.isClosed()).length).toBe(0);
|
||||
}
|
||||
await expect(sender.locator('label[for=send-code]')).toHaveText('Code, no longer valid:');
|
||||
|
||||
const started = downloadStarts(receiver);
|
||||
await sender.click('#confirm');
|
||||
await receiver.click('#confirm');
|
||||
await receiver.click('#download');
|
||||
const { download } = await started;
|
||||
expect(await hash(await download.path())).toBe(await hash(source));
|
||||
await expect(sender.locator('#log')).toContainText('delivered');
|
||||
await expect(sender.locator('#send-error')).toHaveCount(0);
|
||||
await expect(receiver.locator('#receive-error')).toHaveCount(0);
|
||||
});
|
||||
});
|
||||
|
||||
// Trackers that keep the socket open but turn the announcement down used to leave both sides waiting.
|
||||
test(`tracker: ${name} reports trackers that reject the announcement`, async () => {
|
||||
const [sender, receiver] = await Promise.all([open(name), open(name)]);
|
||||
@@ -727,8 +755,93 @@ for (const name of Object.keys(BROWSERS)) {
|
||||
await expect(sender.locator('#log')).toContainText(': slow down');
|
||||
await expect(sender.locator('#log')).not.toContainText('rejected by');
|
||||
await expect(sender.locator('#send-error')).toHaveCount(0);
|
||||
// One per tracker socket, none for the closed connections.
|
||||
expect(await listeners()).toBe(2);
|
||||
// One per tracker socket and the expiry, none for the closed connections.
|
||||
expect(await listeners()).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
// Trackers drop a peer that doesn't announce again on their interval, the defaults after 2 to 3
|
||||
// minutes. A code nobody uses still ends after 6 minutes, on both sides.
|
||||
test(`tracker: ${name} both sides announce again until they give up after 6 minutes`, async () => {
|
||||
const pages = await Promise.all([open(name), open(name)]);
|
||||
const [sender, receiver] = pages as [Page, Page];
|
||||
const announced = pages.map(() => [] as Record<string, any>[]);
|
||||
// 0 would announce in a loop, the app waits 30 s at least.
|
||||
const trackers = await Promise.all(
|
||||
pages.map(async (page, i) => {
|
||||
await page.clock.install();
|
||||
return mockTrackers(page, (msg) => {
|
||||
announced[i]!.push(msg);
|
||||
return { action: 'announce', interval: 0, info_hash: msg.info_hash };
|
||||
});
|
||||
}),
|
||||
);
|
||||
const again = () => announced.map((msgs) => msgs.filter((msg) => !msg.event));
|
||||
const fastForward = (ms: number) => Promise.all(pages.map((page) => page.clock.fastForward(ms)));
|
||||
|
||||
await withLogs(sender, receiver, async () => {
|
||||
await sender.setInputFiles('input[type=file]', await payload(randomBytes(1000)));
|
||||
await sender.click('#share');
|
||||
await expect(sender.locator('label[for=send-code]')).toHaveText(/valid until \d/);
|
||||
await receiver.fill('#receive-code', (await sender.locator('#send-code').textContent())!);
|
||||
await receiver.click('#receive');
|
||||
await expect.poll(() => announced.map((msgs) => msgs.length)).toEqual([2, 2]);
|
||||
// Until the replies with the interval reached the pages, and any early announce the test.
|
||||
await sender.waitForTimeout(500);
|
||||
await fastForward(25_000);
|
||||
await sender.waitForTimeout(500);
|
||||
expect(again().map((msgs) => msgs.length)).toEqual([0, 0]);
|
||||
await fastForward(5_000);
|
||||
await expect.poll(() => again().map((msgs) => msgs.length)).toEqual([2, 2]);
|
||||
// The receiver's offers again, a busy sender may have dropped them.
|
||||
const offers = announced[1]!.filter((msg) => msg.event === 'started').map((msg) => msg.offers[0].offer_id);
|
||||
expect(again()[1]!.map((msg) => msg.offers[0].offer_id).sort()).toEqual(offers.sort());
|
||||
|
||||
await fastForward(330_000);
|
||||
await expect(sender.locator('#send-error')).toHaveText('nobody received the file within 6 minutes, share it again');
|
||||
await expect(sender.locator('label[for=send-code]')).toHaveText('Code, no longer valid:');
|
||||
await expect(sender.locator('#copy-send-code')).toBeDisabled();
|
||||
await expect(receiver.locator('#receive-error')).toHaveText(
|
||||
'no sender answered within 6 minutes, check the code and try again',
|
||||
);
|
||||
await expect.poll(() => trackers.map(({ open }) => open)).toEqual([0, 0]);
|
||||
await expectClosed(sender);
|
||||
await expectClosed(receiver);
|
||||
});
|
||||
});
|
||||
|
||||
// Each expiry used to hold its session until it ran out, a failed receiver its connections.
|
||||
test(`tracker: ${name} sessions that ended keep no expiry and no connection`, async () => {
|
||||
const pages = await Promise.all([open(name, { init: countDeadlines }), open(name, { init: countDeadlines })]);
|
||||
const [sender, receiver] = pages as [Page, Page];
|
||||
let rejecting = false;
|
||||
await Promise.all(
|
||||
pages.map((page) =>
|
||||
mockTrackers(page, ({ info_hash }) =>
|
||||
rejecting ? { 'failure reason': 'not here' } : { action: 'announce', interval: 120, info_hash },
|
||||
),
|
||||
),
|
||||
);
|
||||
const deadlines = () =>
|
||||
Promise.all(pages.map((page) => page.evaluate(() => (window as unknown as { deadlines: () => number }).deadlines())));
|
||||
|
||||
await withLogs(sender, receiver, async () => {
|
||||
await sender.setInputFiles('input[type=file]', await payload(randomBytes(1000)));
|
||||
for (let session = 0; session < 5; session++) {
|
||||
await sender.click('#share');
|
||||
await receiver.fill('#receive-code', `nobody ${session}`);
|
||||
await receiver.click('#receive');
|
||||
}
|
||||
await expect.poll(deadlines).toEqual([1, 1]);
|
||||
|
||||
rejecting = true;
|
||||
await sender.click('#share');
|
||||
await receiver.click('#receive');
|
||||
await expect(sender.locator('#send-error')).toHaveText(/^no tracker accepted it/);
|
||||
await expect(receiver.locator('#receive-error')).toHaveText(/^no tracker accepted it/);
|
||||
await expect.poll(deadlines).toEqual([0, 0]);
|
||||
await expectClosed(sender);
|
||||
await expectClosed(receiver);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -871,6 +984,29 @@ function countAbortListeners() {
|
||||
(window as unknown as { abortListeners: () => number }).abortListeners = () => listeners.size;
|
||||
}
|
||||
|
||||
// Init script: window.deadlines() counts the core's pending TRACKER_WAIT timers, a session's expiry.
|
||||
// The app's own one for the code is left out. It runs in the hidden frames too, only the app's own
|
||||
// window counts.
|
||||
function countDeadlines() {
|
||||
if (window !== window.top) return;
|
||||
const pending = new Set<number>();
|
||||
const { setTimeout: set, clearTimeout: clear } = window;
|
||||
window.setTimeout = ((handler: () => void, ms?: number) => {
|
||||
if (ms !== 6 * 60_000 || !new Error().stack?.includes('/src/core.ts')) return set(handler, ms);
|
||||
const id = set(() => {
|
||||
pending.delete(id);
|
||||
handler();
|
||||
}, ms);
|
||||
pending.add(id);
|
||||
return id;
|
||||
}) as typeof setTimeout;
|
||||
window.clearTimeout = ((id?: number) => {
|
||||
pending.delete(id!);
|
||||
clear(id);
|
||||
}) as typeof clearTimeout;
|
||||
(window as unknown as { deadlines: () => number }).deadlines = () => pending.size;
|
||||
}
|
||||
|
||||
for (const name of Object.keys(BROWSERS)) {
|
||||
test(`large: ${name} survives a 40 s stall`, async () => {
|
||||
test.skip(!process.env.LARGE, 'set LARGE=1');
|
||||
|
||||
Reference in new issue
Block a user