Skip to content

Commit

Permalink
Set timeout on check
Browse files Browse the repository at this point in the history
  • Loading branch information
heeckhau committed Feb 23, 2024
1 parent 621193d commit 8ddc741
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/mocha": "^10.0.6",
"@types/serve-handler": "^6.1.4",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "7.0.2",
"browserify": "^17.0.0",
"concurrently": "^5.1.0",
"constants-browserify": "^1.0.0",
Expand Down
44 changes: 30 additions & 14 deletions test/testRunner.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import puppeteer, { PuppeteerLaunchOptions } from 'puppeteer';
import puppeteer, { Browser, Page, PuppeteerLaunchOptions } from 'puppeteer';
import { describe, it, before, after } from 'mocha';
const assert = require('assert');
const { exec } = require('node:child_process');
import { exec, ChildProcess } from 'node:child_process';

const timeout = 60000;

// puppeteer options
let opts: PuppeteerLaunchOptions = {
headless: !!process.env.HEADLESS ? "new" : false,
headless: !!process.env.HEADLESS ? 'new' : false,
slowMo: 100,
timeout: 60000,
timeout: timeout,
};

if (process.env.CHROME_PATH) {
opts = {
...opts,
executablePath: process.env.CHROME_PATH
}
};

executablePath: process.env.CHROME_PATH,
};
}

console.log("puppeteer options", opts)
console.log('puppeteer options', opts);

let browser: any, page: any, server: any;
let browser: Browser;
let page: Page;
let server: ChildProcess;

// expose variables
before(async function () {
Expand Down Expand Up @@ -62,10 +65,23 @@ describe('tlsn-js test suite', function () {
});

async function check(testId: string): Promise<string> {
const content = await page.$eval('#' + testId, (n: any) => n.innerText);
if (content) return content;
await new Promise((r) => setTimeout(r, 1000));
return check(testId);
const startTime = Date.now();
const attemptFetchContent = async (): Promise<string> => {
const content = await page.$eval(
`#${testId}`,
(el: Element) => el.textContent || '',
);
if (content) return content;
const elapsedTime = Date.now() - startTime;
if (elapsedTime >= timeout) {
throw new Error(
`Timeout: Failed to retrieve content for '#${testId}' within ${timeout} ms.`,
);
}
await new Promise((resolve) => setTimeout(resolve, 1000));
return attemptFetchContent();
};
return attemptFetchContent();
}

function safeParseJson(data: string): any | null {
Expand Down

0 comments on commit 8ddc741

Please sign in to comment.