@@ -3,6 +3,7 @@ import fs from "node:fs/promises";
33import os from "node:os" ;
44import path from "node:path" ;
55import { describe , expect , it , vi } from "vitest" ;
6+ import { withTimeout } from "../utils/with-timeout.js" ;
67import { CronService } from "./service.js" ;
78import { writeCronStoreSnapshot } from "./service.test-harness.js" ;
89
@@ -19,22 +20,6 @@ type IsolatedRunResult = {
1920 error ?: string ;
2021} ;
2122
22- async function withTimeout < T > ( promise : Promise < T > , timeoutMs : number , label : string ) : Promise < T > {
23- let timeout : NodeJS . Timeout | undefined ;
24- try {
25- return await Promise . race ( [
26- promise ,
27- new Promise < T > ( ( _resolve , reject ) => {
28- timeout = setTimeout ( ( ) => reject ( new Error ( `${ label } timed out` ) ) , timeoutMs ) ;
29- } ) ,
30- ] ) ;
31- } finally {
32- if ( timeout ) {
33- clearTimeout ( timeout ) ;
34- }
35- }
36- }
37-
3823async function makeStorePath ( ) {
3924 const dir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-cron-" ) ) ;
4025 return {
@@ -211,12 +196,19 @@ describe("CronService read ops while job is running", () => {
211196 await isolatedRun . runStarted ;
212197
213198 await expect (
214- withTimeout ( cron . list ( { includeDisabled : true } ) , 300 , "cron.list during cron.run" ) ,
199+ withTimeout ( cron . list ( { includeDisabled : true } ) , 300 , {
200+ message : "cron.list during cron.run timed out" ,
201+ } ) ,
215202 ) . resolves . toHaveLength ( 1 ) ;
216- expectCronStatus ( await withTimeout ( cron . status ( ) , 300 , "cron.status during cron.run" ) , {
217- storePath : store . storePath ,
218- jobs : 1 ,
219- } ) ;
203+ expectCronStatus (
204+ await withTimeout ( cron . status ( ) , 300 , {
205+ message : "cron.status during cron.run timed out" ,
206+ } ) ,
207+ {
208+ storePath : store . storePath ,
209+ jobs : 1 ,
210+ } ,
211+ ) ;
220212
221213 isolatedRun . completeRun ( { status : "ok" , summary : "manual done" } ) ;
222214 await expect ( runPromise ) . resolves . toEqual ( { ok : true , ran : true } ) ;
@@ -273,12 +265,19 @@ describe("CronService read ops while job is running", () => {
273265 expect ( isolatedRun . runIsolatedAgentJob ) . not . toHaveBeenCalled ( ) ;
274266
275267 await expect (
276- withTimeout ( cron . list ( { includeDisabled : true } ) , 300 , "cron.list during startup" ) ,
268+ withTimeout ( cron . list ( { includeDisabled : true } ) , 300 , {
269+ message : "cron.list during startup timed out" ,
270+ } ) ,
277271 ) . resolves . toHaveLength ( 1 ) ;
278- expectCronStatus ( await withTimeout ( cron . status ( ) , 300 , "cron.status during startup" ) , {
279- storePath : store . storePath ,
280- jobs : 1 ,
281- } ) ;
272+ expectCronStatus (
273+ await withTimeout ( cron . status ( ) , 300 , {
274+ message : "cron.status during startup timed out" ,
275+ } ) ,
276+ {
277+ storePath : store . storePath ,
278+ jobs : 1 ,
279+ } ,
280+ ) ;
282281
283282 const jobs = await cron . list ( { includeDisabled : true } ) ;
284283 expect ( jobs [ 0 ] ?. state . lastStatus ) . toBeUndefined ( ) ;
0 commit comments