-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
299 lines (287 loc) · 8.49 KB
/
test.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const chai = require('chai');
const expect = chai.expect;
const Contextizer = require('./');
const delay = delay => new Promise(resolve => setTimeout(resolve, delay));
describe('Contextizer', function() {
it('test basic', function(done) {
let context = new Contextizer();
context.register('final').asFunction({ // Test out of order.
deps: [ 'left', 'right' ],
func({ left, right }) {
return [ left, right ];
}
});
context.register('rootVal').asConstant('hello');
context.register('left').asFunction({
deps: [ 'rootVal' ],
func({ rootVal, suffix }) {
return rootVal + ' ' + suffix;
},
params: {
suffix: 'world'
}
});
context.register('right').asFunction({
deps: [ 'rootVal' ],
func({ rootVal }) {
return rootVal + ' darkness my old friend';
}
});
context.execute('final')
.then(vals => {
expect(vals[0]).to.equal('hello world');
expect(vals[1]).to.equal('hello darkness my old friend');
})
.then(done, done);
});
it('test async', function(done) {
let context = new Contextizer();
context.register('final').asFunction({ // Test out of order.
deps: [ 'left', 'right' ],
func({ left, right }) {
return [ left, right ];
}
});
context.register('rootVal').asConstant(delay(10).then(() => 'hello'));
context.register('left').asFunction({
deps: [ 'rootVal' ],
func({ rootVal, suffix }) {
return rootVal + ' ' + suffix;
},
params: {
suffix: 'world'
}
});
context.register('right').asFunction({
deps: [ 'rootVal' ],
func({ rootVal }) {
return delay(30).then(() => rootVal + ' darkness my old friend');
}
});
context.execute('final')
.then(vals => {
expect(vals[0]).to.equal('hello world');
expect(vals[1]).to.equal('hello darkness my old friend');
})
.then(done, done);
});
it('test params', function(done) {
let context = new Contextizer();
context.register('params').asFunction({
deps: [],
func({ vals }) {
return vals.pop();
},
params: {
vals: [ 5, 4, 3, 2, 1 ]
}
});
context.execute('params').then(v => expect(v).to.equal(1))
.then(() => context.execute('params')).then(v => expect(v).to.equal(2))
.then(() => context.execute('params')).then(v => expect(v).to.equal(3))
.then(() => done(), done);
});
it('test cached', function(done) {
let context = new Contextizer();
context.register('cached').asFunction({
cached: true,
deps: [],
func({ vals }) {
return vals.pop();
},
params: {
vals: [ 5, 4, 3, 2, 1 ]
}
});
context.execute('cached').then(v => expect(v).to.equal(1))
.then(() => context.execute('cached')).then(v => expect(v).to.equal(1))
.then(() => done(), done);
});
it('test cleanup', function(done) {
let context = new Contextizer();
// fake stack
context.register('val').asFunction({
deps: [],
func({ vals }) {
return vals.pop();
},
cleanup({ $value, vals }) {
vals.push($value);
},
params: {
vals: [ 5, 4, 3, 2, 1 ]
}
});
context.register('result').asFunction({
deps: [ 'val' ],
func({ val }) {
return delay(50).then(() => val);
}
});
let a = context.execute('result').then(v => expect(v).to.equal(1));
let b = context.execute('result').then(v => expect(v).to.equal(2));
Promise.all([ a, b ])
.then(() => delay(10))
.then(() => context.execute('result'))
.then(v => expect(v).to.be.lessThan(3))
.then(() => done(), done);
});
it('test cleanup with error', function(done) {
// Run previous stack test but throw error first two times
// Check that the stack is repaired even after throw.
let context = new Contextizer();
context.register('doThrow').asInput();
// fake stack
context.register('val').asFunction({
deps: [],
func({ vals }) {
return vals.pop();
},
cleanup({ $value, vals }) {
vals.push($value);
},
params: {
vals: [ 5, 4, 3, 2, 1 ]
}
});
context.register('result').asFunction({
deps: [ 'val', 'doThrow' ],
func({ val, doThrow }) {
return delay(50).then(() => {
if (doThrow)
throw 'err';
return val;
});
}
});
let a = context.execute('result', { doThrow: true })
.catch(v => expect(v).to.equal('err'));
let b = context.execute('result', { doThrow: true })
.catch(v => expect(v).to.equal('err'));
Promise.all([ a, b ])
.then(() => delay(10))
.then(() => context.execute('result', { doThrow: false }))
.then(v => expect(v).to.be.lessThan(3))
.then(() => done(), done);
});
it('test namespace overlap', function(done) {
let context = new Contextizer();
context.register('foo').asInput();
context.register('com.mingweisamuel.foo').asFunction({
deps: [ 'foo' ],
func({ foo }) {
return 'namespaced ' + foo;
}
});
context.register('com.mingweisamuel.result').asFunction({
deps: [ '.foo', 'com.mingweisamuel.foo' ],
func({ '.foo': foo, 'com.mingweisamuel.foo': foo2 }) {
return { foo, foo2 };
}
});
context.execute('com.mingweisamuel.result', { foo: 'foo' })
.then(value => {
expect(value.foo).to.equal('foo');
expect(value.foo2).to.equal('namespaced foo');
})
.then(done, done);
});
it('test circular', function(done) {
let context = new Contextizer();
context.register('grass').asFunction({
deps: [ 'dirt' ],
func({ dirt }) {
return dirt + ' -> grass';
}
});
context.register('cow').asFunction({
deps: [ 'grass' ],
func({ grass }) {
return grass + ' -> cow';
}
});
context.register('pie').asFunction({
deps: [ 'cow' ],
func({ cow }) {
return cow + ' -> pie';
}
});
context.register('human').asFunction({
deps: [ 'pie' ],
func({ pie }) {
return pie + ' -> human. Yum!';
}
});
context.register('dirt').asFunction({
deps: [ 'pie' ],
func({ pie }) {
return pie + ' -> dirt';
}
});
try {
context.execute('human').then(v => done('unexpected value ' + v));
}
catch(e) {
expect(e.message).to
.equal('Dependency Cycle Found: human -> pie -> cow -> grass -> dirt -> pie');
done();
}
});
it('test fibonacci params', function(done) {
let context = new Contextizer();
context.register('fib').asFunction({
deps: [], // No dependencies.
func({ vals }) {
vals.push(vals[0] + vals[1]);
return vals.shift();
},
params: {
vals: [ 1, 1 ]
}
});
let promises = [];
for (let i = 0; i < 10; i++) {
promises.push(context.execute('fib'));
}
Promise.all(promises)
.then(values => {
expect(values).to.deep.equal([ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]);
})
.then(done, done);
});
it('test namespace misc', function(done) {
let context = new Contextizer();
context.register('user').asInput();
context.register(); //TODO
done();
});
for (let pct = 0.25; pct <= 1.0; pct += 0.25) {
let nodes = 48 + 24 * (1 - pct);
it(`test dense tree ${100 * pct}%, ${nodes} nodes`, function(done) {
let vals = [];
let context = new Contextizer();
for (let i = 0; i < nodes; i++) {
let deps = []; // Always required
vals[i] = i;
for (let j = 0; j < i; j++) {
if (Math.random() < pct || j === i - 1) {
vals[i] += vals[j];
deps.unshift(j);
}
}
deps = deps.map(dep => '' + dep); // Convert to string.
context.register('' + i).asFunction({
deps,
func(arg) {
return i + Object.values(arg).reduce((a, b) => a + b, 0);
}
});
}
context.execute('' + (nodes - 1))
.then(value => {
expect(value).to.equal(vals.pop());
})
.then(done, done);
});
}
});