Files
OmniRoute/tests/unit/free-proxy-auto-sync-scheduler.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966)

Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.

One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.

Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.

* fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff

The gate shells out to `git diff` through execFileSync with Node's default 1 MB
maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to
overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing
anything. 64 MB is far above any real PR and costs nothing when unused.
2026-08-29 01:17:40 -03:00

217 lines
7.8 KiB
TypeScript

/**
* #7079 — free-proxy auto-sync scheduler. Network-free: the sync-cycle body
* is swapped out via `_setSyncCycleRunnerForTests()` so no test hits a real
* provider or the network. Timers use `node:test`'s `mock.timers`.
*/
import { test, mock } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-free-proxy-autosync-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.DISABLE_SQLITE_AUTO_BACKUP = "true";
// The module auto-initializes on import (matches `proxyHealth/scheduler.ts`);
// keep it disabled at import time so the very first import doesn't schedule
// anything before a test can control the env.
process.env.FREE_PROXY_AUTO_SYNC_ENABLED = "false";
const core = await import("../../src/lib/db/core.ts");
const freeProxiesDb = await import("../../src/lib/db/freeProxies.ts");
const scheduler = await import("../../src/lib/freeProxyProviders/scheduler.ts");
const ENV_KEYS = [
"FREE_PROXY_AUTO_SYNC_ENABLED",
"FREE_PROXY_AUTO_SYNC_INTERVAL_MS",
"NEXT_PHASE",
"OMNIROUTE_DISABLE_BACKGROUND_SERVICES",
"FREE_PROXY_1PROXY_ENABLED",
"FREE_PROXY_PROXIFLY_ENABLED",
] as const;
const savedEnv = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
function restoreEnv() {
for (const key of ENV_KEYS) {
const val = savedEnv[key];
if (val === undefined) delete process.env[key];
else process.env[key] = val;
}
}
function reset() {
scheduler.stopFreeProxyAutoSync();
scheduler._setSyncCycleRunnerForTests(null);
restoreEnv();
process.env.FREE_PROXY_AUTO_SYNC_ENABLED = "false";
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.beforeEach(() => {
reset();
});
test.after(() => {
scheduler.stopFreeProxyAutoSync();
scheduler._setSyncCycleRunnerForTests(null);
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
restoreEnv();
});
test("disabled by default (unset) — initFreeProxyAutoSync schedules nothing", () => {
delete process.env.FREE_PROXY_AUTO_SYNC_ENABLED;
scheduler.initFreeProxyAutoSync();
assert.equal(globalThis.__freeProxyAutoSyncInterval, undefined);
assert.equal(globalThis.__freeProxyAutoSyncStartupTimer, undefined);
});
test('FREE_PROXY_AUTO_SYNC_ENABLED="false" schedules nothing, sync never called', async () => {
process.env.FREE_PROXY_AUTO_SYNC_ENABLED = "false";
let calls = 0;
scheduler._setSyncCycleRunnerForTests(async () => {
calls++;
return { results: {}, lastSyncAt: new Date().toISOString() };
});
scheduler.initFreeProxyAutoSync();
assert.equal(globalThis.__freeProxyAutoSyncStartupTimer, undefined);
assert.equal(calls, 0);
});
test("enabled but no providers are enabled — scheduling is skipped", () => {
process.env.FREE_PROXY_AUTO_SYNC_ENABLED = "true";
process.env.FREE_PROXY_1PROXY_ENABLED = "false";
process.env.FREE_PROXY_PROXIFLY_ENABLED = "false";
scheduler.initFreeProxyAutoSync();
assert.equal(globalThis.__freeProxyAutoSyncStartupTimer, undefined);
assert.equal(globalThis.__freeProxyAutoSyncInterval, undefined);
});
test("enabled with an enabled provider — startup timer is scheduled", async () => {
process.env.FREE_PROXY_AUTO_SYNC_ENABLED = "true";
// Leave oneproxy/proxifly at their default-enabled state.
scheduler.initFreeProxyAutoSync();
// The startup timer is armed after an async initial-delay DB read resolves.
await new Promise((resolve) => setImmediate(resolve));
assert.ok(globalThis.__freeProxyAutoSyncStartupTimer, "expected a startup timer to be armed");
});
test("an interval below the 5-minute floor is raised to the floor", () => {
process.env.FREE_PROXY_AUTO_SYNC_INTERVAL_MS = "1000";
assert.equal(scheduler.getFreeProxyAutoSyncIntervalMs(), 300_000);
});
test("a valid interval above the floor is respected as-is", () => {
process.env.FREE_PROXY_AUTO_SYNC_INTERVAL_MS = "900000";
assert.equal(scheduler.getFreeProxyAutoSyncIntervalMs(), 900_000);
});
test("an unset/invalid interval falls back to the 30-minute default", () => {
delete process.env.FREE_PROXY_AUTO_SYNC_INTERVAL_MS;
assert.equal(scheduler.getFreeProxyAutoSyncIntervalMs(), 1_800_000);
process.env.FREE_PROXY_AUTO_SYNC_INTERVAL_MS = "not-a-number";
assert.equal(scheduler.getFreeProxyAutoSyncIntervalMs(), 1_800_000);
});
test("isBuildProcess() (NEXT_PHASE=phase-production-build) suppresses scheduling", () => {
process.env.FREE_PROXY_AUTO_SYNC_ENABLED = "true";
process.env.NEXT_PHASE = "phase-production-build";
scheduler.initFreeProxyAutoSync();
assert.equal(globalThis.__freeProxyAutoSyncStartupTimer, undefined);
});
test("OMNIROUTE_DISABLE_BACKGROUND_SERVICES=true suppresses scheduling", () => {
process.env.FREE_PROXY_AUTO_SYNC_ENABLED = "true";
process.env.OMNIROUTE_DISABLE_BACKGROUND_SERVICES = "true";
scheduler.initFreeProxyAutoSync();
assert.equal(globalThis.__freeProxyAutoSyncStartupTimer, undefined);
});
test("reentrancy guard: a second forced cycle is skipped while the first is still running", async () => {
let concurrentCalls = 0;
let maxConcurrent = 0;
let releaseFirst: (() => void) | undefined;
const gate = new Promise<void>((resolve) => {
releaseFirst = resolve;
});
scheduler._setSyncCycleRunnerForTests(async () => {
concurrentCalls++;
maxConcurrent = Math.max(maxConcurrent, concurrentCalls);
await gate;
concurrentCalls--;
return { results: {}, lastSyncAt: new Date().toISOString() };
});
const firstCycle = scheduler.forceFreeProxySyncCycle();
// Fire the second cycle while the first is still awaiting the gate. The
// reentrancy guard must skip it entirely — the runner must not be invoked
// a second time while isRunning is true.
const secondCycle = scheduler.forceFreeProxySyncCycle();
releaseFirst?.();
await Promise.all([firstCycle, secondCycle]);
assert.equal(maxConcurrent, 1, "the cycle runner must never overlap itself");
});
test("cycle delegates to the shared sync-cycle runner (same path as the manual route)", async () => {
let called = false;
scheduler._setSyncCycleRunnerForTests(async () => {
called = true;
return {
results: { "1proxy": { fetched: 1, added: 1, updated: 0, errors: [] } },
lastSyncAt: "x",
};
});
await scheduler.forceFreeProxySyncCycle();
assert.equal(called, true);
});
test("initial delay: a recent lastSyncAt shortens the first tick below a full interval", async () => {
const intervalMs = 300_000; // floor
process.env.FREE_PROXY_AUTO_SYNC_INTERVAL_MS = String(intervalMs);
process.env.FREE_PROXY_AUTO_SYNC_ENABLED = "true";
// Record a sync that "happened" 100s ago — well inside the 5-minute interval.
const elapsedMs = 100_000;
await freeProxiesDb.recordFreeProxySync(new Date(Date.now() - elapsedMs).toISOString());
let cycleRuns = 0;
scheduler._setSyncCycleRunnerForTests(async () => {
cycleRuns++;
return { results: {}, lastSyncAt: new Date().toISOString() };
});
mock.timers.enable({ apis: ["setTimeout", "setInterval"] });
try {
scheduler.initFreeProxyAutoSync();
// Let the async initial-delay computation (a DB read) resolve and arm the timer.
await new Promise((resolve) => setImmediate(resolve));
// Advancing by less than (intervalMs - elapsedMs) must not fire yet.
mock.timers.tick(intervalMs - elapsedMs - 5_000);
assert.equal(cycleRuns, 0, "must not fire before the shortened initial delay elapses");
// Advancing past the remaining delay fires the first cycle.
mock.timers.tick(10_000);
assert.equal(cycleRuns, 1, "expected exactly one cycle once the initial delay elapses");
} finally {
mock.timers.reset();
}
});