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
53 changes: 37 additions & 16 deletions integrationTests/apiTests/utils/lifecycle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,39 @@ import { awaitJobCompleted } from './operations.mjs';
const DEFAULT_READINESS_TIMEOUT_MS = 60000;
const READINESS_POLL_INTERVAL_MS = 250;

/**
* Poll `probePath` until it stops returning 404, i.e. until the route is registered and
* being served. Any response in [200, 499] excluding 404 is treated as ready; 4xx
* auth/validation responses are fine.
*
* Use this directly (without `restartHttpWorkers`) when the component/route is already
* installed and only async post-boot route registration needs to be awaited — no worker
* restart involved.
*
* @param {ReturnType<import('./client.mjs').createApiClient>} client
* @param {string} probePath REST path that should be served by the component
* @param {number} [timeoutMs] overall readiness budget (default 60s)
*/
export async function waitForRouteReady(client, probePath, timeoutMs = DEFAULT_READINESS_TIMEOUT_MS) {
const deadline = Date.now() + timeoutMs;
let lastStatus;
let lastError;
while (Date.now() < deadline) {
try {
const response = await client.reqRest(probePath).timeout(2000);
lastStatus = response.status;
if (response.status !== 404) return;
} catch (err) {
lastError = err;
}
await setTimeout(READINESS_POLL_INTERVAL_MS);
}
throw new Error(
`Probe ${probePath} did not become ready within ${timeoutMs}ms ` +
`(last status=${lastStatus ?? 'none'}, last error=${lastError?.message ?? 'none'})`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (non-blocking): attach lastError as the cause on this timeout Error (same pattern already applied at line 79 for the restartHttpWorkers wrapper). Preserves the causal chain for debugging a probe failure vs. an unreachable route.

Suggested change
`(last status=${lastStatus ?? 'none'}, last error=${lastError?.message ?? 'none'})`
throw new Error(
`Probe ${probePath} did not become ready within ${timeoutMs}ms ` +
`(last status=${lastStatus ?? 'none'}, last error=${lastError?.message ?? 'none'})`,
{ cause: lastError }
);

);
Comment on lines +35 to +38

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When throwing a timeout error after polling, attaching the last encountered error ('lastError') as the 'cause' property of the new 'Error' preserves the causal chain, making it much easier to diagnose why the probe failed (e.g., connection refused vs. invalid response).

	throw new Error(
		'Probe ' + probePath + ' did not become ready within ' + timeoutMs + 'ms ' +
			'(last status=' + (lastStatus ?? 'none') + ', last error=' + (lastError?.message ?? 'none') + ')',
		{ cause: lastError }
	);
References
  1. Avoid mutating the message property of a caught error object. Instead, throw a new custom error with the decorated message and attach the original error as the cause property to preserve the causal chain.

}

/**
* Trigger `restart_service http_workers` and wait for the restart to actually complete and
* the REST workers (and any newly-registered component routes) to be serving traffic again.
Expand Down Expand Up @@ -40,21 +73,9 @@ export async function restartHttpWorkers(client, probePath, timeoutMs = DEFAULT_
assert.ok(body.job_id, `restart_service returned no job_id: ${JSON.stringify(body)}`);
await awaitJobCompleted(client, body.job_id, { timeoutSeconds: Math.ceil(timeoutMs / 1000) });

const deadline = Date.now() + timeoutMs;
let lastStatus;
let lastError;
while (Date.now() < deadline) {
try {
const response = await client.reqRest(probePath).timeout(2000);
lastStatus = response.status;
if (response.status !== 404) return;
} catch (err) {
lastError = err;
}
await setTimeout(READINESS_POLL_INTERVAL_MS);
try {
await waitForRouteReady(client, probePath, timeoutMs);
} catch (err) {
throw new Error(err.message + ' after restart_service', { cause: err });
}
Comment thread
kriszyp marked this conversation as resolved.
throw new Error(
`Probe ${probePath} did not become ready within ${timeoutMs}ms after restart_service ` +
`(last status=${lastStatus ?? 'none'}, last error=${lastError?.message ?? 'none'})`
);
}
17 changes: 4 additions & 13 deletions integrationTests/database/eviction-secondary-index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import { setTimeout as sleep } from 'node:timers/promises';
import { setupHarperWithFixture, teardownHarper, type ContextWithHarper } from '@harperfast/integration-testing';
// @ts-expect-error utils/client.mjs has no type declarations; runtime resolves fine
import { createApiClient } from './../apiTests/utils/client.mjs';
// @ts-expect-error utils/lifecycle.mjs has no type declarations; runtime resolves fine
import { waitForRouteReady } from './../apiTests/utils/lifecycle.mjs';

const FIXTURE_PATH = resolve(import.meta.dirname, 'eviction-secondary-index');
const SCHEMA = 'data';
Expand Down Expand Up @@ -86,19 +88,8 @@ suite(`QA-179 TTL eviction sweep vs secondary index [${ENGINE}]`, { skip: skipSu
proc?.stdout?.on('data', (d: Buffer) => (procOutput += d.toString()));
proc?.stderr?.on('data', (d: Buffer) => (procOutput += d.toString()));

// Poll for route readiness (component is pre-installed; no restart needed)
{
const deadline = Date.now() + 120_000;
while (Date.now() < deadline) {
try {
const probe = await client.reqRest('/Expiring/').timeout(2000);
if (probe.status !== 404) break;
} catch {
/* not ready yet */
}
await sleep(250);
}
}
// Wait for route readiness (component is pre-installed; no restart needed).
await waitForRouteReady(client, '/Expiring/', 120_000);
});

after(async () => {
Expand Down
Loading