-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathtracer.spec.js
More file actions
68 lines (55 loc) · 1.94 KB
/
tracer.spec.js
File metadata and controls
68 lines (55 loc) · 1.94 KB
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
const { expect } = require('chai')
const delay = require('delay')
const { utils } = require('libp2p-pubsub')
const { IWantTracer } = require('../src/tracer')
const constants = require('../src/constants')
const { makeTestMessage } = require('./utils')
const getMsgId = (msg) => utils.msgId(msg.from, msg.seqno)
describe('IWantTracer', () => {
it('should track broken promises', async function () {
// tests that unfullfilled promises are tracked correctly
this.timeout(6000)
const t = new IWantTracer(getMsgId)
const peerA = 'A'
const peerB = 'B'
const msgIds = []
for (let i = 0; i < 100; i++) {
const m = makeTestMessage(i)
m.from = Buffer.from(peerA)
msgIds.push(getMsgId(m))
}
t.addPromise(peerA, msgIds)
t.addPromise(peerB, msgIds)
// no broken promises yet
let brokenPromises = t.getBrokenPromises()
expect(brokenPromises.size).to.be.equal(0)
// make promises break
await delay(constants.GossipsubIWantFollowupTime + 10)
brokenPromises = t.getBrokenPromises()
expect(brokenPromises.size).to.be.equal(2)
expect(brokenPromises.get(peerA)).to.be.equal(1)
expect(brokenPromises.get(peerB)).to.be.equal(1)
})
it('should track unbroken promises', async function () {
// like above, but this time we deliver messages to fullfil the promises
this.timeout(6000)
const t = new IWantTracer(getMsgId)
const peerA = 'A'
const peerB = 'B'
const msgs = []
const msgIds = []
for (let i = 0; i < 100; i++) {
const m = makeTestMessage(i)
m.from = Buffer.from(peerA)
msgs.push(m)
msgIds.push(getMsgId(m))
}
t.addPromise(peerA, msgIds)
t.addPromise(peerB, msgIds)
msgs.forEach(msg => t.deliverMessage(peerA, msg))
await delay(constants.GossipsubIWantFollowupTime + 10)
// there should be no broken promises
const brokenPromises = t.getBrokenPromises()
expect(brokenPromises.size).to.be.equal(0)
})
})