1 |
|
2 |
|
3 | var mod_tap = require('tap');
|
4 | var mod_vasync = require('..');
|
5 |
|
6 | function
|
7 | latched_worker(task, cb)
|
8 | {
|
9 | if (task.immediate) {
|
10 | cb();
|
11 | } else {
|
12 | task.latched = true;
|
13 | task.unlatch = function () {
|
14 | task.latched = false;
|
15 | cb();
|
16 | };
|
17 | }
|
18 | }
|
19 |
|
20 | function
|
21 | unlatchAll(tasks)
|
22 | {
|
23 | tasks.forEach(function (t) {
|
24 | if (t.latched) {
|
25 | t.unlatch();
|
26 | }
|
27 | });
|
28 | }
|
29 |
|
30 | function
|
31 | setAllImmediate(tasks)
|
32 | {
|
33 | tasks.forEach(function (t) {
|
34 | t.immediate = true;
|
35 | });
|
36 | }
|
37 |
|
38 | mod_tap.test('test serial tasks', function (test) {
|
39 | test.plan(2);
|
40 |
|
41 | var q = mod_vasync.queuev({
|
42 | worker: latched_worker,
|
43 | concurrency: 1
|
44 | });
|
45 | test.ok(q);
|
46 |
|
47 | var tasks = [];
|
48 | for (var i = 0; i < 2; ++i) {
|
49 | tasks.push({
|
50 | 'id': i,
|
51 | 'latched': false,
|
52 | 'immediate': false
|
53 | });
|
54 | }
|
55 |
|
56 | setTimeout(function () {
|
57 | var latched = 0;
|
58 | tasks.forEach(function (t) {
|
59 | if (t.latched) {
|
60 | ++latched;
|
61 | }
|
62 | });
|
63 | test.ok(latched === 1);
|
64 | unlatchAll(tasks);
|
65 | setAllImmediate(tasks);
|
66 | }, 10);
|
67 |
|
68 | q.on('drain', function () {
|
69 | q.close();
|
70 | });
|
71 |
|
72 | q.on('end', function () {
|
73 | test.end();
|
74 | });
|
75 |
|
76 | q.push(tasks);
|
77 | });
|
78 |
|
79 | mod_tap.test('test parallel tasks', function (test) {
|
80 | test.plan(2);
|
81 |
|
82 | var q = mod_vasync.queuev({
|
83 | worker: latched_worker,
|
84 | concurrency: 2
|
85 | });
|
86 | test.ok(q);
|
87 |
|
88 | var tasks = [];
|
89 | for (var i = 0; i < 3; ++i) {
|
90 | tasks.push({
|
91 | 'id': i,
|
92 | 'latched': false,
|
93 | 'immediate': false
|
94 | });
|
95 | }
|
96 |
|
97 | setTimeout(function () {
|
98 | var latched = 0;
|
99 | tasks.forEach(function (t) {
|
100 | if (t.latched) {
|
101 | ++latched;
|
102 | }
|
103 | });
|
104 | test.ok(latched === 2);
|
105 | unlatchAll(tasks);
|
106 | setAllImmediate(tasks);
|
107 | }, 10);
|
108 |
|
109 | q.on('drain', function () {
|
110 | q.close();
|
111 | });
|
112 |
|
113 | q.on('end', function () {
|
114 | test.end();
|
115 | });
|
116 |
|
117 | q.push(tasks);
|
118 | });
|
119 |
|
120 | mod_tap.test('test ratchet up and down', function (test) {
|
121 | test.plan(8);
|
122 |
|
123 | var q = mod_vasync.queuev({
|
124 | worker: latched_worker,
|
125 | concurrency: 2
|
126 | });
|
127 | test.ok(q);
|
128 |
|
129 | var bounced = 0;
|
130 | var tasks = [];
|
131 | for (var i = 0; i < 21; ++i) {
|
132 | tasks.push({
|
133 | 'id': i,
|
134 | 'latched': false,
|
135 | 'immediate': false
|
136 | });
|
137 | }
|
138 |
|
139 | function count() {
|
140 | var latched = 0;
|
141 | tasks.forEach(function (t) {
|
142 | if (t.latched) {
|
143 | ++latched;
|
144 | }
|
145 | });
|
146 | return (latched);
|
147 | }
|
148 |
|
149 | function fiveLatch() {
|
150 | if (!q.closed) {
|
151 | ++bounced;
|
152 | test.ok(count() === 5);
|
153 | q.updateConcurrency(2);
|
154 | unlatchAll(tasks);
|
155 | setTimeout(twoLatch, 10);
|
156 | }
|
157 | }
|
158 |
|
159 | function twoLatch() {
|
160 | if (!q.closed) {
|
161 | ++bounced;
|
162 | test.ok(count() === 2);
|
163 | q.updateConcurrency(5);
|
164 | unlatchAll(tasks);
|
165 | setTimeout(fiveLatch, 10);
|
166 | }
|
167 | }
|
168 | setTimeout(twoLatch, 10);
|
169 |
|
170 | q.on('drain', function () {
|
171 | q.close();
|
172 | });
|
173 |
|
174 | q.on('end', function () {
|
175 |
|
176 | test.ok(bounced === 6);
|
177 | test.end();
|
178 | });
|
179 |
|
180 | q.push(tasks);
|
181 | });
|