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() );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'TaskQueue constructor sanity check', 1, function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue();
|
|
|
|
|
|
|
|
assert.ok( taskQueue, 'TaskQueue created successfully' );
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Queue length check', 2, 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' );
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'State check', 3, 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
|
|
|
|
|
|
|
assert.strictEqual( taskQueue.state, mw.mmv.model.TaskQueue.State.NOT_STARTED,
|
|
|
|
'state is initially NOT_STARTED' );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
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' );
|
|
|
|
|
|
|
|
task.resolve();
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'State check for cancellation', 1, 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' );
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Test executing empty queue', 1, function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue();
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.execute().done( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Queue promise resolved' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Simple execution test', 2, function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue();
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Task executed successfully' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.execute().done( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Queue promise resolved' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Task execution order test', 4, function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
|
|
|
nextExpectedTask = 1;
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.strictEqual( nextExpectedTask, 1, 'First task executed in order' );
|
|
|
|
nextExpectedTask++;
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
var deferred = $.Deferred();
|
|
|
|
|
|
|
|
assert.strictEqual( nextExpectedTask, 2, 'Second task executed in order' );
|
|
|
|
nextExpectedTask++;
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
setTimeout( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
deferred.resolve();
|
|
|
|
QUnit.start();
|
|
|
|
}, 0 );
|
|
|
|
|
|
|
|
return deferred;
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.strictEqual( nextExpectedTask, 3, 'Second task executed in order' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.execute().done( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Queue promise resolved' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Double execution test', 2, function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskExecuted = false,
|
|
|
|
taskQueue = new mw.mmv.model.TaskQueue();
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( !taskExecuted, 'Task executed only once' );
|
|
|
|
taskExecuted = true;
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.execute().then( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
return taskQueue.execute();
|
2015-01-23 12:48:27 +00:00
|
|
|
} ).done( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Queue promise resolved' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Parallel execution test', 2, function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskExecuted = false,
|
|
|
|
taskQueue = new mw.mmv.model.TaskQueue();
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( !taskExecuted, 'Task executed only once' );
|
|
|
|
taskExecuted = true;
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.stop();
|
|
|
|
$.when(
|
|
|
|
taskQueue.execute(),
|
|
|
|
taskQueue.execute()
|
2015-01-23 12:48:27 +00:00
|
|
|
).done( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Queue promise resolved' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Test push after execute', 1, 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' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Test failed task', 1, 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();
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.execute().done( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Queue promise resolved' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Test that tasks wait for each other', 1, function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var longRunningTaskFinished = false,
|
|
|
|
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
|
|
|
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;
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( longRunningTaskFinished, 'Task waits for previous task to finish' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
|
|
|
|
taskQueue.execute();
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Test cancellation before start', 2, function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
|
|
|
taskTriggeredByHand = false,
|
2015-01-23 12:48:27 +00:00
|
|
|
verificationTask = function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( taskTriggeredByHand, 'Task was not triggered' );
|
|
|
|
QUnit.start();
|
|
|
|
};
|
|
|
|
|
|
|
|
taskQueue.push( verificationTask );
|
|
|
|
|
|
|
|
taskQueue.cancel();
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.execute().fail( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Queue promise rejected' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
setTimeout( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
taskTriggeredByHand = true;
|
|
|
|
verificationTask();
|
|
|
|
}, 0 );
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Test cancellation within callback', 2, function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
|
|
|
taskTriggeredByHand = false,
|
2015-01-23 12:48:27 +00:00
|
|
|
verificationTask = function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( taskTriggeredByHand, 'Task was not triggered' );
|
|
|
|
QUnit.start();
|
|
|
|
};
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
taskQueue.cancel();
|
|
|
|
} );
|
|
|
|
taskQueue.push( verificationTask );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.execute().fail( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Queue promise rejected' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
setTimeout( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
taskTriggeredByHand = true;
|
|
|
|
verificationTask();
|
|
|
|
}, 0 );
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
QUnit.test( 'Test cancellation from task', 2, function ( assert ) {
|
2014-02-10 21:24:19 +00:00
|
|
|
var taskQueue = new mw.mmv.model.TaskQueue(),
|
|
|
|
taskTriggeredByHand = false,
|
|
|
|
task1 = $.Deferred(),
|
2015-01-23 12:48:27 +00:00
|
|
|
verificationTask = function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( taskTriggeredByHand, 'Task was not triggered' );
|
|
|
|
QUnit.start();
|
|
|
|
};
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.push( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
return task1;
|
|
|
|
} );
|
|
|
|
taskQueue.push( verificationTask );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
taskQueue.execute().fail( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
assert.ok( true, 'Queue promise rejected' );
|
|
|
|
QUnit.start();
|
|
|
|
} );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
setTimeout( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
taskQueue.cancel();
|
|
|
|
task1.resolve();
|
|
|
|
}, 0 );
|
|
|
|
|
|
|
|
QUnit.stop();
|
2015-01-23 12:48:27 +00:00
|
|
|
setTimeout( function () {
|
2014-02-10 21:24:19 +00:00
|
|
|
taskTriggeredByHand = true;
|
|
|
|
verificationTask();
|
|
|
|
}, 0 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
}( mediaWiki, jQuery ) );
|