Skip to content

Commit

Permalink
[patch] More test fixes (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmisty authored Jun 26, 2024
1 parent d998e60 commit e56b333
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 9 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"debug": "4.3.5",
"events": "3.3.0",
"fast-glob": "3.3.2",
"net": "^1.0.2",
"uuid": "10.0.0",
"uuid-by-string": "4.0.0",
"ws": "8.17.1"
Expand Down
76 changes: 69 additions & 7 deletions src/plugins/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import PluginConfigOptions = Cypress.PluginConfigOptions;
import { RawData, WebSocketServer } from 'ws';
import net from 'net';
import { ENV_WS, packageLog, wsPath } from '../common';
import Debug from 'debug';
import { AllureTasks, RequestTask } from '../plugins/allure-types';
Expand All @@ -18,11 +19,70 @@ const messageGot = (...args: unknown[]) => {
logMessage(`${args}`);
};

function getRandomPortNumber(): number {
return 40000 + Math.round(Math.random() * 25000);
const checkPortSync = (port: number, timeoutMs = 2000): boolean => {
let isAvailable = true;
let server: net.Server | null = null;
let timeoutReached = false;
const startTime = Date.now();

try {
server = net.createServer();
server.listen(port);

server.on('error', (err: NodeJS.ErrnoException) => {
if (err.code === 'EADDRINUSE') {
isAvailable = false;
}
});

const checkTimeout = () => {
if (Date.now() - startTime >= timeoutMs) {
timeoutReached = true;
}
};

const waitForListening = () => {
if (!server?.listening && !timeoutReached) {
process.nextTick(waitForListening); // Yield to event loop
checkTimeout(); // Check if timeout has been reached
}
};

waitForListening();

if (timeoutReached) {
throw new Error(`Timeout waiting for port ${port} to become available.`);
}
} catch (error) {
isAvailable = false;
} finally {
if (server) {
server.close();
}
}

return isAvailable;
};

function retrieveRandomPortNumber(): number {
const getRandomPort = () => 40000 + Math.round(Math.random() * 25000);
let port = getRandomPort();

for (let i = 0; i < 30; i++) {
const result = checkPortSync(port);

if (result) {
return port;
}
port = getRandomPort();
}

console.log(`${packageLog} could not find free port, will not report`);

return port;
}

const socketLogic = (port: number, sockserver: WebSocketServer | undefined, tasks: AllureTasks) => {
const socketLogic = (sockserver: WebSocketServer | undefined, tasks: AllureTasks) => {
if (!sockserver) {
log('Could not start reporting server');

Expand Down Expand Up @@ -77,19 +137,21 @@ const socketLogic = (port: number, sockserver: WebSocketServer | undefined, task
};

export const startReporterServer = (configOptions: PluginConfigOptions, tasks: AllureTasks, attempt = 0) => {
const wsPort = getRandomPortNumber();
const wsPort = retrieveRandomPortNumber();

const sockserver: WebSocketServer | undefined = new WebSocketServer({ port: wsPort, path: wsPath }, () => {
let sockserver: WebSocketServer | undefined = new WebSocketServer({ port: wsPort, path: wsPath }, () => {
configOptions.env[ENV_WS] = wsPort;
const attemptMessage = attempt > 0 ? ` from ${attempt} attempt` : '';
console.log(`${packageLog} running on ${wsPort} port${attemptMessage}`);
socketLogic(wsPort, sockserver, tasks);
socketLogic(sockserver, tasks);
});

sockserver.on('error', err => {
if (err.message.indexOf('address already in use') !== -1) {
if (attempt < 30) {
startReporterServer(configOptions, tasks, attempt + 1);
process.nextTick(() => {
sockserver = startReporterServer(configOptions, tasks, attempt + 1);
});
} else {
console.error(`${packageLog} Could not find free port, will not report: ${err.message}`);
}
Expand Down
41 changes: 41 additions & 0 deletions tests/cy-helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export const readWithRetry = (path: string, attempt = 0) => {
export const createResTest2 = (
specTexts: string[],
envConfig?: Record<string, string | undefined>,
shouldBeResults?: boolean,
): {
watch: string;
specs: string[];
Expand Down Expand Up @@ -352,6 +353,31 @@ export const createResTest2 = (
process.env.DEBUG = envConfig?.DEBUG ? 'cypress-allure*' : '';
process.env.COVERAGE_REPORT_DIR = 'reports/coverage-cypress';

const checkFilesExist = retries => {
return new Promise((resolve, reject) => {
const attempt = remainingRetries => {
if (!specs.every(p => existsSync(p))) {
console.log(
`Not all files were written: attempt ${retries - remainingRetries + 1}`,
);

if (remainingRetries > 0) {
return delay(1000)
.then(() => attempt(remainingRetries - 1))
.catch(reject);
} else {
return reject(
new Error('Files are still missing after all retries'),
);
}
}
resolve(true);
};

attempt(retries);
});
};

return cy
.run({
spec,
Expand All @@ -377,9 +403,24 @@ export const createResTest2 = (

return delay(1000);
}
})
.then(() => {
if (shouldBeResults) {
return checkFilesExist(10);
}
});
});

afterEach(() => {
// to investigate issue with missing
if (specs?.some(p => existsSync(p))) {
specs.forEach(s => {
const content = readFileSync(s);
console.log(content.toString());
});
}
});

return {
watch: env.allureResultsWatchPath ?? storeResDir,
specs: specs,
Expand Down
2 changes: 1 addition & 1 deletion tests/test-folder/common/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('utils', () => {
it('delay', async () => {
const started = Date.now();
await delay(100);
expect(Date.now() - started).toBeGreaterThanOrEqual(100);
expect(Date.now() - started).toBeGreaterThanOrEqual(99); // sometime has 99 instead of 100
});

it('messages should be dequeued in the same order as added', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('hello suite', () => {
`,
],
{ allure: 'false' },
false,
);
it('should be ok', () => {
checkCyResults(res?.result?.res, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('mocha events - check failures @oneInconsistency', () => {
const res = createResTest2([
`
describe('hello suite', { retries: 1 }, () => {
beforeEach(()=> {
beforeEach(() => {
cy.wrap(null).then(() => {
throw new Error('Test FAIL on purpose');
});
Expand Down
1 change: 1 addition & 0 deletions tests/test-folder/mocha-events/interface/no-allure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('should be no results when allure:false', () => {
`,
],
{ allure: 'false' },
false,
);

describe('check results', () => {
Expand Down

0 comments on commit e56b333

Please sign in to comment.