Skip to content

Commit dc1d30b

Browse files
committed
Added stats test
1 parent dcd8786 commit dc1d30b

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

lib/drives/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,15 @@ class DriveManager extends EventEmitter {
136136
}
137137

138138
async getDriveStats (drive) {
139-
const mounts = await drive.getAllMounts({ memory: true })
139+
const mounts = await new Promise((resolve, reject) => {
140+
drive.getAllMounts({ memory: true }, (err, mounts) => {
141+
if (err) return reject(err)
142+
return resolve(mounts)
143+
})
144+
})
140145
const stats = []
141146

142-
for (const { path, metadata, content} of mounts) {
147+
for (const [path, { metadata, content }] of mounts) {
143148
stats.push({
144149
path,
145150
metadata: getCoreStats(metadata),
@@ -274,8 +279,8 @@ function createDriveHandlers (driveManager) {
274279
var stats = await driveManager.getAllStats()
275280
stats = stats.map(driveStats => toDriveStats(driveStats))
276281

277-
const rsp = new rpc.drive.messages
278-
rsp.setStats(stats)
282+
const rsp = new rpc.drive.messages.StatsResponse()
283+
rsp.setStatsList(stats)
279284

280285
return rsp
281286
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"buffer-json-encoding": "^1.0.2",
3030
"chalk": "^2.4.2",
3131
"collect-stream": "^1.2.1",
32+
"corestore": "^2.0.0",
3233
"corestore-swarm-networking": "^1.0.0",
3334
"dat-encoding": "^5.0.1",
3435
"forever": "^0.15.3",

test/hyperdrive.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ test('can mount a drive within a remote hyperdrive', async t => {
168168
t.end()
169169
})
170170

171-
test.only('can unmount a drive within a remote hyperdrive', async t => {
171+
test('can unmount a drive within a remote hyperdrive', async t => {
172172
const { client, cleanup } = await createOne()
173173

174174
try {
@@ -211,5 +211,4 @@ test.only('can unmount a drive within a remote hyperdrive', async t => {
211211

212212
await cleanup()
213213
t.end()
214-
215214
})

test/replication.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,62 @@ test('can replicate nested mounts between daemons', async t => {
115115
t.end()
116116
})
117117

118+
test.only('can get networking stats for multiple mounts', async t => {
119+
const { clients, cleanup } = await create(2)
120+
const firstClient = clients[0]
121+
const secondClient = clients[1]
122+
123+
try {
124+
const { opts: rootOpts1, id: rootId1 } = await firstClient.drive.get()
125+
const { opts: mountOpts1, id: mountId1 } = await firstClient.drive.get()
126+
const { opts: mountOpts2, id: mountId2 } = await firstClient.drive.get()
127+
await firstClient.drive.publish(mountId2)
128+
129+
await firstClient.drive.mount(rootId1, 'a', { ...mountOpts1, version: null })
130+
await firstClient.drive.mount(rootId1, 'b', { ...mountOpts2, version: null })
131+
132+
await firstClient.drive.writeFile(mountId2, 'hello', 'world')
133+
134+
const firstStats = await firstClient.drive.allStats()
135+
t.same(firstStats.length, 3)
136+
for (const mountStats of firstStats) {
137+
t.same(mountStats.length, 1)
138+
t.same(mountStats[0].metadata.uploadedBytes, 0)
139+
}
140+
141+
const { opts: rootOpts2, id: rootId2 } = await secondClient.drive.get()
142+
const { id: remoteMountId } = await secondClient.drive.get({ key: mountOpts2.key })
143+
144+
await secondClient.drive.mount(rootId2, 'c', { ...mountOpts2, version: null })
145+
146+
// 100 ms delay for replication.
147+
await delay(100)
148+
149+
const replicatedContent = await secondClient.drive.readFile(rootId2, 'c/hello')
150+
t.same(replicatedContent, Buffer.from('world'))
151+
152+
const secondStats = await firstClient.drive.allStats()
153+
t.same(secondStats.length, 3)
154+
155+
var uploadedBytes = null
156+
for (const mountStats of secondStats) {
157+
if (mountStats[0].metadata.key.equals(mountOpts2.key)) {
158+
uploadedBytes = mountStats[0].content.uploadedBytes
159+
t.notEqual(uploadedBytes, 0)
160+
}
161+
}
162+
t.true(uploadedBytes)
163+
164+
const thirdStats = await firstClient.drive.stats(mountId2)
165+
t.same(thirdStats[0].content.uploadedBytes, uploadedBytes)
166+
} catch (err) {
167+
t.fail(err)
168+
}
169+
170+
await cleanup()
171+
t.end()
172+
})
173+
118174
function delay (ms) {
119175
return new Promise(resolve => setTimeout(resolve, ms))
120176
}

0 commit comments

Comments
 (0)