Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/model/mmv.model.TaskQueue.test.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MediaViewer.
3 *
4 * MediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const { TaskQueue } = require( 'mmv' );
19
20 ( function () {
21 QUnit.module( 'mmv.model.TaskQueue', QUnit.newMwEnvironment() );
22
23 QUnit.test( 'TaskQueue constructor sense check', ( assert ) => {
24 const taskQueue = new TaskQueue();
25
26 assert.true( taskQueue instanceof TaskQueue, 'TaskQueue created successfully' );
27 } );
28
29 QUnit.test( 'Queue length check', ( assert ) => {
30 const taskQueue = new TaskQueue();
31
32 assert.strictEqual( taskQueue.queue.length, 0, 'queue is initially empty' );
33
34 taskQueue.push( () => {} );
35
36 assert.strictEqual( taskQueue.queue.length, 1, 'queue length is incremented on push' );
37 } );
38
39 QUnit.test( 'State check', ( assert ) => {
40 const taskQueue = new TaskQueue();
41 const task = $.Deferred();
42
43 taskQueue.push( () => task );
44
45 assert.strictEqual( taskQueue.state, TaskQueue.State.NOT_STARTED,
46 'state is initially NOT_STARTED' );
47
48 const promise = taskQueue.execute().then( () => {
49 assert.strictEqual( taskQueue.state, TaskQueue.State.FINISHED,
50 'state is FINISHED after execution finished' );
51 } );
52
53 assert.strictEqual( taskQueue.state, TaskQueue.State.RUNNING,
54 'state is RUNNING after execution started' );
55
56 task.resolve();
57
58 return promise;
59 } );
60
61 QUnit.test( 'State check for cancellation', ( assert ) => {
62 const taskQueue = new TaskQueue();
63 const task = $.Deferred();
64
65 taskQueue.push( () => task );
66 taskQueue.execute();
67 taskQueue.cancel();
68
69 assert.strictEqual( taskQueue.state, TaskQueue.State.CANCELLED,
70 'state is CANCELLED after cancellation' );
71 } );
72
73 QUnit.test( 'Test executing empty queue', ( assert ) => {
74 const taskQueue = new TaskQueue();
75
76 return taskQueue.execute().done( () => {
77 assert.true( true, 'Queue promise resolved' );
78 } );
79 } );
80
81 QUnit.test( 'Simple execution test', ( assert ) => {
82 const taskQueue = new TaskQueue();
83 let called = false;
84
85 taskQueue.push( () => {
86 called = true;
87 } );
88
89 return taskQueue.execute().then( () => {
90 assert.strictEqual( called, true, 'Task executed successfully' );
91 } );
92 } );
93
94 QUnit.test( 'Task execution order test', ( assert ) => {
95 const taskQueue = new TaskQueue();
96 const order = [];
97
98 taskQueue.push( () => {
99 order.push( 1 );
100 } );
101
102 taskQueue.push( () => {
103 const deferred = $.Deferred();
104
105 order.push( 2 );
106
107 setTimeout( () => {
108 deferred.resolve();
109 }, 0 );
110
111 return deferred;
112 } );
113
114 taskQueue.push( () => {
115 order.push( 3 );
116 } );
117
118 return taskQueue.execute().then( () => {
119 assert.deepEqual( order, [ 1, 2, 3 ], 'Tasks executed in order' );
120 } );
121 } );
122
123 QUnit.test( 'Double execution test', ( assert ) => {
124 const taskQueue = new TaskQueue();
125 let called = 0;
126
127 taskQueue.push( () => {
128 called++;
129 } );
130
131 return taskQueue.execute().then( () => taskQueue.execute() ).then( () => {
132 assert.strictEqual( called, 1, 'Task executed only once' );
133 } );
134 } );
135
136 QUnit.test( 'Parallel execution test', ( assert ) => {
137 const taskQueue = new TaskQueue();
138 let called = 0;
139
140 taskQueue.push( () => {
141 called++;
142 } );
143
144 return $.when(
145 taskQueue.execute(),
146 taskQueue.execute()
147 ).then( () => {
148 assert.strictEqual( called, 1, 'Task executed only once' );
149 } );
150 } );
151
152 QUnit.test( 'Test push after execute', ( assert ) => {
153 const taskQueue = new TaskQueue();
154
155 taskQueue.execute();
156
157 assert.throws( () => {
158 taskQueue.push( () => {} );
159 }, 'Exception thrown when trying to push to an already running queue' );
160 } );
161
162 QUnit.test( 'Test failed task', ( assert ) => {
163 const taskQueue = new TaskQueue();
164
165 taskQueue.push( () => $.Deferred().reject() );
166
167 return taskQueue.execute().done( () => {
168 assert.true( true, 'Queue promise resolved' );
169 } );
170 } );
171
172 QUnit.test( 'Test that tasks wait for each other', ( assert ) => {
173 const taskQueue = new TaskQueue();
174 let longRunningTaskFinished = false;
175 let seenFinished = false;
176
177 taskQueue.push( () => {
178 const deferred = $.Deferred();
179
180 setTimeout( () => {
181 longRunningTaskFinished = true;
182 deferred.resolve();
183 }, 0 );
184
185 return deferred;
186 } );
187
188 taskQueue.push( () => {
189 seenFinished = longRunningTaskFinished;
190 } );
191
192 return taskQueue.execute().then( () => {
193 assert.true( seenFinished, 'Task waits for previous task to finish' );
194 } );
195 } );
196
197 QUnit.test( 'Test cancellation before start', ( assert ) => {
198 const taskQueue = new TaskQueue();
199 let triggered = false;
200 const verificationTask = function () {
201 triggered = true;
202 };
203
204 taskQueue.push( verificationTask );
205
206 taskQueue.cancel();
207
208 taskQueue.execute()
209 .done( () => {
210 assert.true( false, 'Queue promise rejected' );
211 } )
212 .fail( () => {
213 assert.true( true, 'Queue promise rejected' );
214 assert.strictEqual( triggered, false, 'Task was not triggered' );
215 } )
216 .always( assert.async() );
217 } );
218
219 QUnit.test( 'Test cancellation within callback', ( assert ) => {
220 const taskQueue = new TaskQueue();
221 let triggered = false;
222 const verificationTask = function () {
223 triggered = true;
224 };
225
226 taskQueue.push( () => {
227 taskQueue.cancel();
228 } );
229 taskQueue.push( verificationTask );
230
231 taskQueue.execute()
232 .done( () => {
233 assert.true( false, 'Queue promise rejected' );
234 } )
235 .fail( () => {
236 assert.true( true, 'Queue promise rejected' );
237 assert.strictEqual( triggered, false, 'Task was not triggered' );
238 } )
239 .always( assert.async() );
240 } );
241
242 QUnit.test( 'Test cancellation from task', ( assert ) => {
243 const taskQueue = new TaskQueue();
244 let triggered = false;
245 const task1 = $.Deferred();
246 const verificationTask = function () {
247 triggered = true;
248 };
249
250 taskQueue.push( () => task1 );
251 taskQueue.push( verificationTask );
252
253 setTimeout( () => {
254 taskQueue.cancel();
255 task1.resolve();
256 }, 0 );
257
258 taskQueue.execute()
259 .done( () => {
260 assert.true( false, 'Queue promise rejected' );
261 } )
262 .fail( () => {
263 assert.true( true, 'Queue promise rejected' );
264 assert.strictEqual( triggered, false, 'Task was not triggered' );
265 } )
266 .always( assert.async() );
267 } );
268
269 }() );