diff --git a/src/index.js b/src/index.js index 657cfd71..a98ad718 100644 --- a/src/index.js +++ b/src/index.js @@ -456,6 +456,7 @@ class KadDHT { * @param {CID} key * @param {Object} options - findProviders options * @param {number} options.maxTimeout - how long the query should maximally run, in milliseconds (default: 60000) + * @param {number} options.maxNumProviders - maximum number of providers to find * @param {function(Error, Array)} callback * @returns {void} */ @@ -471,12 +472,11 @@ class KadDHT { options = options || {} } - if (!options.maxTimeout) { - options.maxTimeout = c.minute - } + options.maxTimeout = options.maxTimeout || c.minute + options.maxNumProviders = options.maxNumProviders || c.K this._log('findProviders %s', key.toBaseEncodedString()) - this._findNProviders(key, options.maxTimeout, c.K, callback) + this._findNProviders(key, options.maxTimeout, options.maxNumProviders, callback) } // ----------- Peer Routing diff --git a/test/kad-dht.spec.js b/test/kad-dht.spec.js index d49c90d1..0a184cdf 100644 --- a/test/kad-dht.spec.js +++ b/test/kad-dht.spec.js @@ -342,6 +342,37 @@ describe('KadDHT', () => { }) }) + it('find providers', function (done) { + this.timeout(20 * 1000) + + const val = values[0] + const tdht = new TestDHT() + + tdht.spawn(3, (err, dhts) => { + expect(err).to.not.exist() + + series([ + (cb) => connect(dhts[0], dhts[1], cb), + (cb) => connect(dhts[1], dhts[2], cb), + (cb) => each(dhts, (dht, cb) => dht.provide(val.cid, cb), cb), + (cb) => dhts[0].findProviders(val.cid, {}, cb), + (cb) => dhts[0].findProviders(val.cid, { maxNumProviders: 2 }, cb) + ], (err, res) => { + expect(err).to.not.exist() + + // find providers find all the 3 providers + expect(res[3]).to.exist() + expect(res[3]).to.have.length(3) + + // find providers limited to a maxium of 2 providers + expect(res[4]).to.exist() + expect(res[4]).to.have.length(2) + + done() + }) + }) + }) + it('random-walk', function (done) { this.timeout(40 * 1000)