-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(receiver-stream): add prototype for ReceiverStream
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
var Readable = require('stream').Readable, | ||
util = require('util'); | ||
|
||
function ReceiverStream(link, options) { | ||
Readable.call(this, { objectMode: true }); | ||
this._link = link; | ||
this._increaing = false; | ||
|
||
link.on('message', this._processMessage.bind(this)); | ||
link.on('detached', this._haltProcessing.bind(this)); | ||
link.on('errorReceived', this._haltProcessing.bind(this)); | ||
link.on('creditChange', this._creditChange.bind(this)); | ||
} | ||
util.inherits(ReceiverStream, Readable); | ||
|
||
ReceiverStream.prototype._read = function(size) { | ||
if (this._link.linkCredit <= 0 && !this._increasing) { | ||
return this._link.addCredits(size); | ||
} | ||
}; | ||
|
||
ReceiverStream.prototype._processMessage = function(message) { | ||
if (!this.push(message)) { | ||
return this._link.flow({ linkCredit: 0 }); | ||
} | ||
}; | ||
|
||
ReceiverStream.prototype._haltProcessing = function() { | ||
this.push(null); | ||
}; | ||
|
||
ReceiverStream.prototype._creditChange = function(credits) { | ||
this._increasing = false; | ||
}; | ||
|
||
module.exports = ReceiverStream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
var Promise = require('bluebird'), | ||
AMQPClient = require('../../..').Client, | ||
config = require('./config'), | ||
expect = require('chai').expect; | ||
|
||
var test = {}; | ||
describe('Streams', function() { | ||
|
||
describe('ReadableStream', function() { | ||
beforeEach(function() { | ||
if (!!test.client) delete test.client; | ||
test.client = new AMQPClient(); | ||
return test.client.connect(config.address); | ||
}); | ||
|
||
afterEach(function() { | ||
return test.client.disconnect() | ||
.then(function() { delete test.client; }); | ||
}); | ||
|
||
it('should let you create a receiver link as a readable stream', function(done) { | ||
var expected = Array.apply(null, new Array(100)) | ||
.map(function(a) { return Math.floor(Math.random() * 100); }); | ||
|
||
return Promise.all([ | ||
test.client.createReceiver(config.defaultLink, { stream: true }), | ||
test.client.createSender(config.defaultLink) | ||
]) | ||
.spread(function(stream, sender) { | ||
var count = 0; | ||
stream.on('data', function(data) { | ||
expect(expected[count]).to.eql(data.body); | ||
count++; | ||
if (count === expected.length) done(); | ||
}); | ||
|
||
var promises = []; | ||
for (var i = 0; i < expected.length; ++i) | ||
promises.push(sender.send(expected[i])); | ||
return Promise.all(promises); | ||
}); | ||
}); | ||
|
||
|
||
}); // ReadableStream | ||
|
||
}); |