Build: Bump the github-actions group with 2 updates
[jquery.git] / test / runner / queue.js
blob843d5672f8ec4a1ba6a7867b9ae7e80a9b6af0e2
1 import chalk from "chalk";
2 import { getBrowserString } from "./lib/getBrowserString.js";
3 import {
4         checkLastTouches,
5         createBrowserWorker,
6         restartBrowser,
7         setBrowserWorkerUrl
8 } from "./browsers.js";
10 const TEST_POLL_TIMEOUT = 1000;
12 const queue = [];
14 export function getNextBrowserTest( reportId ) {
15         const index = queue.findIndex( ( test ) => test.id === reportId );
16         if ( index === -1 ) {
17                 return;
18         }
20         // Remove the completed test from the queue
21         const previousTest = queue[ index ];
22         queue.splice( index, 1 );
24         // Find the next test for the same browser
25         for ( const test of queue.slice( index ) ) {
26                 if ( test.fullBrowser === previousTest.fullBrowser ) {
28                         // Set the URL for our tracking
29                         setBrowserWorkerUrl( test.browser, test.url );
30                         test.running = true;
32                         // Return the URL for the next test.
33                         // listeners.js will use this to set the browser URL.
34                         return { url: test.url };
35                 }
36         }
39 export function retryTest( reportId, maxRetries ) {
40         if ( !maxRetries ) {
41                 return;
42         }
43         const test = queue.find( ( test ) => test.id === reportId );
44         if ( test ) {
45                 test.retries++;
46                 if ( test.retries <= maxRetries ) {
47                         console.log(
48                                 `\nRetrying test ${ reportId } for ${ chalk.yellow(
49                                         test.options.modules.join( ", " )
50                                 ) }...${ test.retries }`
51                         );
52                         return test;
53                 }
54         }
57 export async function hardRetryTest( reportId, maxHardRetries ) {
58         if ( !maxHardRetries ) {
59                 return false;
60         }
61         const test = queue.find( ( test ) => test.id === reportId );
62         if ( test ) {
63                 test.hardRetries++;
64                 if ( test.hardRetries <= maxHardRetries ) {
65                         console.log(
66                                 `\nHard retrying test ${ reportId } for ${ chalk.yellow(
67                                         test.options.modules.join( ", " )
68                                 ) }...${ test.hardRetries }`
69                         );
70                         await restartBrowser( test.browser );
71                         return true;
72                 }
73         }
74         return false;
77 export function addRun( url, browser, options ) {
78         queue.push( {
79                 browser,
80                 fullBrowser: getBrowserString( browser ),
81                 hardRetries: 0,
82                 id: options.reportId,
83                 url,
84                 options,
85                 retries: 0,
86                 running: false
87         } );
90 export async function runAll() {
91         return new Promise( async( resolve, reject ) => {
92                 while ( queue.length ) {
93                         try {
94                                 await checkLastTouches();
95                         } catch ( error ) {
96                                 reject( error );
97                         }
99                         // Run one test URL per browser at a time
100                         const browsersTaken = [];
101                         for ( const test of queue ) {
102                                 if ( browsersTaken.indexOf( test.fullBrowser ) > -1 ) {
103                                         continue;
104                                 }
105                                 browsersTaken.push( test.fullBrowser );
106                                 if ( !test.running ) {
107                                         test.running = true;
108                                         try {
109                                                 await createBrowserWorker( test.url, test.browser, test.options );
110                                         } catch ( error ) {
111                                                 reject( error );
112                                         }
113                                 }
114                         }
115                         await new Promise( ( resolve ) => setTimeout( resolve, TEST_POLL_TIMEOUT ) );
116                 }
117                 resolve();
118         } );