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