-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathprocessing-queue.js
More file actions
231 lines (194 loc) · 5.24 KB
/
processing-queue.js
File metadata and controls
231 lines (194 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import config from "./config";
import {
defined,
generateHash,
now
} from "./utilities";
import {
runLoggingCallbacks
} from "./logging";
import Promise from "../promise";
import {
globalSuite
} from "../core";
import {
emit
} from "../events";
import {
setTimeout
} from "../globals";
let priorityCount = 0;
let unitSampler;
// This is a queue of functions that are tasks within a single test.
// After tests are dequeued from config.queue they are expanded into
// a set of tasks in this queue.
const taskQueue = [];
/**
* Advances the taskQueue to the next task. If the taskQueue is empty,
* process the testQueue
*/
function advance() {
advanceTaskQueue();
if ( !taskQueue.length && !config.blocking && !config.current ) {
advanceTestQueue();
}
}
/**
* Advances the taskQueue with an increased depth
*/
function advanceTaskQueue() {
const start = now();
config.depth = ( config.depth || 0 ) + 1;
processTaskQueue( start );
config.depth--;
}
/**
* Process the first task on the taskQueue as a promise.
* Each task is a function returned by https://github.com/qunitjs/qunit/blob/master/src/test.js#L381
*/
function processTaskQueue( start ) {
if ( taskQueue.length && !config.blocking ) {
const elapsedTime = now() - start;
if ( !defined.setTimeout || config.updateRate <= 0 || elapsedTime < config.updateRate ) {
const task = taskQueue.shift();
const processNextTaskOrAdvance = () => {
if ( !taskQueue.length ) {
advance();
} else {
processTaskQueue( start );
}
};
const throwAndAdvance = ( err ) => {
setTimeout( advance );
throw err;
};
// Without throwAndAdvance, qunit does not continue advanceing the processing queue if
// task() throws an error
Promise.resolve( task() )
.then( processNextTaskOrAdvance, throwAndAdvance )
.catch( throwAndAdvance );
} else {
setTimeout( advance );
}
}
}
/**
* Advance the testQueue to the next test to process. Call done() if testQueue completes.
*/
function advanceTestQueue() {
if ( !config.blocking && !config.queue.length && config.depth === 0 ) {
done();
return;
}
const testTasks = config.queue.shift();
addToTaskQueue( testTasks() );
if ( priorityCount > 0 ) {
priorityCount--;
}
advance();
}
/**
* Enqueue the tasks for a test into the task queue.
* @param {Array} tasksArray
*/
function addToTaskQueue( tasksArray ) {
taskQueue.push( ...tasksArray );
}
/**
* Return the number of tasks remaining in the task queue to be processed.
* @return {Number}
*/
function taskQueueLength() {
return taskQueue.length;
}
/**
* Adds a test to the TestQueue for execution.
* @param {Function} testTasksFunc
* @param {Boolean} prioritize
* @param {String} seed
*/
function addToTestQueue( testTasksFunc, prioritize, seed ) {
if ( prioritize ) {
config.queue.splice( priorityCount++, 0, testTasksFunc );
} else if ( seed ) {
if ( !unitSampler ) {
unitSampler = unitSamplerGenerator( seed );
}
// Insert into a random position after all prioritized items
const index = Math.floor( unitSampler() * ( config.queue.length - priorityCount + 1 ) );
config.queue.splice( priorityCount + index, 0, testTasksFunc );
} else {
config.queue.push( testTasksFunc );
}
}
/**
* Creates a seeded "sample" generator which is used for randomizing tests.
*/
function unitSamplerGenerator( seed ) {
// 32-bit xorshift, requires only a nonzero seed
// http://excamera.com/sphinx/article-xorshift.html
let sample = parseInt( generateHash( seed ), 16 ) || -1;
return function() {
sample ^= sample << 13;
sample ^= sample >>> 17;
sample ^= sample << 5;
// ECMAScript has no unsigned number type
if ( sample < 0 ) {
sample += 0x100000000;
}
return sample / 0x100000000;
};
}
// Clear own storage items when tests completes
function cleanStorage() {
const storage = config.storage;
if ( storage && config.stats.bad === 0 ) {
for ( let i = storage.length - 1; i >= 0; i-- ) {
const key = storage.key( i );
if ( key.indexOf( "qunit-test-" ) === 0 ) {
storage.removeItem( key );
}
}
}
}
/**
* This function is called when the ProcessingQueue is done processing all
* items. It handles emitting the final run events.
*/
function done() {
ProcessingQueue.finished = true;
const runtime = now() - config.started;
const passed = config.stats.all - config.stats.bad;
if ( config.stats.all === 0 ) {
if ( config.filter && config.filter.length ) {
throw new Error( `No tests matched the filter "${config.filter}".` );
}
if ( config.module && config.module.length ) {
throw new Error( `No tests matched the module "${config.module}".` );
}
if ( config.moduleId && config.moduleId.length ) {
throw new Error( `No tests matched the moduleId "${config.moduleId}".` );
}
if ( config.testId && config.testId.length ) {
throw new Error( `No tests matched the testId "${config.testId}".` );
}
throw new Error( "No tests were run." );
}
emit( "runEnd", globalSuite.end( true ) );
runLoggingCallbacks( "done", {
passed,
failed: config.stats.bad,
total: config.stats.all,
runtime
} ).then( cleanStorage, function( err ) {
cleanStorage();
throw err;
} );
}
const ProcessingQueue = {
finished: false,
add: addToTestQueue,
advance,
taskCount: taskQueueLength
};
export default ProcessingQueue;