forked from libp2p/js-libp2p-kad-dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting.spec.js
123 lines (106 loc) · 3.04 KB
/
routing.spec.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
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const PeerId = require('peer-id')
const map = require('async/map')
const each = require('async/each')
const series = require('async/series')
const range = require('lodash.range')
const random = require('lodash.random')
const RoutingTable = require('../src/routing')
const kadUtils = require('../src/utils')
function createPeerId (n, callback) {
map(range(n), (i, cb) => PeerId.create({bits: 512}, cb), callback)
}
describe('Routing Table', () => {
let table
beforeEach(function (done) {
this.timeout(20 * 1000)
PeerId.create({ bits: 512 }, (err, id) => {
expect(err).to.not.exist()
table = new RoutingTable(id, 20)
done()
})
})
it('add', function (done) {
this.timeout(20 * 1000)
createPeerId(20, (err, ids) => {
expect(err).to.not.exist()
series([
(cb) => each(range(1000), (n, cb) => {
table.add(ids[random(ids.length - 1)], cb)
}, cb),
(cb) => each(range(20), (n, cb) => {
const id = ids[random(ids.length - 1)]
kadUtils.convertPeerId(id, (err, key) => {
expect(err).to.not.exist()
expect(table.closestPeers(key, 5).length)
.to.be.above(0)
cb()
})
}, cb)
], done)
})
})
it('remove', function (done) {
this.timeout(20 * 1000)
createPeerId(10, (err, peers) => {
expect(err).to.not.exist()
let k
series([
(cb) => each(peers, (peer, cbEach) => table.add(peer, cbEach), cb),
(cb) => {
const id = peers[2]
kadUtils.convertPeerId(id, (err, key) => {
expect(err).to.not.exist()
k = key
expect(table.closestPeers(key, 10)).to.have.length(10)
cb()
})
},
(cb) => table.remove(peers[5], cb),
(cb) => {
expect(table.closestPeers(k, 10)).to.have.length(9)
expect(table.size).to.be.eql(9)
cb()
}
], done)
})
})
it('closestPeer', function (done) {
this.timeout(10 * 1000)
createPeerId(4, (err, peers) => {
expect(err).to.not.exist()
series([
(cb) => each(peers, (peer, cb) => table.add(peer, cb), cb),
(cb) => {
const id = peers[2]
kadUtils.convertPeerId(id, (err, key) => {
expect(err).to.not.exist()
expect(table.closestPeer(key)).to.eql(id)
cb()
})
}
], done)
})
})
it('closestPeers', function (done) {
this.timeout(20 * 1000)
createPeerId(18, (err, peers) => {
expect(err).to.not.exist()
series([
(cb) => each(peers, (peer, cb) => table.add(peer, cb), cb),
(cb) => {
const id = peers[2]
kadUtils.convertPeerId(id, (err, key) => {
expect(err).to.not.exist()
expect(table.closestPeers(key, 15)).to.have.length(15)
cb()
})
}
], done)
})
})
})