This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathpeers.js
132 lines (106 loc) · 4 KB
/
peers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint-env mocha */
import { waitForPeers, getTopic } from './utils.js'
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import delay from 'delay'
import { isWebWorker } from 'ipfs-utils/src/env.js'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testPeers (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.pubsub.peers', function () {
this.timeout(80 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let ipfs1
/** @type {import('ipfs-core-types').IPFS} */
let ipfs2
/** @type {import('ipfs-core-types').IPFS} */
let ipfs3
/** @type {string[]} */
let subscribedTopics = []
/** @type {import('ipfs-core-types/src/root').IDResult} */
let ipfs2Id
/** @type {import('ipfs-core-types/src/root').IDResult} */
let ipfs3Id
before(async () => {
ipfs1 = (await factory.spawn()).api
// webworkers are not dialable because webrtc is not available
ipfs2 = (await factory.spawn({ type: isWebWorker ? 'js' : undefined })).api
ipfs3 = (await factory.spawn({ type: isWebWorker ? 'js' : undefined })).api
ipfs2Id = await ipfs2.id()
ipfs3Id = await ipfs3.id()
const ipfs2Addr = ipfs2Id.addresses
.find(ma => ma.nodeAddress().address === '127.0.0.1')
const ipfs3Addr = ipfs3Id.addresses
.find(ma => ma.nodeAddress().address === '127.0.0.1')
if (!ipfs2Addr || !ipfs3Addr) {
throw new Error('Could not find addrs')
}
await ipfs1.swarm.connect(ipfs2Addr)
await ipfs1.swarm.connect(ipfs3Addr)
await ipfs2.swarm.connect(ipfs3Addr)
})
afterEach(async () => {
const nodes = [ipfs1, ipfs2, ipfs3]
for (let i = 0; i < subscribedTopics.length; i++) {
const topic = subscribedTopics[i]
await Promise.all(nodes.map(ipfs => ipfs.pubsub.unsubscribe(topic)))
}
subscribedTopics = []
await delay(100)
})
after(() => factory.clean())
it('should not error when not subscribed to a topic', async () => {
const topic = getTopic()
const peers = await ipfs1.pubsub.peers(topic)
expect(peers).to.exist()
// Should be empty() but as mentioned below go-ipfs returns more than it should
// expect(peers).to.be.empty()
})
it('should not return extra peers', async () => {
// Currently go-ipfs returns peers that have not been
// subscribed to the topic. Enable when go-ipfs has been fixed
const sub1 = () => {}
const sub2 = () => {}
const sub3 = () => {}
const topic = getTopic()
const topicOther = topic + 'different topic'
subscribedTopics = [topic, topicOther]
await ipfs1.pubsub.subscribe(topic, sub1)
await ipfs2.pubsub.subscribe(topicOther, sub2)
await ipfs3.pubsub.subscribe(topicOther, sub3)
const peers = await ipfs1.pubsub.peers(topic)
expect(peers).to.be.empty()
})
it('should return peers for a topic - one peer', async () => {
// Currently go-ipfs returns peers that have not been
// subscribed to the topic. Enable when go-ipfs has been fixed
const sub1 = () => {}
const sub2 = () => {}
const sub3 = () => {}
const topic = getTopic()
subscribedTopics = [topic]
await ipfs1.pubsub.subscribe(topic, sub1)
await ipfs2.pubsub.subscribe(topic, sub2)
await ipfs3.pubsub.subscribe(topic, sub3)
await waitForPeers(ipfs1, topic, [ipfs2Id.id], 30000)
})
it('should return peers for a topic - multiple peers', async () => {
const sub1 = () => {}
const sub2 = () => {}
const sub3 = () => {}
const topic = getTopic()
subscribedTopics = [topic]
await ipfs1.pubsub.subscribe(topic, sub1)
await ipfs2.pubsub.subscribe(topic, sub2)
await ipfs3.pubsub.subscribe(topic, sub3)
await waitForPeers(ipfs1, topic, [ipfs2Id.id, ipfs3Id.id], 30000)
})
})
}