aboutsummaryrefslogtreecommitdiffstats
path: root/test/runner/browserstack/queue.js
blob: 6d1c8d51fe6bee45068105744901c8aa3d2bf5de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import chalk from "chalk";
import { getBrowserString } from "../lib/getBrowserString.js";
import {
	checkLastTouches,
	createBrowserWorker,
	restartBrowser,
	setBrowserWorkerUrl
} from "./browsers.js";

const TEST_POLL_TIMEOUT = 1000;

const queue = [];

export function getNextBrowserTest( reportId ) {
	const index = queue.findIndex( ( test ) => test.id === reportId );
	if ( index === -1 ) {
		return;
	}

	// Remove the completed test from the queue
	const previousTest = queue[ index ];
	queue.splice( index, 1 );

	// Find the next test for the same browser
	for ( const test of queue.slice( index ) ) {
		if ( test.fullBrowser === previousTest.fullBrowser ) {

			// Set the URL for our tracking
			setBrowserWorkerUrl( test.browser, test.url );
			test.running = true;

			// Return the URL for the next test.
			// listeners.js will use this to set the browser URL.
			return { url: test.url };
		}
	}
}

export function retryTest( reportId, maxRetries ) {
	if ( !maxRetries ) {
		return;
	}
	const test = queue.find( ( test ) => test.id === reportId );
	if ( test ) {
		test.retries++;
		if ( test.retries <= maxRetries ) {
			console.log(
				`Retrying test ${ reportId } for ${ chalk.yellow(
					test.options.modules.join( ", " )
				) }...${ test.retries }`
			);
			return test;
		}
	}
}

export async function hardRetryTest( reportId, maxHardRetries ) {
	if ( !maxHardRetries ) {
		return false;
	}
	const test = queue.find( ( test ) => test.id === reportId );
	if ( test ) {
		test.hardRetries++;
		if ( test.hardRetries <= maxHardRetries ) {
			console.log(
				`Hard retrying test ${ reportId } for ${ chalk.yellow(
					test.options.modules.join( ", " )
				) }...${ test.hardRetries }`
			);
			await restartBrowser( test.browser );
			return true;
		}
	}
	return false;
}

export function addBrowserStackRun( url, browser, options ) {
	queue.push( {
		browser,
		fullBrowser: getBrowserString( browser ),
		hardRetries: 0,
		id: options.reportId,
		url,
		options,
		retries: 0,
		running: false
	} );
}

export async function runAllBrowserStack() {
	return new Promise( async( resolve, reject ) => {
		while ( queue.length ) {
			try {
				await checkLastTouches();
			} catch ( error ) {
				reject( error );
			}

			// Run one test URL per browser at a time
			const browsersTaken = [];
			for ( const test of queue ) {
				if ( browsersTaken.indexOf( test.fullBrowser ) > -1 ) {
					continue;
				}
				browsersTaken.push( test.fullBrowser );
				if ( !test.running ) {
					test.running = true;
					try {
						await createBrowserWorker( test.url, test.browser, test.options );
					} catch ( error ) {
						reject( error );
					}
				}
			}
			await new Promise( ( resolve ) => setTimeout( resolve, TEST_POLL_TIMEOUT ) );
		}
		resolve();
	} );
}