Skip to content

Commit

Permalink
chore: fix up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Dec 2, 2021
1 parent b388009 commit 574663c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
53 changes: 28 additions & 25 deletions test/go-gossipsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const errcode = require('err-code')
const sinon = require('sinon')
const pRetry = require('p-retry')
const { EventEmitter } = require('events')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const { equals: uint8ArrayEquals } = require('uint8arrays/equals')

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

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

const owner = Math.floor(Math.random() * psubs.length)
const results = Promise.all(
psubs
Expand Down Expand Up @@ -128,7 +131,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

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

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

const owner = 0

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

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

const owner = 0

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

const owner = 0

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

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

const owner = 0

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

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

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

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

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

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

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

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

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

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

let sendRecv = []
for (const owner of [9, 3]) {
const msg = Buffer.from(`${owner} its not a flooooood ${owner}`)
const msg = uint8ArrayFromString(`${owner} its not a flooooood ${owner}`)
const results = Promise.all(
psubs
.filter((psub, j) => j !== owner)
Expand Down Expand Up @@ -798,7 +801,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
// send a message from each peer and assert it was propagated
let sendRecv = []
for (let i = 0; i < psubs.length; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = i
const results = Promise.all(
psubs
Expand Down Expand Up @@ -867,7 +870,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

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

sendRecv = []
for (let i = 0; i < 3; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = i
const results = Promise.all(
psubs
Expand Down Expand Up @@ -925,7 +928,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {
// send messages from the star and assert they were received
let sendRecv = []
for (let i = 0; i < 20; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = 0
const results = Promise.all(
psubs
Expand Down Expand Up @@ -978,7 +981,7 @@ describe("go-libp2p-pubsub gossipsub tests", function () {

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

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

const msg = Buffer.from('its not a flooooood')
const msg = uint8ArrayFromString('its not a flooooood')
await psubs[1].publish(topic, msg)
await psubs[2].publish(topic, msg)

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

for (let i = 0; i < 300; i++) {
const msg = Buffer.from(`${i} its not a flooooood ${i}`)
const msg = uint8ArrayFromString(`${i} its not a flooooood ${i}`)
const owner = i % 10
await psubs[owner].publish(topic, msg)
await delay(20)
Expand Down
6 changes: 3 additions & 3 deletions test/tracer.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { expect } = require('chai')
const delay = require('delay')
const { utils } = require('libp2p-interfaces/src/pubsub')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')

const { IWantTracer } = require('../src/tracer')
const constants = require('../src/constants')
Expand All @@ -17,7 +17,7 @@ describe('IWantTracer', () => {
const msgIds = []
for (let i = 0; i < 100; i++) {
const m = makeTestMessage(i)
m.from = Buffer.from(peerA)
m.from = uint8ArrayFromString(peerA)
msgIds.push(getMsgId(m))
}

Expand Down Expand Up @@ -47,7 +47,7 @@ describe('IWantTracer', () => {
const msgIds = []
for (let i = 0; i < 100; i++) {
const m = makeTestMessage(i)
m.from = Buffer.from(peerA)
m.from = uint8ArrayFromString(peerA)
msgs.push(m)
msgIds.push(getMsgId(m))
}
Expand Down
5 changes: 3 additions & 2 deletions test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { expect } = require('chai')
const FloodSub = require('libp2p-floodsub')
const PeerId = require('peer-id')
const delay = require('delay')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')

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

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

exports.getMsgId = (msg) => {
const from = Buffer.from(msg.from)
const seqno = Buffer.from(msg.seqno)
const from = uint8ArrayFromString(msg.from)
const seqno = uint8ArrayFromString(msg.seqno)
const result = new Uint8Array(from.length + seqno.length)
result.set(from, 0)
result.set(seqno, from.length)
Expand Down
4 changes: 2 additions & 2 deletions test/utils/make-test-message.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const makeTestMessage = (i, topicIDs = []) => {
return {
seqno: Buffer.alloc(8, i),
data: Buffer.from([i]),
seqno: Uint8Array.from(new Array(8).fill(i)),
data: Uint8Array.from([i]),
from: "test",
topicIDs
}
Expand Down

0 comments on commit 574663c

Please sign in to comment.