2019-11-15 16:48:32 +01:00
|
|
|
'use strict'
|
|
|
|
/* eslint-env mocha */
|
|
|
|
|
|
|
|
const chai = require('chai')
|
|
|
|
chai.use(require('dirty-chai'))
|
|
|
|
const { expect } = chai
|
|
|
|
const sinon = require('sinon')
|
|
|
|
|
|
|
|
const pWaitFor = require('p-wait-for')
|
|
|
|
const pDefer = require('p-defer')
|
|
|
|
const mergeOptions = require('merge-options')
|
|
|
|
const multiaddr = require('multiaddr')
|
|
|
|
|
|
|
|
const { create } = require('../../src')
|
|
|
|
const { subsystemOptions, subsystemMulticodecs } = require('./utils')
|
|
|
|
const peerUtils = require('../utils/creators/peer')
|
|
|
|
|
|
|
|
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
|
|
|
const remoteListenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
|
|
|
|
|
|
|
describe('Pubsub subsystem operates correctly', () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
let peerId, remotePeerId
|
2019-11-15 16:48:32 +01:00
|
|
|
let libp2p, remoteLibp2p
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
[peerId, remotePeerId] = await peerUtils.createPeerId({ number: 2 })
|
2019-11-15 16:48:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('pubsub started before connect', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
libp2p = await create(mergeOptions(subsystemOptions, {
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId,
|
|
|
|
addresses: {
|
|
|
|
listen: [listenAddr]
|
|
|
|
}
|
2019-11-15 16:48:32 +01:00
|
|
|
}))
|
|
|
|
|
|
|
|
remoteLibp2p = await create(mergeOptions(subsystemOptions, {
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId: remotePeerId,
|
|
|
|
addresses: {
|
|
|
|
listen: [remoteListenAddr]
|
|
|
|
}
|
2019-11-15 16:48:32 +01:00
|
|
|
}))
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
libp2p.start(),
|
|
|
|
remoteLibp2p.start()
|
|
|
|
])
|
2020-04-14 14:05:30 +02:00
|
|
|
|
|
|
|
libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.addresses.listen)
|
2019-11-15 16:48:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => Promise.all([
|
|
|
|
libp2p && libp2p.stop(),
|
|
|
|
remoteLibp2p && remoteLibp2p.stop()
|
|
|
|
]))
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
sinon.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get notified of connected peers on dial', async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
const connection = await libp2p.dialProtocol(remotePeerId, subsystemMulticodecs)
|
2019-11-15 16:48:32 +01:00
|
|
|
|
|
|
|
expect(connection).to.exist()
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
pWaitFor(() => libp2p.pubsub._pubsub.peers.size === 1),
|
|
|
|
pWaitFor(() => remoteLibp2p.pubsub._pubsub.peers.size === 1)
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should receive pubsub messages', async () => {
|
|
|
|
const defer = pDefer()
|
|
|
|
const topic = 'test-topic'
|
|
|
|
const data = 'hey!'
|
2020-04-14 14:05:30 +02:00
|
|
|
const libp2pId = libp2p.peerId.toB58String()
|
2019-11-15 16:48:32 +01:00
|
|
|
|
2020-04-14 14:05:30 +02:00
|
|
|
await libp2p.dialProtocol(remotePeerId, subsystemMulticodecs)
|
2019-11-15 16:48:32 +01:00
|
|
|
|
|
|
|
let subscribedTopics = libp2p.pubsub.getTopics()
|
|
|
|
expect(subscribedTopics).to.not.include(topic)
|
|
|
|
|
|
|
|
libp2p.pubsub.subscribe(topic, (msg) => {
|
|
|
|
expect(msg.data.toString()).to.equal(data)
|
|
|
|
defer.resolve()
|
|
|
|
})
|
|
|
|
|
|
|
|
subscribedTopics = libp2p.pubsub.getTopics()
|
|
|
|
expect(subscribedTopics).to.include(topic)
|
|
|
|
|
|
|
|
// wait for remoteLibp2p to know about libp2p subscription
|
|
|
|
await pWaitFor(() => {
|
2019-12-03 11:29:20 +01:00
|
|
|
const subscribedPeers = remoteLibp2p.pubsub.getSubscribers(topic)
|
2019-11-15 16:48:32 +01:00
|
|
|
return subscribedPeers.includes(libp2pId)
|
|
|
|
})
|
|
|
|
remoteLibp2p.pubsub.publish(topic, data)
|
|
|
|
|
|
|
|
await defer.promise
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('pubsub started after connect', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
libp2p = await create(mergeOptions(subsystemOptions, {
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId,
|
|
|
|
addresses: {
|
|
|
|
listen: [listenAddr]
|
|
|
|
}
|
2019-11-15 16:48:32 +01:00
|
|
|
}))
|
|
|
|
|
|
|
|
remoteLibp2p = await create(mergeOptions(subsystemOptions, {
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId: remotePeerId,
|
|
|
|
addresses: {
|
|
|
|
listen: [remoteListenAddr]
|
|
|
|
},
|
2019-11-15 16:48:32 +01:00
|
|
|
config: {
|
|
|
|
pubsub: {
|
|
|
|
enabled: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
await libp2p.start()
|
|
|
|
await remoteLibp2p.start()
|
2020-04-14 14:05:30 +02:00
|
|
|
|
|
|
|
libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.addresses.listen)
|
2019-11-15 16:48:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => Promise.all([
|
|
|
|
libp2p && libp2p.stop(),
|
|
|
|
remoteLibp2p && remoteLibp2p.stop()
|
|
|
|
]))
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
sinon.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get notified of connected peers after starting', async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
const connection = await libp2p.dial(remotePeerId)
|
2019-11-15 16:48:32 +01:00
|
|
|
|
|
|
|
expect(connection).to.exist()
|
|
|
|
expect(libp2p.pubsub._pubsub.peers.size).to.be.eql(0)
|
|
|
|
expect(remoteLibp2p.pubsub._pubsub.peers.size).to.be.eql(0)
|
|
|
|
|
|
|
|
remoteLibp2p.pubsub.start()
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
pWaitFor(() => libp2p.pubsub._pubsub.peers.size === 1),
|
|
|
|
pWaitFor(() => remoteLibp2p.pubsub._pubsub.peers.size === 1)
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should receive pubsub messages', async function () {
|
|
|
|
this.timeout(10e3)
|
|
|
|
const defer = pDefer()
|
2020-04-14 14:05:30 +02:00
|
|
|
const libp2pId = libp2p.peerId.toB58String()
|
2019-11-15 16:48:32 +01:00
|
|
|
const topic = 'test-topic'
|
|
|
|
const data = 'hey!'
|
|
|
|
|
2020-04-14 14:05:30 +02:00
|
|
|
await libp2p.dial(remotePeerId)
|
2019-11-15 16:48:32 +01:00
|
|
|
|
|
|
|
remoteLibp2p.pubsub.start()
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
pWaitFor(() => libp2p.pubsub._pubsub.peers.size === 1),
|
|
|
|
pWaitFor(() => remoteLibp2p.pubsub._pubsub.peers.size === 1)
|
|
|
|
])
|
|
|
|
|
|
|
|
let subscribedTopics = libp2p.pubsub.getTopics()
|
|
|
|
expect(subscribedTopics).to.not.include(topic)
|
|
|
|
|
|
|
|
libp2p.pubsub.subscribe(topic, (msg) => {
|
|
|
|
expect(msg.data.toString()).to.equal(data)
|
|
|
|
defer.resolve()
|
|
|
|
})
|
|
|
|
|
|
|
|
subscribedTopics = libp2p.pubsub.getTopics()
|
|
|
|
expect(subscribedTopics).to.include(topic)
|
|
|
|
|
|
|
|
// wait for remoteLibp2p to know about libp2p subscription
|
|
|
|
await pWaitFor(() => {
|
2019-12-03 11:29:20 +01:00
|
|
|
const subscribedPeers = remoteLibp2p.pubsub.getSubscribers(topic)
|
2019-11-15 16:48:32 +01:00
|
|
|
return subscribedPeers.includes(libp2pId)
|
|
|
|
})
|
|
|
|
|
|
|
|
remoteLibp2p.pubsub.publish(topic, data)
|
|
|
|
|
|
|
|
await defer.promise
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|