|
| 1 | +/* eslint-env mocha */ |
| 2 | + |
| 3 | +import { KEEP_ALIVE, type Libp2p } from '@libp2p/interface' |
| 4 | +import { expect } from 'aegir/chai' |
| 5 | +import { createLibp2p } from 'libp2p' |
| 6 | +import pWaitFor from 'p-wait-for' |
| 7 | +import { createBaseOptions } from './fixtures/base-options.js' |
| 8 | + |
| 9 | +describe('peers', () => { |
| 10 | + let nodes: Libp2p[] |
| 11 | + |
| 12 | + beforeEach(async () => { |
| 13 | + nodes = await Promise.all([ |
| 14 | + createLibp2p(createBaseOptions()), |
| 15 | + createLibp2p(createBaseOptions()), |
| 16 | + createLibp2p(createBaseOptions()) |
| 17 | + ]) |
| 18 | + }) |
| 19 | + |
| 20 | + afterEach(async () => Promise.all(nodes.map(async n => { await n.stop() }))) |
| 21 | + |
| 22 | + it('should redial a peer tagged with KEEP_ALIVE', async () => { |
| 23 | + await nodes[0].dial(nodes[1].getMultiaddrs()) |
| 24 | + |
| 25 | + expect(nodes[0].getConnections(nodes[1].peerId)).to.not.be.empty() |
| 26 | + |
| 27 | + await nodes[0].peerStore.merge(nodes[1].peerId, { |
| 28 | + tags: { |
| 29 | + [KEEP_ALIVE]: { |
| 30 | + value: 1 |
| 31 | + } |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + await Promise.all( |
| 36 | + nodes[0].getConnections(nodes[1].peerId).map(async conn => conn.close()) |
| 37 | + ) |
| 38 | + |
| 39 | + await pWaitFor(async () => { |
| 40 | + return nodes[0].getConnections(nodes[1].peerId).length > 0 |
| 41 | + }, { |
| 42 | + interval: 100, |
| 43 | + timeout: { |
| 44 | + milliseconds: 5000, |
| 45 | + message: 'Did not reconnect to peer tagged with KEEP_ALIVE' |
| 46 | + } |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should store the multiaddr for a peer after a successful dial', async () => { |
| 51 | + await nodes[0].dial(nodes[1].getMultiaddrs()) |
| 52 | + |
| 53 | + expect(nodes[0].getConnections(nodes[1].peerId)).to.not.be.empty() |
| 54 | + |
| 55 | + const peer = await nodes[0].peerStore.get(nodes[1].peerId) |
| 56 | + expect(peer.addresses).to.not.be.empty() |
| 57 | + expect(peer.metadata.get('last-dial-success')).to.be.ok() |
| 58 | + }) |
| 59 | +}) |
0 commit comments