1
0
mirror of https://github.com/fluencelabs/js-libp2p synced 2025-03-31 23:01:04 +00:00
Alex Potsides 2836acc90f
fix: use keep-alive tag to reconnect to peers on startup ()
Instead of trying to connect to every peer in the peer store when
we start a node, only connect to the peers that have been marked
with a `keep-alive` tag.
2022-06-28 08:05:16 +01:00

71 lines
1.5 KiB
TypeScript

/* eslint-env mocha */
import { expect } from 'aegir/chai'
import sinon from 'sinon'
import { createBaseOptions } from '../utils/base-options.browser.js'
import { createPeerId } from '../utils/creators/peer.js'
import type { PeerId } from '@libp2p/interface-peer-id'
import { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js'
import type { Startable } from '@libp2p/interfaces/startable'
describe('peer discovery', () => {
describe('basic functions', () => {
let peerId: PeerId
let libp2p: Libp2pNode
before(async () => {
peerId = await createPeerId()
})
afterEach(async () => {
if (libp2p != null) {
await libp2p.stop()
}
sinon.reset()
})
it('should stop discovery on libp2p start/stop', async () => {
let started = 0
let stopped = 0
class MockDiscovery implements Startable {
static tag = 'mock'
started = false
isStarted () {
return this.started
}
start () {
this.started = true
started++
}
stop () {
this.started = false
stopped++
}
addEventListener () {}
removeEventListener () {}
}
libp2p = await createLibp2pNode(createBaseOptions({
peerId,
peerDiscovery: [
new MockDiscovery()
]
}))
await libp2p.start()
expect(started).to.equal(1)
expect(stopped).to.equal(0)
await libp2p.stop()
expect(started).to.equal(1)
expect(stopped).to.equal(1)
})
})
})