Skip to content

Commit aa10f4d

Browse files
committed
Better cleanup + nested mount test
1 parent 85f4deb commit aa10f4d

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const sub = require('subleveldown')
88
const grpc = require('grpc')
99

1010
const { rpc, loadMetadata } = require('hyperdrive-daemon-client')
11-
const corestore = require('random-access-corestore')
11+
const corestore = require('corestore')
1212
const SwarmNetworker = require('corestore-swarm-networking')
1313

1414
const { DriveManager, createDriveHandlers } = require('./lib/drives')
@@ -96,7 +96,6 @@ module.exports = async function start (opts = {}) {
9696

9797
const daemonOpts = {}
9898
const bootstrapOpts = opts.bootstrap || argv.bootstrap
99-
console.log('BOOTSTRAP OPTS:', opts.bootstrap)
10099
if (bootstrapOpts.length) {
101100
if (bootstrapOpts === false && bootstrapOpts[0] === 'false') {
102101
daemonOpts.network = { bootstrap: false }
@@ -126,16 +125,19 @@ module.exports = async function start (opts = {}) {
126125
server.start()
127126
log.info({ port: port }, 'server listening')
128127

129-
process.once('SIGINT', cleanup)
130-
process.once('SIGTERM', cleanup)
131-
process.once('unhandledRejection', cleanup)
132-
process.once('uncaughtException', cleanup)
128+
const cleanupEvents = ['SIGINT', 'SIGTERM', 'unhandledRejection', 'uncaughtException']
129+
for (const event of cleanupEvents) {
130+
process.once(event, cleanup)
131+
}
133132

134133
return cleanup
135134

136135
async function cleanup () {
137136
await daemon.close()
138137
server.forceShutdown()
138+
for (const event of cleanupEvents) {
139+
process.removeListener(event, cleanup)
140+
}
139141
}
140142

141143
function ensureStorage () {

lib/drives/index.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,10 @@ class DriveManager extends EventEmitter {
150150
key = datEncoding.encode(drive.key)
151151
keyString = this._generateKeyString(key, opts)
152152

153-
154153
if (drive.writable) {
155154
await this._configureDrive(drive, opts && opts.configure)
156155
} else {
157156
// All read-only drives are currently published by default.
158-
console.log('PUBLISHING READ-ONLY DRIVE')
159157
await this.publish(drive)
160158
}
161159
if (this.opts.stats) {
@@ -235,13 +233,11 @@ function createDriveHandlers (driveManager) {
235233
},
236234

237235
publish: async (call) => {
238-
console.error('IN SERVER PUBLISH')
239236
const id = call.request.getId()
240237

241238
if (!id) throw new Error('A publish request must specify a session ID.')
242239
const drive = driveManager.driveForSession(id)
243240

244-
console.log('PUBLISHING DRIVE WITH KEY:', drive.key)
245241
await driveManager.publish(drive)
246242

247243
const rsp = new rpc.drive.messages.PublishDriveResponse()
@@ -353,7 +349,6 @@ function createDriveHandlers (driveManager) {
353349

354350
return new Promise((resolve, reject) => {
355351
drive.mount(path, opts.key, opts, err => {
356-
console.error('after server mount, err:', err)
357352
if (err) return reject(err)
358353
const rsp = new rpc.drive.messages.MountDriveResponse()
359354
return resolve(rsp)

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"buffer-json-encoding": "^1.0.2",
2929
"chalk": "^2.4.2",
3030
"collect-stream": "^1.2.1",
31+
"corestore-swarm-networking": "^1.0.0",
3132
"dat-encoding": "^5.0.1",
3233
"forever": "^0.15.3",
3334
"forever-monitor": "^1.7.1",
@@ -38,8 +39,6 @@
3839
"hyperdrive-daemon-client": "^0.9.4",
3940
"hyperdrive-fuse": "^1.1.0",
4041
"level": "^4.0.0",
41-
"megastore-swarm-networking": "^1.0.0",
42-
"mini-megastore": "^1.0.0",
4342
"mkdirp": "^0.5.1",
4443
"ora": "^3.4.0",
4544
"pino": "^5.12.6",

test/replication.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,40 @@ test('can replicate many mounted drives between daemons', async t => {
8181
}
8282
})
8383

84+
test('can replicate nested mounts between daemons', async t => {
85+
const { clients, cleanup } = await create(2)
86+
const firstClient = clients[0]
87+
const secondClient = clients[1]
88+
89+
try {
90+
const { opts: rootOpts1, id: rootId1 } = await firstClient.drive.get()
91+
const { opts: mountOpts1, id: mountId1 } = await firstClient.drive.get()
92+
const { opts: mountOpts2, id: mountId2 } = await firstClient.drive.get()
93+
await firstClient.drive.publish(mountId2)
94+
95+
await firstClient.drive.mount(rootId1, 'a', { ...mountOpts1, version: null })
96+
await firstClient.drive.mount(mountId1, 'b', { ...mountOpts2, version: null })
97+
98+
await firstClient.drive.writeFile(mountId2, 'hello', 'world')
99+
100+
const { opts: rootOpts2, id: rootId2 } = await secondClient.drive.get()
101+
const { id: remoteMountId } = await secondClient.drive.get({ key: mountOpts2.key })
102+
103+
await secondClient.drive.mount(rootId2, 'c', { ...mountOpts2, version: null })
104+
105+
// 100 ms delay for replication.
106+
await delay(100)
107+
108+
const replicatedContent = await secondClient.drive.readFile(rootId2, 'c/hello')
109+
t.same(replicatedContent, Buffer.from('world'))
110+
} catch (err) {
111+
t.fail(err)
112+
}
113+
114+
await cleanup()
115+
t.end()
116+
})
117+
84118
function delay (ms) {
85119
return new Promise(resolve => setTimeout(resolve, ms))
86120
}

0 commit comments

Comments
 (0)