chore: restructure pubsub tests

This commit is contained in:
Vasco Santos
2021-01-20 10:34:47 +01:00
committed by Vasco Santos
parent 2a6a635f13
commit 2c4b567b00
7 changed files with 105 additions and 468 deletions

View File

@@ -0,0 +1,125 @@
'use strict'
/* eslint-env mocha */
const { expect } = require('aegir/utils/chai')
const mergeOptions = require('merge-options')
const pDefer = require('p-defer')
const delay = require('delay')
const { create } = require('../../src')
const { baseOptions, pubsubSubsystemOptions } = require('./utils')
const peerUtils = require('../utils/creators/peer')
describe('Pubsub subsystem is configurable', () => {
let libp2p
afterEach(async () => {
libp2p && await libp2p.stop()
})
it('should not exist if no module is provided', async () => {
libp2p = await create(baseOptions)
expect(libp2p.pubsub).to.not.exist()
})
it('should exist if the module is provided', async () => {
libp2p = await create(pubsubSubsystemOptions)
expect(libp2p.pubsub).to.exist()
})
it('should start and stop by default once libp2p starts', async () => {
const [peerId] = await peerUtils.createPeerId()
const customOptions = mergeOptions(pubsubSubsystemOptions, {
peerId
})
libp2p = await create(customOptions)
expect(libp2p.pubsub.started).to.equal(false)
await libp2p.start()
expect(libp2p.pubsub.started).to.equal(true)
await libp2p.stop()
expect(libp2p.pubsub.started).to.equal(false)
})
it('should not start if disabled once libp2p starts', async () => {
const [peerId] = await peerUtils.createPeerId()
const customOptions = mergeOptions(pubsubSubsystemOptions, {
peerId,
config: {
pubsub: {
enabled: false
}
}
})
libp2p = await create(customOptions)
expect(libp2p.pubsub.started).to.equal(false)
await libp2p.start()
expect(libp2p.pubsub.started).to.equal(false)
})
it('should allow a manual start', async () => {
const [peerId] = await peerUtils.createPeerId()
const customOptions = mergeOptions(pubsubSubsystemOptions, {
peerId,
config: {
pubsub: {
enabled: false
}
}
})
libp2p = await create(customOptions)
await libp2p.start()
expect(libp2p.pubsub.started).to.equal(false)
await libp2p.pubsub.start()
expect(libp2p.pubsub.started).to.equal(true)
})
})
describe('Pubsub subscription handlers adapter', () => {
let libp2p
beforeEach(async () => {
const [peerId] = await peerUtils.createPeerId()
libp2p = await create(mergeOptions(pubsubSubsystemOptions, {
peerId
}))
await libp2p.start()
})
it('extends pubsub with subscribe handler', async () => {
let countMessages = 0
const topic = 'topic'
const defer = pDefer()
const handler = () => {
countMessages++
if (countMessages > 1) {
throw new Error('only one message should be received')
}
defer.resolve()
}
await libp2p.pubsub.subscribe(topic, handler)
libp2p.pubsub.emit(topic, 'useless-data')
await defer.promise
await libp2p.pubsub.unsubscribe(topic, handler)
libp2p.pubsub.emit(topic, 'useless-data')
// wait to guarantee that the handler is not called twice
await delay(100)
})
})

View File

@@ -0,0 +1,52 @@
'use strict'
const Pubsub = require('libp2p-interfaces/src/pubsub')
const { NOISE: Crypto } = require('libp2p-noise')
const Muxer = require('libp2p-mplex')
const Transport = require('libp2p-websockets')
const filters = require('libp2p-websockets/src/filters')
const transportKey = Transport.prototype[Symbol.toStringTag]
const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')
const relayAddr = MULTIADDRS_WEBSOCKETS[0]
const mergeOptions = require('merge-options')
const baseOptions = {
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [Crypto]
}
}
module.exports.baseOptions = baseOptions
class MockPubsub extends Pubsub {
constructor (libp2p, options = {}) {
super({
debugName: 'mock-pubsub',
multicodecs: '/mock-pubsub',
libp2p,
...options
})
}
}
const pubsubSubsystemOptions = mergeOptions(baseOptions, {
modules: {
pubsub: MockPubsub
},
addresses: {
listen: [`${relayAddr}/p2p-circuit`]
},
config: {
transport: {
[transportKey]: {
filter: filters.all
}
}
}
})
module.exports.pubsubSubsystemOptions = pubsubSubsystemOptions