This repository was archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathpeer-distance-list.spec.js
116 lines (90 loc) · 3.07 KB
/
peer-distance-list.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
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const PeerId = require('peer-id')
const { Buffer } = require('buffer')
const kadUtils = require('../src/utils')
const PeerDistanceList = require('../src/peer-list/peer-distance-list')
describe('PeerDistanceList', () => {
const p1 = new PeerId(Buffer.from('11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31'))
const p2 = new PeerId(Buffer.from('11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32'))
const p3 = new PeerId(Buffer.from('11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'))
const p4 = new PeerId(Buffer.from('11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34'))
const p5 = new PeerId(Buffer.from('11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31'))
const p6 = new PeerId(Buffer.from('11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a35'))
const p7 = new PeerId(Buffer.from('11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32'))
let key
before(async () => {
key = await kadUtils.convertPeerId(p1)
})
describe('basics', () => {
it('add', async () => {
const pdl = new PeerDistanceList(key)
await pdl.add(p3)
await pdl.add(p1)
await pdl.add(p2)
await pdl.add(p4)
await pdl.add(p5)
await pdl.add(p1)
// Note: p1 and p5 are equal
expect(pdl.length).to.eql(4)
expect(pdl.peers).to.be.eql([p1, p4, p3, p2])
})
it('capacity', async () => {
const pdl = new PeerDistanceList(key, 3)
await pdl.add(p1)
await pdl.add(p2)
await pdl.add(p3)
await pdl.add(p4)
await pdl.add(p5)
await pdl.add(p6)
// Note: p1 and p5 are equal
expect(pdl.length).to.eql(3)
// Closer peers added later should replace further
// peers added earlier
expect(pdl.peers).to.be.eql([p1, p6, p4])
})
})
describe('closer', () => {
let pdl
before(async () => {
pdl = new PeerDistanceList(key)
await pdl.add(p1)
await pdl.add(p2)
await pdl.add(p3)
await pdl.add(p4)
})
it('single closer peer', async () => {
const closer = await pdl.anyCloser([p6])
expect(closer).to.be.eql(true)
})
it('single further peer', async () => {
const closer = await pdl.anyCloser([p7])
expect(closer).to.be.eql(false)
})
it('closer and further peer', async () => {
const closer = await pdl.anyCloser([p6, p7])
expect(closer).to.be.eql(true)
})
it('single peer equal to furthest in list', async () => {
const closer = await pdl.anyCloser([p2])
expect(closer).to.be.eql(false)
})
it('no peers', async () => {
const closer = await pdl.anyCloser([])
expect(closer).to.be.eql(false)
})
it('empty peer distance list', async () => {
const pdl = new PeerDistanceList(key)
const closer = await pdl.anyCloser([p1])
expect(closer).to.be.eql(true)
})
it('empty peer distance list and no peers', async () => {
const pdl = new PeerDistanceList(key)
const closer = await pdl.anyCloser([])
expect(closer).to.be.eql(false)
})
})
})