Files
jobqueue/tests/helpers.ts

35 lines
905 B
TypeScript

import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
export function createTempDir(prefix = 'jobqueue-'): string {
return mkdtempSync(join(tmpdir(), prefix));
}
export function createDbPath(dir: string): string {
return join(dir, 'jobs.db');
}
export function cleanupDir(dir: string): void {
rmSync(dir, { recursive: true, force: true });
}
export async function waitFor(
predicate: () => boolean,
options: { timeoutMs?: number; intervalMs?: number } = {},
): Promise<void> {
const timeoutMs = options.timeoutMs ?? 2_000;
const intervalMs = options.intervalMs ?? 10;
const start = Date.now();
while (!predicate()) {
if (Date.now() - start > timeoutMs) {
throw new Error(`waitFor timed out after ${timeoutMs}ms`);
}
await new Promise((resolve) => {
setTimeout(resolve, intervalMs);
});
}
}