-
Notifications
You must be signed in to change notification settings - Fork 42
/
utils.js
69 lines (67 loc) · 1.62 KB
/
utils.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
var async = require('async');
var utils = require('../lib/utils.js');
var TileRequest = require('../lib/TileRequest.js');
var assert = require('chai').assert;
describe('utils', function() {
describe('wrapWithMaxConcurrency', function() {
var wrapWithMaxConcurrency = utils.wrapWithMaxConcurrency;
it('should limit concurrency', function(done) {
var active = 0;
var maxConcurrency = 5;
var plugin = wrapWithMaxConcurrency([{
serve: function(server, req, callback) {
var id = req.id;
// console.log('Processing #' + id);
++active;
if (active > maxConcurrency) {
throw new Error('Too many active at a time!');
}
var finish = (err, body, headers) => {
--active;
// console.log('Finished #' + id);
callback(err, body, headers);
};
if (req.shouldFail) {
return setTimeout(finish, 50, new Error('Failed'));
}
setTimeout(finish, 50, null, '#' + req.id, {});
},
}], 5);
var result = [];
var runner = (taskId, shouldFail) => {
return callback => {
var req = new TileRequest();
req.id = taskId;
req.shouldFail = shouldFail;
plugin[0].serve(null, req, (err, body, headers) => {
result.push(err ? '#' + taskId + ' failed' : body);
callback();
});
};
};
async.parallel([
runner(1),
runner(2),
runner(3),
runner(4, true),
runner(5),
runner(6),
runner(7),
runner(8),
], err => {
if (err) throw err;
assert.deepEqual(result, [
'#1',
'#2',
'#3',
'#4 failed',
'#5',
'#6',
'#7',
'#8',
]);
done();
});
});
});
});