Skip to content

Commit 574663c

Browse files
committed
chore: fix up tests
1 parent b388009 commit 574663c

File tree

4 files changed

+36
-32
lines changed

4 files changed

+36
-32
lines changed

test/go-gossipsub.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const errcode = require('err-code')
88
const sinon = require('sinon')
99
const pRetry = require('p-retry')
1010
const { EventEmitter } = require('events')
11+
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
12+
const { equals: uint8ArrayEquals } = require('uint8arrays/equals')
1113

1214
const Floodsub = require('libp2p-floodsub')
1315
const Gossipsub = require('../src')
@@ -41,7 +43,7 @@ const checkReceivedMessage = (topic, data, senderIx, msgIx) =>
4143
reject(new Error(`Message never received, sender ${senderIx}, receiver ${receiverIx}, index ${msgIx}`))
4244
}, 20000)
4345
cb = (msg) => {
44-
if (data.equals(msg.data)) {
46+
if (uint8ArrayEquals(data, msg.data)) {
4547
clearTimeout(t)
4648
psub.off(topic, cb)
4749
resolve()
@@ -95,7 +97,8 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
9597

9698
let sendRecv = []
9799
for (let i = 0; i < 100; i++) {
98-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
100+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
101+
99102
const owner = Math.floor(Math.random() * psubs.length)
100103
const results = Promise.all(
101104
psubs
@@ -128,7 +131,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
128131

129132
let sendRecv = []
130133
for (let i = 0; i < 100; i++) {
131-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
134+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
132135
const owner = Math.floor(Math.random() * psubs.length)
133136
const results = Promise.all(
134137
psubs
@@ -164,7 +167,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
164167

165168
let sendRecv = []
166169
for (let i = 0; i < 100; i++) {
167-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
170+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
168171

169172
const owner = 0
170173

@@ -186,7 +189,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
186189

187190
sendRecv = []
188191
for (let i = 0; i < 100; i++) {
189-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
192+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
190193

191194
const owner = 0
192195

@@ -227,7 +230,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
227230
let sendRecv = []
228231
const sendMessages = () => {
229232
for (let i = 0; i < 100; i++) {
230-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
233+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
231234

232235
const owner = 0
233236

@@ -286,7 +289,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
286289

287290
let sendRecv = []
288291
for (let i = 0; i < 5; i++) {
289-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
292+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
290293

291294
const owner = 0
292295

@@ -328,7 +331,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
328331
await Promise.all(psubs.map(ps => awaitEvents(ps, 'gossipsub:heartbeat', 2)))
329332

330333
for (let i = 0; i < 100; i++) {
331-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
334+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
332335
const owner = Math.floor(Math.random() * psubs.length)
333336
const results = Promise.all(
334337
psubs
@@ -372,7 +375,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
372375

373376
let sendRecv = []
374377
for (let i = 0; i < 10; i++) {
375-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
378+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
376379
const owner = 0
377380
const results = Promise.all(
378381
group1.slice(1)
@@ -435,7 +438,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
435438

436439
let sendRecv = []
437440
for (let i = 0; i < 100; i++) {
438-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
441+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
439442
const owner = Math.floor(Math.random() * psubs.length)
440443
const results = Promise.all(
441444
psubs
@@ -473,7 +476,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
473476

474477
let sendRecv = []
475478
for (let i = 0; i < 100; i++) {
476-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
479+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
477480
const owner = Math.floor(Math.random() * psubs.length)
478481
const results = Promise.all(
479482
psubs
@@ -514,7 +517,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
514517

515518
let sendRecv = []
516519
for (let i = 0; i < 100; i++) {
517-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
520+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
518521
const owner = Math.floor(Math.random() * (psubs.length - 5))
519522
const results = Promise.all(
520523
psubs.slice(5)
@@ -549,7 +552,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
549552
await Promise.all(psubs.map(ps => awaitEvents(ps, 'gossipsub:heartbeat', 9)))
550553

551554
for (let i = 0; i < 35; i++) {
552-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
555+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
553556
const owner = Math.floor(Math.random() * psubs.length)
554557
const results = Promise.all(
555558
psubs
@@ -589,7 +592,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
589592

590593
// create a background flood of messages that overloads the queues
591594
const floodOwner = Math.floor(Math.random() * psubs.length)
592-
const floodMsg = Buffer.from('background flooooood')
595+
const floodMsg = uint8ArrayFromString('background flooooood')
593596
const backgroundFlood = new Promise(async resolve => {
594597
for (let i = 0; i < 10000; i++) {
595598
await psubs[floodOwner].publish(floodTopic, floodMsg)
@@ -612,7 +615,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
612615
// and test that we have functional overlays
613616
let sendRecv = []
614617
for (let i = 0; i < 5; i++) {
615-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
618+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
616619
const owner = Math.floor(Math.random() * psubs.length)
617620
const results = Promise.all(
618621
psubs
@@ -657,7 +660,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
657660

658661
let sendRecv = []
659662
for (let i = 0; i < 100; i++) {
660-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
663+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
661664
const owner = Math.floor(Math.random() * psubs.length)
662665
const results = Promise.all(
663666
psubs
@@ -691,7 +694,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
691694
// wait for heartbeats to build mesh
692695
await Promise.all(psubs.map(ps => awaitEvents(ps, 'gossipsub:heartbeat', 2)))
693696

694-
const msg = Buffer.from(`${0} its not a flooooood ${0}`)
697+
const msg = uint8ArrayFromString(`${0} its not a flooooood ${0}`)
695698
const owner = 0
696699
const results = checkReceivedMessage(topic, msg, owner, 0)(psubs[5], 5)
697700
await psubs[owner].publish(topic, msg)
@@ -742,7 +745,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
742745

743746
let sendRecv = []
744747
for (const owner of [9, 3]) {
745-
const msg = Buffer.from(`${owner} its not a flooooood ${owner}`)
748+
const msg = uint8ArrayFromString(`${owner} its not a flooooood ${owner}`)
746749
const results = Promise.all(
747750
psubs
748751
.filter((psub, j) => j !== owner)
@@ -798,7 +801,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
798801
// send a message from each peer and assert it was propagated
799802
let sendRecv = []
800803
for (let i = 0; i < psubs.length; i++) {
801-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
804+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
802805
const owner = i
803806
const results = Promise.all(
804807
psubs
@@ -867,7 +870,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
867870

868871
let sendRecv = []
869872
for (let i = 0; i < 3; i++) {
870-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
873+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
871874
const owner = i
872875
const results = Promise.all(
873876
psubs
@@ -888,7 +891,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
888891

889892
sendRecv = []
890893
for (let i = 0; i < 3; i++) {
891-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
894+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
892895
const owner = i
893896
const results = Promise.all(
894897
psubs
@@ -925,7 +928,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
925928
// send messages from the star and assert they were received
926929
let sendRecv = []
927930
for (let i = 0; i < 20; i++) {
928-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
931+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
929932
const owner = 0
930933
const results = Promise.all(
931934
psubs
@@ -978,7 +981,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
978981

979982
let sendRecv = []
980983
for (let i = 0; i < 20; i++) {
981-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
984+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
982985
const owner = i
983986
sendRecv.push(psubs[owner].publish(topic, msg))
984987
}
@@ -1030,7 +1033,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
10301033

10311034
psubs[0].on(topic, () => expect.fail('node 0 should not receive any messages'))
10321035

1033-
const msg = Buffer.from('its not a flooooood')
1036+
const msg = uint8ArrayFromString('its not a flooooood')
10341037
await psubs[1].publish(topic, msg)
10351038
await psubs[2].publish(topic, msg)
10361039

@@ -1139,7 +1142,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
11391142
psubs.forEach(ps => ps.subscribe(topic))
11401143

11411144
for (let i = 0; i < 300; i++) {
1142-
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
1145+
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
11431146
const owner = i % 10
11441147
await psubs[owner].publish(topic, msg)
11451148
await delay(20)

test/tracer.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { expect } = require('chai')
22
const delay = require('delay')
3-
const { utils } = require('libp2p-interfaces/src/pubsub')
3+
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
44

55
const { IWantTracer } = require('../src/tracer')
66
const constants = require('../src/constants')
@@ -17,7 +17,7 @@ describe('IWantTracer', () => {
1717
const msgIds = []
1818
for (let i = 0; i < 100; i++) {
1919
const m = makeTestMessage(i)
20-
m.from = Buffer.from(peerA)
20+
m.from = uint8ArrayFromString(peerA)
2121
msgIds.push(getMsgId(m))
2222
}
2323

@@ -47,7 +47,7 @@ describe('IWantTracer', () => {
4747
const msgIds = []
4848
for (let i = 0; i < 100; i++) {
4949
const m = makeTestMessage(i)
50-
m.from = Buffer.from(peerA)
50+
m.from = uint8ArrayFromString(peerA)
5151
msgs.push(m)
5252
msgIds.push(getMsgId(m))
5353
}

test/utils/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { expect } = require('chai')
55
const FloodSub = require('libp2p-floodsub')
66
const PeerId = require('peer-id')
77
const delay = require('delay')
8+
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
89

910
exports.first = (map) => map.values().next().value
1011

@@ -46,8 +47,8 @@ for (const [k, v] of Object.entries({
4647
}
4748

4849
exports.getMsgId = (msg) => {
49-
const from = Buffer.from(msg.from)
50-
const seqno = Buffer.from(msg.seqno)
50+
const from = uint8ArrayFromString(msg.from)
51+
const seqno = uint8ArrayFromString(msg.seqno)
5152
const result = new Uint8Array(from.length + seqno.length)
5253
result.set(from, 0)
5354
result.set(seqno, from.length)

test/utils/make-test-message.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const makeTestMessage = (i, topicIDs = []) => {
22
return {
3-
seqno: Buffer.alloc(8, i),
4-
data: Buffer.from([i]),
3+
seqno: Uint8Array.from(new Array(8).fill(i)),
4+
data: Uint8Array.from([i]),
55
from: "test",
66
topicIDs
77
}

0 commit comments

Comments
 (0)