@@ -326,6 +326,102 @@ describe('Runner', () => {
326326 } ) ;
327327 } ) ;
328328
329+ describe ( 'Check running landrequests for timeout' , ( ) => {
330+ let onStatusUpdateSpy : jest . SpyInstance ;
331+ const getLandRequestStatus = ( date : Date ) => {
332+ const request = new LandRequest ( {
333+ created : new Date ( 123 ) ,
334+ forCommit : 'abc' ,
335+ id : '1' ,
336+ buildId : 1 ,
337+ triggererAaid : '123' ,
338+ pullRequestId : 1 ,
339+ pullRequest : mockPullRequest ,
340+ } ) ;
341+ return new LandRequestStatus ( {
342+ date,
343+ id : '1' ,
344+ isLatest : true ,
345+ request,
346+ requestId : '1' ,
347+ state : 'running' ,
348+ } ) ;
349+ } ;
350+
351+ beforeEach ( ( ) => {
352+ onStatusUpdateSpy = jest . spyOn ( runner , 'onStatusUpdate' ) . mockResolvedValue ( ) ;
353+ } ) ;
354+
355+ afterEach ( ( ) => {
356+ onStatusUpdateSpy . mockRestore ( ) ;
357+ } ) ;
358+
359+ test ( 'should get land build status (failed) if timeout period is not breached' , async ( ) => {
360+ jest . spyOn ( mockClient , 'getLandBuild' ) . mockResolvedValue ( {
361+ state : {
362+ result : {
363+ name : 'FAILED' ,
364+ } ,
365+ } ,
366+ } as any ) ;
367+ const mockLandRequestStatus = getLandRequestStatus ( new Date ( ) ) ;
368+
369+ //running state is within the timeout period of 2 hours
370+ mockQueue . getRunning = jest . fn ( async ( ) => [ mockLandRequestStatus ] ) ;
371+ await wait ( 500 ) ;
372+ expect ( mockLandRequestStatus . request . setStatus ) . not . toHaveBeenCalled ( ) ;
373+ await runner . checkRunningLandRequests ( ) ;
374+
375+ expect ( mockLandRequestStatus . request . setStatus ) . not . toHaveBeenCalled ( ) ;
376+ expect ( mockClient . getLandBuild ) . toHaveBeenCalled ( ) ;
377+ expect ( onStatusUpdateSpy ) . toHaveBeenCalledWith ( {
378+ buildId : 1 ,
379+ buildStatus : 'FAILED' ,
380+ } ) ;
381+ } ) ;
382+
383+ test ( 'should get land build status (running) if timeout period is not breached' , async ( ) => {
384+ jest . spyOn ( mockClient , 'getLandBuild' ) . mockResolvedValue ( {
385+ state : {
386+ stage : {
387+ name : 'RUNNING' ,
388+ } ,
389+ } ,
390+ } as any ) ;
391+ //running state is within the timeout period of 2 hours
392+ const mockLandRequestStatus = getLandRequestStatus ( new Date ( ) ) ;
393+
394+ await wait ( 500 ) ;
395+ mockQueue . getRunning = jest . fn ( async ( ) => [ mockLandRequestStatus ] ) ;
396+
397+ expect ( mockLandRequestStatus . request . setStatus ) . not . toHaveBeenCalled ( ) ;
398+ await runner . checkRunningLandRequests ( ) ;
399+
400+ expect ( mockClient . getLandBuild ) . toHaveBeenCalled ( ) ;
401+ expect ( mockLandRequestStatus . request . setStatus ) . not . toHaveBeenCalled ( ) ;
402+ expect ( onStatusUpdateSpy ) . toHaveBeenCalledWith ( {
403+ buildId : 1 ,
404+ buildStatus : undefined ,
405+ } ) ;
406+ } ) ;
407+
408+ test ( 'should fail land request if timeout period is breached' , async ( ) => {
409+ //running state is beyond the timeout period of 2 hours
410+ const mockLandRequestStatus = getLandRequestStatus ( new Date ( '2022-12-13T03:42:48.071Z' ) ) ;
411+
412+ mockQueue . getRunning = jest . fn ( async ( ) => [ mockLandRequestStatus ] ) ;
413+
414+ expect ( mockLandRequestStatus . request . setStatus ) . not . toHaveBeenCalled ( ) ;
415+ await runner . checkRunningLandRequests ( ) ;
416+
417+ expect ( mockLandRequestStatus . request . setStatus ) . toHaveBeenCalledTimes ( 1 ) ;
418+ expect ( mockLandRequestStatus . request . setStatus ) . toHaveBeenCalledWith (
419+ 'fail' ,
420+ 'Build timeout period breached' ,
421+ ) ;
422+ } ) ;
423+ } ) ;
424+
329425 describe ( 'moveFromQueueToRunning' , ( ) => {
330426 test ( 'should successfully transition land request from queued to running if all checks pass' , async ( ) => {
331427 const request = new LandRequest ( {
0 commit comments