-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstacks-composed.js
More file actions
191 lines (165 loc) · 3.89 KB
/
Copy pathstacks-composed.js
File metadata and controls
191 lines (165 loc) · 3.89 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
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
'use strict';
var Runtime = require('../');
it('runs callback if fn throws from other stack', function (done) {
var error = Error('on no!');
var runtime = Runtime.create();
function one (next) { next(); }
function two () { throw error; }
runtime.stack(one, runtime.stack(two))(function (err) {
err.should.be.eql(error);
done();
});
});
it('runs callback if error given to next from other stack', function (done) {
var error = Error('on no!');
var runtime = Runtime.create();
function one (next) { next(); }
function two (next) { next(error); }
runtime.stack(one, runtime.stack(two))(function (err) {
err.should.be.eql(error);
done();
});
});
it('runs the callback on completion of all stacks', function (done) {
var runtime = Runtime.create();
var count = 0;
function one (next) {
setTimeout(function () {
++count; next();
}, Math.random()*10);
}
function two (next) {
setTimeout(function () {
++count; next();
}, Math.random()*10*count);
}
runtime.stack(one, runtime.stack(two))(function (err) {
if (err) { return done(err); }
count.should.be.eql(2);
done();
});
});
it('runs stacks in parallel by default', function (done) {
var runtime = Runtime.create();
var stack = '';
function one (next) {
setTimeout(function () {
stack += 'one';
next();
}, Math.random()*10);
}
function two (next) {
stack += 'two';
next();
}
function three (next) {
stack += 'three';
next();
}
function four (next) {
stack += 'four';
next();
}
runtime.stack(
one,
runtime.stack(two),
runtime.stack(three, four)
)(function (err) {
if (err) { return done(err); }
stack.should.not.be.eql('onetwothreefour');
done();
});
});
it('{wait: true} should run stacks in series', function (done) {
var runtime = Runtime.create();
var stack = '';
function one (next) {
setTimeout(function () {
stack += 'one';
next();
}, Math.random()*10);
}
function two (next) {
stack += 'two';
next();
}
function three (next) {
setTimeout(function () {
stack += 'three';
next();
}, Math.random()*10);
}
function four (next) {
stack += 'four';
next();
}
runtime.stack(
one,
runtime.stack(one, two, {wait: true}),
runtime.stack(one, two, three, four, {wait: true}),
four,
{wait: true})(function (err) {
if (err) { return done(err); }
stack.should.be.eql('oneonetwoonetwothreefourfour');
done();
}
);
});
it('series: callback is run after all stacks are finished', function (done) {
var runtime = Runtime.create();
var count = -1;
var stack = [];
function one (next) {
var pos = ++count;
setTimeout(function () {
stack.push(pos);
next();
}, Math.random()*10);
}
runtime.stack(
runtime.stack(one,
runtime.stack(one,
runtime.stack(one,
runtime.stack(one,
runtime.stack(one, {wait: true}),
{wait: true}),
{wait: true}),
{wait: true}),
{wait: true}),
{wait: true})(function (err) {
if (err) { return done(err); }
stack.should.be.eql([0, 1, 2, 3, 4]);
done();
});
});
it('passes arguments when host and completed stack waits', function (done) {
var runtime = Runtime.create();
function one (next, foo) {
foo.should.be.eql(1);
next(null, 2);
}
function two (next, foo) {
foo.should.be.eql(2);
next();
}
runtime.stack(
runtime.stack(one, {wait: true}),
runtime.stack(two),
{wait: true}
)(1, done);
});
it('does NOT pass arguments when stacks does NOT wait', function (done) {
var runtime = Runtime.create();
function one (next, foo) {
foo.should.be.eql(1);
next(null, 2);
}
function two (next, foo) {
foo.should.be.eql(1);
next();
}
runtime.stack(
runtime.stack(one),
runtime.stack(two)
)(1, done);
});