summaryrefslogtreecommitdiff
path: root/mocha_test/priorityQueue.js
blob: b7778151d5071d55cf9656c94b93f496e64048ae (plain)
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
var async = require('../lib');
var expect = require('chai').expect;

describe('priorityQueue', function() {

    it('priorityQueue', function (done) {
        var call_order = [];

        // order of completion: 2,1,4,3

        var q = async.priorityQueue(function (task, callback) {
            call_order.push('process ' + task);
            callback('error', 'arg');
        }, 1);

        q.push(1, 1.4, function (err, arg) {
            expect(err).to.equal('error');
            expect(arg).to.equal('arg');
            expect(q.length()).to.equal(2);
            call_order.push('callback ' + 1);
        });
        q.push(2, 0.2, function (err, arg) {
            expect(err).to.equal('error');
            expect(arg).to.equal('arg');
            expect(q.length()).to.equal(3);
            call_order.push('callback ' + 2);
        });
        q.push(3, 3.8, function (err, arg) {
            expect(err).to.equal('error');
            expect(arg).to.equal('arg');
            expect(q.length()).to.equal(0);
            call_order.push('callback ' + 3);
        });
        q.push(4, 2.9, function (err, arg) {
            expect(err).to.equal('error');
            expect(arg).to.equal('arg');
            expect(q.length()).to.equal(1);
            call_order.push('callback ' + 4);
        });
        expect(q.length()).to.equal(4);
        expect(q.concurrency).to.equal(1);

        q.drain = function () {
            expect(call_order).to.eql([
                'process 2', 'callback 2',
                'process 1', 'callback 1',
                'process 4', 'callback 4',
                'process 3', 'callback 3'
            ]);
            expect(q.concurrency).to.equal(1);
            expect(q.length()).to.equal(0);
            done();
        };
    });

    it('concurrency', function (done) {
        var call_order = [],
            delays = [160,80,240,80];

        // worker1: --2-3
        // worker2: -1---4
        // order of completion: 1,2,3,4

        var q = async.priorityQueue(function (task, callback) {
            setTimeout(function () {
                call_order.push('process ' + task);
                callback('error', 'arg');
            }, delays.splice(0,1)[0]);
        }, 2);

        q.push(1, 1.4, function (err, arg) {
            expect(err).to.equal('error');
            expect(arg).to.equal('arg');
            expect(q.length()).to.equal(2);
            call_order.push('callback ' + 1);
        });
        q.push(2, 0.2, function (err, arg) {
            expect(err).to.equal('error');
            expect(arg).to.equal('arg');
            expect(q.length()).to.equal(1);
            call_order.push('callback ' + 2);
        });
        q.push(3, 3.8, function (err, arg) {
            expect(err).to.equal('error');
            expect(arg).to.equal('arg');
            expect(q.length()).to.equal(0);
            call_order.push('callback ' + 3);
        });
        q.push(4, 2.9, function (err, arg) {
            expect(err).to.equal('error');
            expect(arg).to.equal('arg');
            expect(q.length()).to.equal(0);
            call_order.push('callback ' + 4);
        });
        expect(q.length()).to.equal(4);
        expect(q.concurrency).to.equal(2);

        q.drain = function () {
            expect(call_order).to.eql([
                'process 1', 'callback 1',
                'process 2', 'callback 2',
                'process 3', 'callback 3',
                'process 4', 'callback 4'
            ]);
            expect(q.concurrency).to.equal(2);
            expect(q.length()).to.equal(0);
            done();
        };
    });

    context('q.saturated(): ', function() {
        it('should call the saturated callback if tasks length is concurrency', function(done) {
            var calls = [];
            var q = async.priorityQueue(function(task, cb) {
                calls.push('process ' + task);
                async.setImmediate(cb);
            }, 4);
            q.saturated = function() {
                calls.push('saturated');
            };
            q.empty = function() {
                expect(calls.indexOf('saturated')).to.be.above(-1);
                setTimeout(function() {
                    expect(calls).eql([
                        'process foo4',
                        'process foo3',
                        'process foo2',
                        "saturated",
                        'process foo1',
                        'foo4 cb',
                        "saturated",
                        'process foo0',
                        'foo3 cb',
                        'foo2 cb',
                        'foo1 cb',
                        'foo0 cb'
                    ]);
                    done();
                }, 50);
            };
            q.push('foo0', 5, function () {calls.push('foo0 cb');});
            q.push('foo1', 4, function () {calls.push('foo1 cb');});
            q.push('foo2', 3, function () {calls.push('foo2 cb');});
            q.push('foo3', 2, function () {calls.push('foo3 cb');});
            q.push('foo4', 1, function () {calls.push('foo4 cb');});
        });
    });

    context('q.unsaturated(): ',function() {
        it('should have a default buffer property that equals 25% of the concurrenct rate', function(done) {
            var calls = [];
            var q = async.priorityQueue(function(task, cb) {
                // nop
                calls.push('process ' + task);
                async.setImmediate(cb);
            }, 10);
            expect(q.buffer).to.equal(2.5);
            done();
        });

        it('should allow a user to change the buffer property', function(done) {
            var calls = [];
            var q = async.priorityQueue(function(task, cb) {
                // nop
                calls.push('process ' + task);
                async.setImmediate(cb);
            }, 10);
            q.buffer = 4;
            expect(q.buffer).to.not.equal(2.5);
            expect(q.buffer).to.equal(4);
            done();
        });

        it('should call the unsaturated callback if tasks length is less than concurrency minus buffer', function(done) {
            var calls = [];
            var q = async.priorityQueue(function(task, cb) {
                calls.push('process ' + task);
                setTimeout(cb, 10);
            }, 4);
            q.unsaturated = function() {
                calls.push('unsaturated');
            };
            q.empty = function() {
                expect(calls.indexOf('unsaturated')).to.be.above(-1);
                setTimeout(function() {
                    expect(calls).eql([
                        'process foo4',
                        'process foo3',
                        'process foo2',
                        'process foo1',
                        'foo4 cb',
                        'unsaturated',
                        'process foo0',
                        'foo3 cb',
                        'unsaturated',
                        'foo2 cb',
                        'unsaturated',
                        'foo1 cb',
                        'unsaturated',
                        'foo0 cb',
                        'unsaturated'
                    ]);
                    done();
                }, 50);
            };
            q.push('foo0', 5, function () {calls.push('foo0 cb');});
            q.push('foo1', 4, function () {calls.push('foo1 cb');});
            q.push('foo2', 3, function () {calls.push('foo2 cb');});
            q.push('foo3', 2, function () {calls.push('foo3 cb');});
            q.push('foo4', 1, function () {calls.push('foo4 cb');});
        });
    });
});