rafa
Version:
Rafa.js is a Javascript framework for building concurrent applications.
100 lines (84 loc) • 3.06 kB
JavaScript
module.exports = (assert, Rafa) => {
suite("split", () => {
// split without downstream async
test("sync split", done => {
var values = [];
var stream = Rafa.stream();
stream
.split(value => value.split(','))
.each(value => values.push(value));
Rafa.message("a,b").push(stream);
setTimeout(() => {
assert.equal(values.length, 2);
assert.equal(values[0], 'a');
assert.equal(values[1], 'b');
done();
});
});
// Backpressure should be applied when downstream nodes return promises
test("pressure", done => {
var values = [];
var stream = Rafa.stream();
var futures = [Rafa.future(), Rafa.future()];
stream
.split(value => value.split(','))
.map(value => futures[value].promise)
.each(value => values.push(value));
// push message should split the the first value '0' will be mapped
// to a promise. no values should be stored in the results yet.
Rafa.message("0,1").push(stream);
assert.equal(values.length, 0);
setTimeout(() => {
// resolving the promise should push the promise value into the
// values array and then the next value from split will be released
futures[0].resolve(10);
setTimeout(() => {
assert.equal(values.length, 1);
assert.equal(values[0], 10);
// resolving the second promise will push the value into the values
// array
futures[1].resolve(20);
setTimeout(() => {
assert.equal(values.length, 2);
assert.equal(values[1], 20);
done();
});
});
});
});
// Split without backpressure should push messages downstream immediately
// rather than waiting for the promises to complete
test("release", done => {
var values = [];
var stream = Rafa.stream();
var futures = [Rafa.future(), Rafa.future()];
stream
.split(value => value.split(','))
.each(value => values.push(value))
.map(value => futures[value].promise)
.each(value => values.push(value));
// push message should split the string and push each value to
// downstream nodes right away (after clearing the event stack)
Rafa.message("0,1").push(stream);
assert.equal(values.length, 2);
// proof that both values 0 and 1 made their way downstream
// without waiting
assert.equal(values.length, 2);
assert.equal(values[0], 0);
assert.equal(values[1], 1);
// resolve the first promise should push downstream
futures[0].resolve(10);
setTimeout(() => {
assert.equal(values.length, 3);
assert.equal(values[2], 10);
// resolve the second promise should push downstream
futures[1].resolve(20);
setTimeout(() => {
assert.equal(values.length, 4);
assert.equal(values[3], 20);
done();
});
});
});
});
};