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