Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions tests/playwright-tests/src/api/apiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { config } from "../config/env";

const apiRetryDefault = Number(config.apiRetry);
const initialWaitTimeDefault = Number(config.apiWaitTime) || 2000;
const stepWaitTimeDefault = Number(config.apiStepMs) || 5000;
const maxWaitTimeDefault = Number(config.apiMaxWaitMs) || 5000;
const endpointCohortDistributionDataService = config.endpointCohortDistributionDataService;
const endpointParticipantManagementDataService = config.endpointParticipantManagementDataService;
const endpointExceptionManagementDataService = config.endpointExceptionManagementDataService;
Expand All @@ -26,15 +26,18 @@ let response: APIResponse;
export async function validateApiResponse(
validationJson: any,
request: any,
options?: { retries?: number; initialWaitMs?: number; stepMs?: number }
options?: { retries?: number; initialWaitMs?: number; maxWaitMs?: number }
): Promise<{ status: boolean; errorTrace?: any }> {
let status = false;
let endpoint = "";
let errorTrace: any = undefined;

const maxAttempts = Math.max(1, options?.retries ?? apiRetryDefault);
let waitTime = Math.max(0, options?.initialWaitMs ?? initialWaitTimeDefault);
const stepMs = Math.max(0, options?.stepMs ?? stepWaitTimeDefault);
const maxWaitTime = Math.max(0, options?.maxWaitMs ?? maxWaitTimeDefault);

// Track timing for better diagnostics
const startTime = Date.now();

for (let attempt = 1; attempt <= maxAttempts; attempt++) {
if (status) break;
Expand Down Expand Up @@ -76,23 +79,53 @@ export async function validateApiResponse(
status = await validateFields(apiValidation, matchingObject, nhsNumber, matchingObjects);
}
}

// Log success and exit retry loop
if (status) {
const elapsedSecs = getElapsedSeconds(startTime);
const attemptInfo = attempt === 1 ? 'on first attempt' : `on attempt ${attempt}/${maxAttempts}`;
console.info(`✅ Data found ${attemptInfo} (${elapsedSecs}s)`);
break;
}
} catch (error) {
const errorMsg = `Endpoint: ${endpoint}, Status: ${response?.status?.()}, Error: ${error instanceof Error ? error.stack || error.message : error}`;
errorTrace = errorMsg;
if (response?.status?.() === 204) {
console.info(`ℹ️\t Status 204: No data found in the table using endpoint ${endpoint}`);
}

// Fail fast for 5xx server errors - these won't resolve with retries
const statusCode = response?.status?.();
if (statusCode && statusCode >= 500 && statusCode < 600) {
console.error(`❌ Server error ${statusCode} detected - failing fast without retries`);
break; // Exit retry loop immediately
}

// Fail fast for 400 Bad Request - indicates a problem with the test data/request
if (statusCode === 400) {
console.error(`❌ Bad Request (400) detected - failing fast without retries`);
break; // Exit retry loop immediately
}
}

if (attempt < maxAttempts && !status) {
const secs = waitTime > 0 ? Math.round(waitTime / 1000) : 0;
console.info(`🚧 Function processing in progress; will check again using data service ${endpoint} in ${secs} seconds...`);
const elapsedSecs = getElapsedSeconds(startTime);
console.info(`🚧 Retry ${attempt}/${maxAttempts}: will check again using data service ${endpoint} in ${secs} seconds (elapsed: ${elapsedSecs}s)...`);
if (waitTime > 0) {
await new Promise((resolve) => setTimeout(resolve, waitTime));
}
waitTime += stepMs;
// Capped exponential backoff: multiply by 1.5x, max configured wait time
waitTime = Math.min(Math.round(waitTime * 1.5), maxWaitTime);
}
}

// Log final status with total elapsed time
const totalElapsedSecs = getElapsedSeconds(startTime);
if (!status) {
console.error(`❌ Validation failed after ${maxAttempts} attempts (total time: ${totalElapsedSecs}s)`);
}

return { status, errorTrace };
}

Expand Down Expand Up @@ -166,6 +199,9 @@ async function findMatchingObject(endpoint: string, responseBody: any[], apiVali
return { matchingObject, nhsNumber, matchingObjects };
}

function getElapsedSeconds(startTime: number): string {
return ((Date.now() - startTime) / 1000).toFixed(1);
}

async function validateFields(apiValidation: any, matchingObject: any, nhsNumber: any, matchingObjects: any): Promise<boolean> {
const fieldsToValidate = Object.entries(apiValidation.validations).filter(([key]) => key !== IGNORE_VALIDATION_KEY);
Expand Down
4 changes: 2 additions & 2 deletions tests/playwright-tests/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export const config = {
e2eTestFilesPath: 'e2e/testFiles',
apiTestFilesPath: 'api/testFiles',
apiRetry: Number(process.env.API_RETRY ?? 8),
apiWaitTime: Number(process.env.API_WAIT_MS ?? 5000),
apiStepMs: Number(process.env.API_STEP_MS ?? 5000),
apiWaitTime: Number(process.env.API_WAIT_MS ?? 1000),
apiMaxWaitMs: Number(process.env.API_MAX_WAIT_MS ?? 5000),
nhsNumberKey: 'NHSNumber',
nhsNumberKeyExceptionDemographic: 'NhsNumber',
uniqueKeyCohortDistribution: 'CohortDistributionId',
Expand Down
Loading