Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 17 additions & 2 deletions lib/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,28 @@ module.exports = class Monitor extends ReadyResource {
this.blobs.core.on('peer-remove', this._boundPeerUpdate)
this.blobs.core.on('upload', this._boundOnUpload)
this.blobs.core.on('download', this._boundOnDownload)

if (!this.entry) {
this.drive.db.core.on('peer-add', this._boundPeerUpdate)
this.drive.db.core.on('peer-remove', this._boundPeerUpdate)
this.drive.db.core.on('upload', this._boundOnUpload)
this.drive.db.core.on('download', this._boundOnDownload)
}
}

async _close() {
this.blobs.core.off('peer-add', this._boundPeerUpdate)
this.blobs.core.off('peer-remove', this._boundPeerUpdate)
this.blobs.core.off('upload', this._boundOnUpload)
this.blobs.core.off('download', this._boundOnDownload)

if (!this.entry) {
this.drive.db.core.off('peer-add', this._boundPeerUpdate)
this.drive.db.core.off('peer-remove', this._boundPeerUpdate)
this.drive.db.core.off('upload', this._boundOnUpload)
this.drive.db.core.off('download', this._boundOnDownload)
}

this.drive.monitors.delete(this)
}

Expand Down Expand Up @@ -88,8 +103,8 @@ module.exports = class Monitor extends ReadyResource {
}

_updateStats(speed, stats, index, bytes, from) {
if (!this.entry || this.closing) return
if (!isWithinRange(index, this.entry)) return
if (this.closing) return
if (this.entry && !isWithinRange(index, this.entry)) return

if (!stats.startTime) stats.startTime = Date.now()

Expand Down
28 changes: 28 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,34 @@ test('monitor is removed from the Set on close', async (t) => {
t.is(drive.monitors.size, 0)
})

test('monitor range download', async (t) => {
const { corestore, drive, swarm, mirror } = await testenv(t)
swarm.on('connection', (conn) => corestore.replicate(conn))
swarm.join(drive.discoveryKey, { server: true, client: false })
await swarm.flush()

mirror.swarm.on('connection', (conn) => mirror.corestore.replicate(conn))
mirror.swarm.join(drive.discoveryKey, { server: false, client: true })
await mirror.swarm.flush()

await drive.put('/file-a', Buffer.alloc(1024))
await drive.put('/file-b', Buffer.alloc(1024))
await drive.put('/file-c', Buffer.alloc(1024))

await eventFlush()

const monitor = mirror.drive.monitor('download-monitor')
await monitor.ready()

const download = await mirror.drive.downloadRange([], [{ start: 0, end: 3 }])
await download.done()

t.is(monitor.downloadStats.peers, 1)
t.ok(monitor.downloadStats.speed > 0)
t.ok(monitor.downloadStats.blocks > 0)
t.ok(monitor.downloadStats.totalBytes, 3072)
})

async function testenv(t) {
const { teardown } = t

Expand Down