feat: add reusable jobqueue library
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
34
tests/helpers.ts
Normal file
34
tests/helpers.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user