2012-12-23 22:18:10 +08:00
|
|
|
var helper = require(__dirname + '/../test-helper');
|
|
|
|
var CopyFromStream = require(__dirname + '/../../../lib/copystream').CopyFromStream;
|
|
|
|
var ConnectionImitation = function () {
|
|
|
|
this.send = 0;
|
|
|
|
this.hasToBeSend = 0;
|
|
|
|
this.finished = 0;
|
|
|
|
};
|
|
|
|
ConnectionImitation.prototype = {
|
|
|
|
endCopyFrom: function () {
|
|
|
|
assert.ok(this.finished++ === 0, "end shoud be called only once");
|
|
|
|
assert.equal(this.send, this.hasToBeSend, "at the moment of the end all data has to be sent");
|
|
|
|
},
|
|
|
|
sendCopyFromChunk: function (chunk) {
|
|
|
|
this.send += chunk.length;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
updateHasToBeSend: function (chunk) {
|
|
|
|
this.hasToBeSend += chunk.length;
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var buf1 = new Buffer("asdfasd"),
|
|
|
|
buf2 = new Buffer("q03r90arf0aospd;"),
|
|
|
|
buf3 = new Buffer(542),
|
|
|
|
buf4 = new Buffer("93jfemialfjkasjlfas");
|
|
|
|
|
2012-12-24 23:37:38 +08:00
|
|
|
test('CopyFromStream, start streaming before data, end after data. no drain event', function () {
|
2012-12-23 22:18:10 +08:00
|
|
|
var stream = new CopyFromStream();
|
|
|
|
var conn = new ConnectionImitation();
|
|
|
|
stream.on('drain', function () {
|
|
|
|
assert.ok(false, "there has not be drain event");
|
|
|
|
});
|
|
|
|
stream.startStreamingToConnection(conn);
|
|
|
|
assert.ok(stream.write(conn.updateHasToBeSend(buf1)));
|
|
|
|
assert.ok(stream.write(conn.updateHasToBeSend(buf2)));
|
|
|
|
assert.ok(stream.write(conn.updateHasToBeSend(buf3)));
|
|
|
|
assert.ok(stream.writable, "stream has to be writable");
|
|
|
|
stream.end(conn.updateHasToBeSend(buf4));
|
|
|
|
assert.ok(!stream.writable, "stream has not to be writable");
|
|
|
|
stream.end();
|
2012-12-24 23:37:38 +08:00
|
|
|
assert.equal(conn.hasToBeSend, conn.send);
|
2012-12-23 22:18:10 +08:00
|
|
|
});
|
2012-12-24 23:37:38 +08:00
|
|
|
test('CopyFromStream, start streaming after end, end after data. drain event', function () {
|
2012-12-23 22:18:10 +08:00
|
|
|
var stream = new CopyFromStream();
|
|
|
|
assert.emits(stream, 'drain', function() {}, 'drain have to be emitted');
|
|
|
|
var conn = new ConnectionImitation()
|
|
|
|
assert.ok(!stream.write(conn.updateHasToBeSend(buf1)));
|
|
|
|
assert.ok(!stream.write(conn.updateHasToBeSend(buf2)));
|
|
|
|
assert.ok(!stream.write(conn.updateHasToBeSend(buf3)));
|
|
|
|
assert.ok(stream.writable, "stream has to be writable");
|
|
|
|
stream.end(conn.updateHasToBeSend(buf4));
|
|
|
|
assert.ok(!stream.writable, "stream has not to be writable");
|
|
|
|
stream.end();
|
|
|
|
stream.startStreamingToConnection(conn);
|
2012-12-24 23:37:38 +08:00
|
|
|
assert.equal(conn.hasToBeSend, conn.send);
|
2012-12-23 22:18:10 +08:00
|
|
|
});
|
2012-12-24 23:37:38 +08:00
|
|
|
test('CopyFromStream, start streaming between data chunks. end after data. drain event', function () {
|
2012-12-23 22:18:10 +08:00
|
|
|
var stream = new CopyFromStream();
|
|
|
|
var conn = new ConnectionImitation()
|
|
|
|
assert.emits(stream, 'drain', function() {}, 'drain have to be emitted');
|
|
|
|
stream.write(conn.updateHasToBeSend(buf1));
|
|
|
|
stream.write(conn.updateHasToBeSend(buf2));
|
|
|
|
stream.startStreamingToConnection(conn);
|
|
|
|
stream.write(conn.updateHasToBeSend(buf3));
|
|
|
|
assert.ok(stream.writable, "stream has to be writable");
|
|
|
|
stream.end(conn.updateHasToBeSend(buf4));
|
2012-12-24 23:37:38 +08:00
|
|
|
assert.equal(conn.hasToBeSend, conn.send);
|
2012-12-23 22:18:10 +08:00
|
|
|
assert.ok(!stream.writable, "stream has not to be writable");
|
|
|
|
stream.end();
|
|
|
|
});
|
2012-12-24 23:37:38 +08:00
|
|
|
test('CopyFromStream, start sreaming before end. end stream with data. drain event', function () {
|
2012-12-23 22:18:10 +08:00
|
|
|
var stream = new CopyFromStream();
|
|
|
|
var conn = new ConnectionImitation()
|
|
|
|
assert.emits(stream, 'drain', function() {}, 'drain have to be emitted');
|
|
|
|
stream.write(conn.updateHasToBeSend(buf1));
|
|
|
|
stream.write(conn.updateHasToBeSend(buf2));
|
|
|
|
stream.write(conn.updateHasToBeSend(buf3));
|
|
|
|
stream.startStreamingToConnection(conn);
|
|
|
|
assert.ok(stream.writable, "stream has to be writable");
|
|
|
|
stream.end(conn.updateHasToBeSend(buf4));
|
2012-12-24 23:37:38 +08:00
|
|
|
assert.equal(conn.hasToBeSend, conn.send);
|
2012-12-23 22:18:10 +08:00
|
|
|
assert.ok(!stream.writable, "stream has not to be writable");
|
|
|
|
stream.end();
|
|
|
|
});
|
2012-12-24 23:37:38 +08:00
|
|
|
test('CopyFromStream, start streaming after end. end with data. drain event', function(){
|
2012-12-23 22:18:10 +08:00
|
|
|
var stream = new CopyFromStream();
|
|
|
|
var conn = new ConnectionImitation()
|
|
|
|
assert.emits(stream, 'drain', function() {}, 'drain have to be emitted');
|
|
|
|
stream.write(conn.updateHasToBeSend(buf1));
|
|
|
|
stream.write(conn.updateHasToBeSend(buf2));
|
|
|
|
stream.write(conn.updateHasToBeSend(buf3));
|
|
|
|
stream.startStreamingToConnection(conn);
|
|
|
|
assert.ok(stream.writable, "stream has to be writable");
|
|
|
|
stream.end(conn.updateHasToBeSend(buf4));
|
|
|
|
stream.startStreamingToConnection(conn);
|
2012-12-24 23:37:38 +08:00
|
|
|
assert.equal(conn.hasToBeSend, conn.send);
|
2012-12-23 22:18:10 +08:00
|
|
|
assert.ok(!stream.writable, "stream has not to be writable");
|
|
|
|
stream.end();
|
|
|
|
});
|