Merge "Support multi-content diffs on Special:Undelete"
[lhc/web/wiklou.git] / tests / selenium / wdio-mediawiki / RunJobs.js
1 const MWBot = require( 'mwbot' ),
2 Page = require( './Page' ),
3 FRONTPAGE_REQUESTS_MAX_RUNS = 10; // (arbitrary) safe-guard against endless execution
4
5 /**
6 * Trigger the execution of jobs
7 *
8 * @see https://www.mediawiki.org/wiki/Manual:Job_queue/For_developers#Execution_of_jobs
9 *
10 * Use RunJobs.run() to ensure that jobs are executed before making assertions that depend on it.
11 *
12 * Systems that are selenium-tested are usually provisioned for that purpose, see no organic
13 * traffic, consequently typical post-send job queue processing rarely happens. Additionally,
14 * test set-up is often done through the API, requests to which do not trigger job queue
15 * processing at all.
16 *
17 * This can lead to an accumulation of unprocessed jobs, which in turn would render certain
18 * assertions impossible - e.g. checking a page is listed on Special:RecentChanges right
19 * after creating it.
20 *
21 * This class will try to trigger job execution through
22 * repeated blunt requests against the wiki's home page to trigger them at a rate
23 * of $wgJobRunRate per request.
24 */
25 class RunJobs {
26
27 static run() {
28 browser.call( () => {
29 return this.runThroughFrontPageRequests();
30 } );
31 }
32
33 static getJobCount() {
34 let bot = new MWBot( {
35 apiUrl: `${browser.options.baseUrl}/api.php`
36 } );
37 return new Promise( ( resolve ) => {
38 return bot.request( {
39 action: 'query',
40 meta: 'siteinfo',
41 siprop: 'statistics'
42 } ).then( ( response ) => {
43 resolve( response.query.statistics.jobs );
44 } );
45 } );
46 }
47
48 static runThroughFrontPageRequests( runCount = 1 ) {
49 let page = new Page();
50 this.log( `through requests to the front page (run ${runCount}).` );
51
52 page.openTitle( '' );
53
54 return this.getJobCount().then( ( jobCount ) => {
55 if ( jobCount === 0 ) {
56 this.log( 'found no more queued jobs.' );
57 return;
58 }
59 this.log( `detected ${jobCount} more queued job(s).` );
60 if ( runCount >= FRONTPAGE_REQUESTS_MAX_RUNS ) {
61 this.log( 'stopping requests to the front page due to reached limit.' );
62 return;
63 }
64 return this.runThroughFrontPageRequests( ++runCount );
65 } );
66 }
67
68 static log( message ) {
69 process.stdout.write( `RunJobs ${message}\n` );
70 }
71 }
72
73 module.exports = RunJobs;