diff --git a/src/core/remote.js b/src/core/remote.js index c39b437a..638def41 100644 --- a/src/core/remote.js +++ b/src/core/remote.js @@ -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; }; diff --git a/test/api-test.js b/test/api-test.js index 1dbf408e..9e2f4225 100644 --- a/test/api-test.js +++ b/test/api-test.js @@ -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 diff --git a/test/mock-rippled.js b/test/mock-rippled.js index bf662d05..e76f94ae 100644 --- a/test/mock-rippled.js +++ b/test/mock-rippled.js @@ -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;