-
Notifications
You must be signed in to change notification settings - Fork 784
/
processing-queue.js
221 lines (189 loc) 路 5.44 KB
/
processing-queue.js
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
import config from "./config";
import onUncaughtException from "./on-uncaught-exception";
import {
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 added by Test#queue() in /src/test.js
*/
function processTaskQueue( start ) {
if ( taskQueue.length && !config.blocking ) {
const elapsedTime = now() - start;
if ( !setTimeout || config.updateRate <= 0 || elapsedTime < config.updateRate ) {
const task = taskQueue.shift();
Promise.resolve( task() ).then( function() {
if ( !taskQueue.length ) {
advance();
} else {
processTaskQueue( start );
}
} );
} 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
// https://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;
};
}
/**
* This function is called when the ProcessingQueue is done processing all
* items. It handles emitting the final run events.
*/
function done() {
const storage = config.storage;
const runtime = now() - config.started;
const passed = config.stats.all - config.stats.bad;
// We have reached the end of the processing queue, but there is one more
// thing we'd like to report as a possible failing test. For this to work
// we need to call `onUncaughtException` before closing `ProcessingQueue.finished`.
// However, we do need to call `advance()` in order to resume the processing queue.
// Once this new test is finished processing, we'll reach `done` again, and
// that time the below condition should evaluate to false.
if ( config.stats.testCount === 0 && config.failOnZeroTests === true ) {
let error;
if ( config.filter && config.filter.length ) {
error = new Error( `No tests matched the filter "${config.filter}".` );
} else if ( config.module && config.module.length ) {
error = new Error( `No tests matched the module "${config.module}".` );
} else if ( config.moduleId && config.moduleId.length ) {
error = new Error( `No tests matched the moduleId "${config.moduleId}".` );
} else if ( config.testId && config.testId.length ) {
error = new Error( `No tests matched the testId "${config.testId}".` );
} else {
error = new Error( "No tests were run." );
}
onUncaughtException( error );
advance();
return;
}
ProcessingQueue.finished = true;
emit( "runEnd", globalSuite.end( true ) );
runLoggingCallbacks( "done", {
passed,
failed: config.stats.bad,
total: config.stats.all,
runtime
} ).then( () => {
// Clear own storage items if all tests passed
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 );
}
}
}
} );
}
const ProcessingQueue = {
finished: false,
add: addToTestQueue,
advance,
taskCount: taskQueueLength
};
export default ProcessingQueue;