2014-02-10 21:24:19 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MediaViewer.
|
|
|
|
*
|
|
|
|
* MediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MediaViewer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
( function ( mw, $ ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
QUnit.module( 'mmv.model.TaskQueue', QUnit.newMwEnvironment() );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'TaskQueue constructor sanity check', function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue();
|
|
|
|
|
|
|
|
assert.ok( taskQueue, 'TaskQueue created successfully' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Queue length check', function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue();
|
|
|
|
|
|
|
|
assert.strictEqual( taskQueue.queue.length, 0, 'queue is initially empty' );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {} );
|
2014-02-10 21:24:19 +00:00
|
|
|
|
|
|
|
assert.strictEqual( taskQueue.queue.length, 1, 'queue length is incremented on push' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'State check', function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
2017-05-17 09:53:25 +00:00
|
|
|
task = $.Deferred(),
|
|
|
|
promise;
|
2014-02-10 21:24:19 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () { return task; } );
|
2014-02-10 21:24:19 +00:00
|
|
|
|
|
|
|
assert.strictEqual( taskQueue.state, mw.mmv.model.TaskQueue.State.NOT_STARTED,
|
|
|
|
'state is initially NOT_STARTED' );
|
|
|
|
|
2017-05-17 09:53:25 +00:00
|
|
|
promise = taskQueue.execute().then( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.strictEqual( taskQueue.state, mw.mmv.model.TaskQueue.State.FINISHED,
|
|
|
|
'state is FINISHED after execution finished' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
assert.strictEqual( taskQueue.state, mw.mmv.model.TaskQueue.State.RUNNING,
|
|
|
|
'state is RUNNING after execution started' );
|
|
|
|
|
2017-05-17 09:53:25 +00:00
|
|
|
task.resolve();
|
|
|
|
|
|
|
|
return promise;
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'State check for cancellation', function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
|
|
|
task = $.Deferred();
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () { return task; } );
|
2014-02-10 21:24:19 +00:00
|
|
|
taskQueue.execute();
|
|
|
|
taskQueue.cancel();
|
|
|
|
|
|
|
|
assert.strictEqual( taskQueue.state, mw.mmv.model.TaskQueue.State.CANCELLED,
|
|
|
|
'state is CANCELLED after cancellation' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Test executing empty queue', function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue();
|
|
|
|
|
2017-07-26 00:01:07 +00:00
|
|
|
return taskQueue.execute().done( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Queue promise resolved' );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Simple execution test', function ( assert ) {
|
2017-07-26 00:01:07 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
|
|
|
called = false;
|
2014-02-10 21:24:19 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2017-07-26 00:01:07 +00:00
|
|
|
called = true;
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-26 00:01:07 +00:00
|
|
|
return taskQueue.execute().then( function () {
|
|
|
|
assert.strictEqual( called, true, 'Task executed successfully' );
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Task execution order test', function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
2017-07-26 00:01:07 +00:00
|
|
|
order = [];
|
2014-02-10 21:24:19 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2017-07-26 00:01:07 +00:00
|
|
|
order.push( 1 );
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
var deferred = $.Deferred();
|
|
|
|
|
2017-07-26 00:01:07 +00:00
|
|
|
order.push( 2 );
|
2014-02-10 21:24:19 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
setTimeout( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
deferred.resolve();
|
|
|
|
}, 0 );
|
|
|
|
|
|
|
|
return deferred;
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2017-07-26 00:01:07 +00:00
|
|
|
order.push( 3 );
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-26 00:01:07 +00:00
|
|
|
return taskQueue.execute().then( function () {
|
|
|
|
assert.deepEqual( order, [ 1, 2, 3 ], 'Tasks executed in order' );
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Double execution test', function ( assert ) {
|
2017-07-26 00:01:07 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
|
|
|
called = 0;
|
2014-02-10 21:24:19 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2017-07-26 00:01:07 +00:00
|
|
|
called++;
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-26 00:01:07 +00:00
|
|
|
return taskQueue.execute().then( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
return taskQueue.execute();
|
2017-07-26 00:01:07 +00:00
|
|
|
} ).then( function () {
|
|
|
|
assert.strictEqual( called, 1, 'Task executed only once' );
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Parallel execution test', function ( assert ) {
|
2017-07-26 00:01:07 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
|
|
|
called = 0;
|
2014-02-10 21:24:19 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2017-07-26 00:01:07 +00:00
|
|
|
called++;
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-26 00:01:07 +00:00
|
|
|
return $.when(
|
2014-02-10 21:24:19 +00:00
|
|
|
taskQueue.execute(),
|
|
|
|
taskQueue.execute()
|
2017-07-26 00:01:07 +00:00
|
|
|
).then( function () {
|
|
|
|
assert.strictEqual( called, 1, 'Task executed only once' );
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Test push after execute', function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue();
|
|
|
|
|
|
|
|
taskQueue.execute();
|
|
|
|
|
|
|
|
try {
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {} );
|
|
|
|
} catch ( e ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( e, 'Exception thrown when trying to push to an already running queue' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Test failed task', function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue();
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
return $.Deferred().reject();
|
|
|
|
} );
|
|
|
|
|
2017-07-26 00:01:07 +00:00
|
|
|
return taskQueue.execute().done( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Queue promise resolved' );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Test that tasks wait for each other', function ( assert ) {
|
2017-07-26 00:01:07 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
|
|
|
longRunningTaskFinished = false,
|
|
|
|
seenFinished = false;
|
2014-02-10 21:24:19 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
var deferred = $.Deferred();
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
setTimeout( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
longRunningTaskFinished = true;
|
|
|
|
deferred.resolve();
|
|
|
|
}, 0 );
|
|
|
|
|
|
|
|
return deferred;
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2017-07-26 00:01:07 +00:00
|
|
|
seenFinished = longRunningTaskFinished;
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-26 00:01:07 +00:00
|
|
|
return taskQueue.execute().then( function () {
|
|
|
|
assert.ok( seenFinished, 'Task waits for previous task to finish' );
|
|
|
|
} );
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Test cancellation before start', function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
2017-07-26 00:01:07 +00:00
|
|
|
triggered = false,
|
2015-01-23 12:48:27 +00:00
|
|
|
verificationTask = function () {
|
2017-07-26 00:01:07 +00:00
|
|
|
triggered = true;
|
2014-02-10 21:24:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
taskQueue.push( verificationTask );
|
|
|
|
|
|
|
|
taskQueue.cancel();
|
|
|
|
|
2017-07-26 00:01:07 +00:00
|
|
|
taskQueue.execute()
|
|
|
|
.done( function () {
|
|
|
|
assert.ok( false, 'Queue promise rejected' );
|
|
|
|
} )
|
|
|
|
.fail( function () {
|
|
|
|
assert.ok( true, 'Queue promise rejected' );
|
|
|
|
assert.strictEqual( triggered, false, 'Task was not triggered' );
|
|
|
|
} )
|
|
|
|
.always( assert.async() );
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Test cancellation within callback', function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
2017-07-26 00:01:07 +00:00
|
|
|
triggered = false,
|
2015-01-23 12:48:27 +00:00
|
|
|
verificationTask = function () {
|
2017-07-26 00:01:07 +00:00
|
|
|
triggered = true;
|
2014-02-10 21:24:19 +00:00
|
|
|
};
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
taskQueue.cancel();
|
|
|
|
} );
|
|
|
|
taskQueue.push( verificationTask );
|
|
|
|
|
2017-07-26 00:01:07 +00:00
|
|
|
taskQueue.execute()
|
|
|
|
.done( function () {
|
|
|
|
assert.ok( false, 'Queue promise rejected' );
|
|
|
|
} )
|
|
|
|
.fail( function () {
|
|
|
|
assert.ok( true, 'Queue promise rejected' );
|
|
|
|
assert.strictEqual( triggered, false, 'Task was not triggered' );
|
|
|
|
} )
|
|
|
|
.always( assert.async() );
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'Test cancellation from task', function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
2017-07-26 00:01:07 +00:00
|
|
|
triggered = false,
|
2014-02-10 21:24:19 +00:00
|
|
|
task1 = $.Deferred(),
|
2015-01-23 12:48:27 +00:00
|
|
|
verificationTask = function () {
|
2017-07-26 00:01:07 +00:00
|
|
|
triggered = true;
|
2014-02-10 21:24:19 +00:00
|
|
|
};
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
return task1;
|
|
|
|
} );
|
|
|
|
taskQueue.push( verificationTask );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
setTimeout( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
taskQueue.cancel();
|
|
|
|
task1.resolve();
|
|
|
|
}, 0 );
|
|
|
|
|
2017-07-26 00:01:07 +00:00
|
|
|
taskQueue.execute()
|
|
|
|
.done( function () {
|
|
|
|
assert.ok( false, 'Queue promise rejected' );
|
|
|
|
} )
|
|
|
|
.fail( function () {
|
|
|
|
assert.ok( true, 'Queue promise rejected' );
|
|
|
|
assert.strictEqual( triggered, false, 'Task was not triggered' );
|
|
|
|
} )
|
|
|
|
.always( assert.async() );
|
2014-02-10 21:24:19 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
}( mediaWiki, jQuery ) );
|