|
| 1 | +var helper = require(__dirname + '/../test-helper'); |
| 2 | +var CopyFromStream = require(__dirname + '/../../../lib/copystream').CopyFromStream; |
| 3 | +var ConnectionImitation = function () { |
| 4 | + this.send = 0; |
| 5 | + this.hasToBeSend = 0; |
| 6 | + this.finished = 0; |
| 7 | +}; |
| 8 | +ConnectionImitation.prototype = { |
| 9 | + endCopyFrom: function () { |
| 10 | + assert.ok(this.finished++ === 0, "end shoud be called only once"); |
| 11 | + assert.equal(this.send, this.hasToBeSend, "at the moment of the end all data has to be sent"); |
| 12 | + }, |
| 13 | + sendCopyFromChunk: function (chunk) { |
| 14 | + this.send += chunk.length; |
| 15 | + return true; |
| 16 | + }, |
| 17 | + updateHasToBeSend: function (chunk) { |
| 18 | + this.hasToBeSend += chunk.length; |
| 19 | + return chunk; |
| 20 | + } |
| 21 | +}; |
| 22 | +var buf1 = new Buffer("asdfasd"), |
| 23 | + buf2 = new Buffer("q03r90arf0aospd;"), |
| 24 | + buf3 = new Buffer(542), |
| 25 | + buf4 = new Buffer("93jfemialfjkasjlfas"); |
| 26 | + |
| 27 | +test('stream has to finish data stream to connection exactly once', function () { |
| 28 | + var stream = new CopyFromStream(); |
| 29 | + var conn = new ConnectionImitation(); |
| 30 | + stream.on('drain', function () { |
| 31 | + assert.ok(false, "there has not be drain event"); |
| 32 | + }); |
| 33 | + stream.startStreamingToConnection(conn); |
| 34 | + assert.ok(stream.write(conn.updateHasToBeSend(buf1))); |
| 35 | + assert.ok(stream.write(conn.updateHasToBeSend(buf2))); |
| 36 | + assert.ok(stream.write(conn.updateHasToBeSend(buf3))); |
| 37 | + assert.ok(stream.writable, "stream has to be writable"); |
| 38 | + stream.end(conn.updateHasToBeSend(buf4)); |
| 39 | + assert.ok(!stream.writable, "stream has not to be writable"); |
| 40 | + stream.end(); |
| 41 | +}); |
| 42 | +test('', function () { |
| 43 | + var stream = new CopyFromStream(); |
| 44 | + assert.emits(stream, 'drain', function() {}, 'drain have to be emitted'); |
| 45 | + var conn = new ConnectionImitation() |
| 46 | + assert.ok(!stream.write(conn.updateHasToBeSend(buf1))); |
| 47 | + assert.ok(!stream.write(conn.updateHasToBeSend(buf2))); |
| 48 | + assert.ok(!stream.write(conn.updateHasToBeSend(buf3))); |
| 49 | + assert.ok(stream.writable, "stream has to be writable"); |
| 50 | + stream.end(conn.updateHasToBeSend(buf4)); |
| 51 | + assert.ok(!stream.writable, "stream has not to be writable"); |
| 52 | + stream.end(); |
| 53 | + stream.startStreamingToConnection(conn); |
| 54 | +}); |
| 55 | +test('', function () { |
| 56 | + var stream = new CopyFromStream(); |
| 57 | + var conn = new ConnectionImitation() |
| 58 | + assert.emits(stream, 'drain', function() {}, 'drain have to be emitted'); |
| 59 | + stream.write(conn.updateHasToBeSend(buf1)); |
| 60 | + stream.write(conn.updateHasToBeSend(buf2)); |
| 61 | + stream.startStreamingToConnection(conn); |
| 62 | + stream.write(conn.updateHasToBeSend(buf3)); |
| 63 | + assert.ok(stream.writable, "stream has to be writable"); |
| 64 | + stream.end(conn.updateHasToBeSend(buf4)); |
| 65 | + assert.ok(!stream.writable, "stream has not to be writable"); |
| 66 | + stream.end(); |
| 67 | +}); |
| 68 | +test('', function () { |
| 69 | + var stream = new CopyFromStream(); |
| 70 | + var conn = new ConnectionImitation() |
| 71 | + assert.emits(stream, 'drain', function() {}, 'drain have to be emitted'); |
| 72 | + stream.write(conn.updateHasToBeSend(buf1)); |
| 73 | + stream.write(conn.updateHasToBeSend(buf2)); |
| 74 | + stream.write(conn.updateHasToBeSend(buf3)); |
| 75 | + stream.startStreamingToConnection(conn); |
| 76 | + assert.ok(stream.writable, "stream has to be writable"); |
| 77 | + stream.end(conn.updateHasToBeSend(buf4)); |
| 78 | + assert.ok(!stream.writable, "stream has not to be writable"); |
| 79 | + stream.end(); |
| 80 | +}); |
| 81 | +test('', function(){ |
| 82 | + var stream = new CopyFromStream(); |
| 83 | + var conn = new ConnectionImitation() |
| 84 | + assert.emits(stream, 'drain', function() {}, 'drain have to be emitted'); |
| 85 | + stream.write(conn.updateHasToBeSend(buf1)); |
| 86 | + stream.write(conn.updateHasToBeSend(buf2)); |
| 87 | + stream.write(conn.updateHasToBeSend(buf3)); |
| 88 | + stream.startStreamingToConnection(conn); |
| 89 | + assert.ok(stream.writable, "stream has to be writable"); |
| 90 | + stream.end(conn.updateHasToBeSend(buf4)); |
| 91 | + stream.startStreamingToConnection(conn); |
| 92 | + assert.ok(!stream.writable, "stream has not to be writable"); |
| 93 | + stream.end(); |
| 94 | +}); |
0 commit comments