Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
chore: enable name-pubsub core test (#2003)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored and Alan Shaw committed Apr 29, 2019
1 parent dd817ad commit 240c0b3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"homepage": "https://github.com/ipfs/js-ipfs#readme",
"devDependencies": {
"aegir": "^18.1.0",
"base64url": "^3.0.1",
"chai": "^4.2.0",
"delay": "^4.1.0",
"detect-node": "^2.0.4",
Expand Down
58 changes: 51 additions & 7 deletions test/core/name-pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const base64url = require('base64url')
const { fromB58String } = require('multihashes')
const parallel = require('async/parallel')
const retry = require('async/retry')
const series = require('async/series')

const isNode = require('detect-node')
const ipns = require('ipns')
const IPFS = require('../../src')
const waitFor = require('../utils/wait-for')

const DaemonFactory = require('ipfsd-ctl')
const df = DaemonFactory.create({ type: 'proc' })

const namespace = '/record/'
const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'

describe('name-pubsub', function () {
Expand Down Expand Up @@ -76,19 +83,56 @@ describe('name-pubsub', function () {
it('should publish and then resolve correctly', function (done) {
this.timeout(80 * 1000)

let subscribed = false

function checkMessage (msg) {
subscribed = true
}

const alreadySubscribed = (cb) => {
return cb(null, subscribed === true)
}

// Wait until a peer subscribes a topic
const waitForPeerToSubscribe = (node, topic, callback) => {
retry({
times: 5,
interval: 2000
}, (next) => {
node.pubsub.peers(topic, (error, res) => {
if (error) {
return next(error)
}

if (!res || !res.length) {
return next(new Error('Could not find subscription'))
}

return next(null, res)
})
}, callback)
}

const keys = ipns.getIdKeys(fromB58String(idA.id))
const topic = `${namespace}${base64url.encode(keys.routingKey.toBuffer())}`

nodeB.name.resolve(idA.id, (err) => {
expect(err).to.exist()

nodeA.name.publish(ipfsRef, { resolve: false }, (err, res) => {
series([
(cb) => waitForPeerToSubscribe(nodeA, topic, cb),
(cb) => nodeB.pubsub.subscribe(topic, checkMessage, cb),
(cb) => nodeA.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => waitFor((callback) => alreadySubscribed(callback), cb),
(cb) => setTimeout(() => cb(), 1000), // guarantee record is written
(cb) => nodeB.name.resolve(idA.id, cb)
], (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()

nodeB.name.resolve(idA.id, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res.path).to.equal(ipfsRef)
done()
})
expect(res[5]).to.exist()
expect(res[5].path).to.equal(ipfsRef)
done()
})
})
})
Expand Down
1 change: 1 addition & 0 deletions test/core/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require('./circuit-relay')
require('./files-regular-utils')
require('./name')
require('./name-pubsub')
require('./key-exchange')
require('./pin')
require('./pin-set')
Expand Down

0 comments on commit 240c0b3

Please sign in to comment.