This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert bitswap tests to use async/await
NOTE: the PRs for ipfs/js-ipfs#1670 are being merged into the `feat/async-iterators` branch. Once they're all in we can merge them into master. --- This simply refactors the bitswap tests to use async/await and updates the documentation to remove callback support and propose a better method for documenting the parameters and return values. depends on #390 License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
- Loading branch information
Showing
6 changed files
with
79 additions
and
127 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,29 +1,17 @@ | ||
'use strict' | ||
|
||
const until = require('async/until') | ||
|
||
function waitForWantlistKey (ipfs, key, opts, cb) { | ||
if (typeof opts === 'function') { | ||
cb = opts | ||
opts = {} | ||
} | ||
|
||
async function waitForWantlistKey (ipfs, key, opts) { | ||
opts = opts || {} | ||
opts.timeout = opts.timeout || 1000 | ||
|
||
let list = { Keys: [] } | ||
let timedOut = false | ||
const start = Date.now() | ||
|
||
setTimeout(() => { timedOut = true }, opts.timeout) | ||
|
||
const test = () => timedOut ? true : list.Keys.every(k => k['/'] === key) | ||
const iteratee = (cb) => ipfs.bitswap.wantlist(opts.peerId, cb) | ||
while (Date.now() <= start + opts.timeout) { | ||
const list = await ipfs.bitswap.wantlist(opts.peerId) | ||
if (list.Keys.find(k => k['/'] === key)) return | ||
} | ||
|
||
until(test, iteratee, (err) => { | ||
if (err) return cb(err) | ||
if (timedOut) return cb(new Error(`Timed out waiting for ${key} in wantlist`)) | ||
cb() | ||
}) | ||
throw new Error(`Timed out waiting for ${key} in wantlist`) | ||
} | ||
|
||
module.exports.waitForWantlistKey = waitForWantlistKey |
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