test: cover runner scheduling and restart
This commit is contained in:
@@ -2,7 +2,8 @@ import { assert, assertEquals, assertRejects } from "jsr:@std/assert@^1.0.0";
|
||||
|
||||
import { RunnerCoordinator } from "../../../uptime.link/ts_api/classes/runner-coordinator.ts";
|
||||
import { RunnerFileStore } from "../../../uptime.link/ts_api/classes/runner-file-store.ts";
|
||||
import { RunnerHttpHandler } from "../../../uptime.link/ts_api/classes/runner-http-handler.ts";
|
||||
import { createRunnerRequestHandler } from "../../../uptime.link/ts_api/classes/runner-request-handler.ts";
|
||||
import { RunnerScheduler } from "../../../uptime.link/ts_api/classes/runner-scheduler.ts";
|
||||
import { UptimeRunner } from "../../../uptimerunner/ts/runner.ts";
|
||||
import type {
|
||||
IResultSubmitRequest,
|
||||
@@ -45,10 +46,22 @@ const main = async () => {
|
||||
const coordinator = new RunnerCoordinator({
|
||||
runners: [{ runnerId, token: runnerToken, labels: ["scenario:basic"] }],
|
||||
});
|
||||
for (const check of checkQueue) {
|
||||
coordinator.enqueueCheck(check);
|
||||
}
|
||||
const runnerHttpHandler = new RunnerHttpHandler({ coordinator });
|
||||
const scheduler = new RunnerScheduler(coordinator, { now: () => 1000 });
|
||||
const scheduleResult = scheduler.scheduleDueChecks(
|
||||
checkQueue.map((check) => ({
|
||||
monitorId: `monitor-${check.id}`,
|
||||
check,
|
||||
intervalMs: 60000,
|
||||
})),
|
||||
);
|
||||
assertEquals(scheduleResult.scheduledChecks.length, 3);
|
||||
assertEquals(coordinator.getQueueLength(), 3);
|
||||
assertEquals(
|
||||
scheduler.scheduleDueChecks(scheduleResult.schedules).scheduledChecks
|
||||
.length,
|
||||
0,
|
||||
);
|
||||
const runnerRequestHandler = createRunnerRequestHandler(coordinator);
|
||||
|
||||
const coordinatorServer = await startServer(async (request) => {
|
||||
if (
|
||||
@@ -57,7 +70,7 @@ const main = async () => {
|
||||
) {
|
||||
resultSubmissions.push(await request.clone().json());
|
||||
}
|
||||
return await runnerHttpHandler.handleRequest(request);
|
||||
return await runnerRequestHandler(request);
|
||||
});
|
||||
|
||||
try {
|
||||
@@ -85,7 +98,7 @@ const main = async () => {
|
||||
assert(resultSubmissions[0].results[0].responseTime !== undefined);
|
||||
assertEquals(coordinator.listResults().length, 3);
|
||||
assertEquals(coordinator.getQueueLength(), 0);
|
||||
await assertSnapshotPersistence(coordinator);
|
||||
await assertSnapshotPersistence(coordinator, targetServer.url);
|
||||
|
||||
const emptyResult = await runner.runOnce();
|
||||
assertEquals(emptyResult.checks.length, 0);
|
||||
@@ -159,6 +172,7 @@ function startTcpServer(): { port: number; close: () => void } {
|
||||
|
||||
async function assertSnapshotPersistence(
|
||||
coordinatorArg: RunnerCoordinator,
|
||||
targetUrlArg: string,
|
||||
): Promise<void> {
|
||||
const snapshotPath = await Deno.makeTempFile();
|
||||
const store = new RunnerFileStore({
|
||||
@@ -184,6 +198,30 @@ async function assertSnapshotPersistence(
|
||||
const restoredCoordinator = new RunnerCoordinator({ snapshot });
|
||||
assertEquals(restoredCoordinator.listResults().length, 3);
|
||||
assertEquals(restoredCoordinator.listRunners().length, 1);
|
||||
restoredCoordinator.enqueueCheck({
|
||||
id: "post-restart-http-health",
|
||||
type: "http",
|
||||
url: targetUrlArg,
|
||||
expectedStatusCodes: [200],
|
||||
expectedBodyIncludes: "healthy",
|
||||
});
|
||||
|
||||
const restoredServer = await startServer(
|
||||
createRunnerRequestHandler(restoredCoordinator),
|
||||
);
|
||||
try {
|
||||
const restoredRunner = new UptimeRunner({
|
||||
instanceUrl: restoredServer.url,
|
||||
runnerId,
|
||||
token: runnerToken,
|
||||
});
|
||||
const restoredResult = await restoredRunner.runOnce();
|
||||
assertEquals(restoredResult.results.length, 1);
|
||||
assertEquals(restoredResult.results[0].status, "ok");
|
||||
assertEquals(restoredCoordinator.listResults().length, 4);
|
||||
} finally {
|
||||
await restoredServer.server.shutdown();
|
||||
}
|
||||
} finally {
|
||||
await Deno.remove(snapshotPath).catch(() => null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user