mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-06-14 16:21:39 +00:00
feat: interface pubsub (#60)
* feat: interface pubsub * chore: pubsub router tests * chore: move pubsub abstractions from gossipsub * chore: address review * chore: revamp docs * chore: add emit self tests to interface * chore: refactor base tests * chore: publish should only accept one topic per api call * chore: normalize msg before emit * chore: do not reset inbound stream * chore: apply suggestions from code review Co-authored-by: Jacob Heun <jacobheun@gmail.com> * chore: address review * fix: remove subscribe handler * chore: remove bits from create peerId Co-authored-by: Jacob Heun <jacobheun@gmail.com> * chore: remove delay from topic validators tests * chore: add event emitter information * fix: topic validator docs Co-authored-by: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
78
test/pubsub/emit-self.spec.js
Normal file
78
test/pubsub/emit-self.spec.js
Normal file
@ -0,0 +1,78 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const { expect } = require('aegir/utils/chai')
|
||||
|
||||
const {
|
||||
createPeerId,
|
||||
mockRegistrar,
|
||||
PubsubImplementation
|
||||
} = require('./utils')
|
||||
|
||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||
|
||||
const protocol = '/pubsub/1.0.0'
|
||||
const topic = 'foo'
|
||||
const data = uint8ArrayFromString('bar')
|
||||
const shouldNotHappen = (_) => expect.fail()
|
||||
|
||||
describe('emitSelf', () => {
|
||||
let pubsub
|
||||
|
||||
describe('enabled', () => {
|
||||
before(async () => {
|
||||
const peerId = await createPeerId()
|
||||
|
||||
pubsub = new PubsubImplementation(protocol, {
|
||||
peerId,
|
||||
registrar: mockRegistrar
|
||||
}, { emitSelf: true })
|
||||
})
|
||||
|
||||
before(() => {
|
||||
pubsub.start()
|
||||
pubsub.subscribe(topic)
|
||||
})
|
||||
|
||||
after(() => {
|
||||
pubsub.stop()
|
||||
})
|
||||
|
||||
it('should emit to self on publish', () => {
|
||||
const promise = new Promise((resolve) => pubsub.once(topic, resolve))
|
||||
|
||||
pubsub.publish(topic, data)
|
||||
|
||||
return promise
|
||||
})
|
||||
})
|
||||
|
||||
describe('disabled', () => {
|
||||
before(async () => {
|
||||
const peerId = await createPeerId()
|
||||
|
||||
pubsub = new PubsubImplementation(protocol, {
|
||||
peerId,
|
||||
registrar: mockRegistrar
|
||||
}, { emitSelf: false })
|
||||
})
|
||||
|
||||
before(() => {
|
||||
pubsub.start()
|
||||
pubsub.subscribe(topic)
|
||||
})
|
||||
|
||||
after(() => {
|
||||
pubsub.stop()
|
||||
})
|
||||
|
||||
it('should not emit to self on publish', () => {
|
||||
pubsub.once(topic, (m) => shouldNotHappen)
|
||||
|
||||
pubsub.publish(topic, data)
|
||||
|
||||
// Wait 1 second to guarantee that self is not noticed
|
||||
return new Promise((resolve) => setTimeout(() => resolve(), 1000))
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user