Merge pull request #571 from clark800/fix-pathfind-queue

Fix pathfind queuing and add unit test
This commit is contained in:
Chris Clark
2015-09-29 15:22:38 -07:00
3 changed files with 28 additions and 15 deletions

View File

@@ -1803,9 +1803,20 @@ Remote.prototype.createPathFind = function(options, callback) {
if (callback) {
pathFind.on('update', (data) => {
if (data.full_reply) {
pathFind.close();
if (data.full_reply && !data.closed) {
this._cur_path_find = null;
callback(null, data);
// "A client can only have one pathfinding request open at a time.
// If another pathfinding request is already open on the same
// connection, the old request is automatically closed and replaced
// with the new request."
// - ripple.com/build/rippled-apis/#path-find-create
if (this._queued_path_finds.length > 0) {
const pathfind = this._queued_path_finds.shift();
this.createPathFind(pathfind.options, pathfind.callback);
} else {
pathFind.close();
}
}
});
pathFind.on('error', callback);
@@ -2145,19 +2156,8 @@ Remote.prototype.requestPathFindCreate = function(options, callback) {
Remote.prototype.requestPathFindClose = function(callback) {
const request = new Request(this, 'path_find');
request.message.subcommand = 'close';
request.callback((error, data) => {
this._cur_path_find = null;
if (this._queued_path_finds.length > 0) {
const pathfind = this._queued_path_finds.shift();
this.createPathFind(pathfind.options, pathfind.callback);
}
if (callback) {
callback(error, data);
}
});
request.callback(callback);
return request;
};

View File

@@ -570,6 +570,18 @@ describe('RippleAPI', function() {
_.partial(checkResult, responses.getPaths.XrpToUsd, 'getPaths'));
});
it('getPaths - queuing', function() {
return Promise.all([
this.api.getPaths(requests.getPaths.normal),
this.api.getPaths(requests.getPaths.UsdToUsd),
this.api.getPaths(requests.getPaths.XrpToXrp)
]).then(results => {
checkResult(responses.getPaths.XrpToUsd, 'getPaths', results[0]);
checkResult(responses.getPaths.UsdToUsd, 'getPaths', results[1]);
checkResult(responses.getPaths.XrpToXrp, 'getPaths', results[2]);
});
});
// @TODO
// need decide what to do with currencies/XRP:
// if add 'XRP' in currencies, then there will be exception in

View File

@@ -271,7 +271,8 @@ module.exports = function(port) {
request.id, request.source_account, request.destination_account,
request.destination_amount);
}
conn.send(response);
// delay response to simulate calculation time so we can test queuing
setTimeout(() => conn.send(response), 20);
});
return mock;