priority.test.js
1.41 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
/**
*
* Priority test
* 1st task added expected to execute firstly, ignoring the priority
* following tasks expect to abide the priority
*
*/
'use strict';
var Crawler = require('../lib/crawler');
var expect = require('chai').expect;
var nock = require('nock');
var c;
var spf = [0, 0, 0, 0];
var cnt = 0;
describe('Priority test', function () {
before(function () {
nock.cleanAll();
nock('http://nockHost').get(uri => uri.indexOf('links') >= 0).times(4).reply(200, 'Yes');
c = new Crawler({
jquery: false,
maxConnections: 1
});
c.queue([{
uri: 'http://nockHost/links/0',
priority: 4,
callback: function (error, result, done) {
spf[cnt++] = 3;
done();
}
}]);
c.queue([{
uri: 'http://nockHost/links/4',
priority: 3,
callback: function (error, result, done) {
spf[cnt++] = 4;
done();
}
}]);
c.queue([{
uri: 'http://nockHost/links/5',
priority: 2,
callback: function (error, result, done) {
spf[cnt++] = 5;
done();
}
}]);
c.queue([{
uri: 'http://nockHost/links/6',
priority: 1,
callback: function (error, result, done) {
spf[cnt++] = 6;
done();
}
}]);
});
it('should execute in order', function (done) {
this.timeout(5000);
setTimeout(function () {
expect(spf[0]).to.equal(3);
expect(spf[1]).to.equal(6);
expect(spf[2]).to.equal(5);
expect(spf[3]).to.equal(4);
done();
}, 1000);
});
});