Skip to content

Commit

Permalink
Avoid flakes in download test
Browse files Browse the repository at this point in the history
  • Loading branch information
davidje13 committed Jul 25, 2024
1 parent 1c8f639 commit 530d266
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions e2e/helpers/downloads.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fileURLToPath } from 'node:url';
import { join, dirname } from 'node:path';
import { readFile, mkdir, rm } from 'node:fs/promises';
import { readFile, mkdir, rm, stat } from 'node:fs/promises';

export const downloadDir = join(
dirname(fileURLToPath(import.meta.url)),
Expand All @@ -11,19 +11,25 @@ export const downloadDir = join(
await rm(downloadDir, { recursive: true, force: true });
await mkdir(downloadDir, { recursive: true });

const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms));

export async function waitForFile(
name: string,
minimumSize: number,
timeout: number,
): Promise<string> {
const fileName = join(downloadDir, name);
const exp = Date.now() + timeout;

do {
try {
return await readFile(fileName, { encoding: 'utf-8' });
} catch (e) {
await new Promise((res) => setTimeout(res, 100));
}
const { size } = await stat(fileName);
if (size >= minimumSize) { // wait until file has some content
await sleep(10); // wait a little longer to avoid partial file reads if we get unlucky with the timing
return await readFile(fileName, { encoding: 'utf-8' });
}
} catch {}
await sleep(100);
} while (Date.now() < exp);

throw new Error(`Failed to download file ${name} within ${timeout}ms`);
Expand Down
2 changes: 1 addition & 1 deletion e2e/pages/RetroArchiveList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class RetroArchiveList extends Page {

public async clickExportJson(): Promise<string> {
await this.click(By.linkText('Export as JSON'));
return waitForFile(`${this.slug}-export.json`, this.explicitWaitTimeout);
return waitForFile(`${this.slug}-export.json`, 10, this.explicitWaitTimeout);
}

public async getArchiveLabels(): Promise<string[]> {
Expand Down

0 comments on commit 530d266

Please sign in to comment.