Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wait for peer stats to be updated during test #1238

Merged
merged 2 commits into from
Jun 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions test/metrics/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createBaseOptions } from '../utils/base-options.js'
import type { Libp2pNode } from '../../src/libp2p.js'
import type { Libp2pOptions } from '../../src/index.js'
import type { DefaultMetrics } from '../../src/metrics/index.js'
import pWaitFor from 'p-wait-for'
import drain from 'it-drain'

describe('libp2p.metrics', () => {
Expand Down Expand Up @@ -170,6 +171,19 @@ describe('libp2p.metrics', () => {
throw new Error('Metrics not configured')
}

await pWaitFor(() => {
const peerStats = metrics.forPeer(connection.remotePeer)?.getSnapshot()
const transferred = parseInt(peerStats?.dataReceived.toString() ?? '0')

if (transferred < bytes.length) {
return false
}

return true
}, {
interval: 100
})

const peerStats = metrics.forPeer(connection.remotePeer)?.getSnapshot()
expect(parseInt(peerStats?.dataReceived.toString() ?? '0')).to.be.at.least(bytes.length)

Expand Down