-
Notifications
You must be signed in to change notification settings - Fork 63
feat: dont call spawn cb more than once on error #243
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
Changes from all commits
46ed8fa
1f0b736
e0fc124
fd13f5a
825f74a
42597c1
b7ef098
21b9850
0ed2e96
ffa36a2
cf6f865
408b96f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
const multiaddr = require('multiaddr') | ||
const defaultsDeep = require('lodash.defaultsdeep') | ||
const createRepo = require('./utils/repo/create-nodejs') | ||
const repoUtils = require('./utils/repo/nodejs') | ||
const defaults = require('lodash.defaults') | ||
const waterfall = require('async/waterfall') | ||
const debug = require('debug') | ||
|
@@ -30,14 +30,14 @@ class Node extends EventEmitter { | |
IPFS = this.opts.exec | ||
|
||
this.opts.args = this.opts.args || [] | ||
this.path = this.opts.repoPath | ||
this.repo = createRepo(this.path) | ||
this.path = this.opts.repoPath || repoUtils.createTempRepoPath() | ||
this.disposable = this.opts.disposable | ||
this.clean = true | ||
this._apiAddr = null | ||
this._gatewayAddr = null | ||
this._started = false | ||
this.api = null | ||
this.initialized = false | ||
this.bits = this.opts.initOptions ? this.opts.initOptions.bits : null | ||
|
||
this.opts.EXPERIMENTAL = defaultsDeep({}, opts.EXPERIMENTAL, { | ||
|
@@ -64,14 +64,16 @@ class Node extends EventEmitter { | |
}) | ||
|
||
this.exec = new IPFS({ | ||
repo: this.repo, | ||
repo: this.path, | ||
init: false, | ||
start: false, | ||
pass: this.opts.pass, | ||
EXPERIMENTAL: this.opts.EXPERIMENTAL, | ||
libp2p: this.opts.libp2p | ||
}) | ||
|
||
// TODO: should this be wrapped in a process.nextTick(), for context: | ||
// https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#why-use-process-nexttick | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not do it now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm asking for feedback there mostly - did you take a look at the link? |
||
this.exec.once('error', err => this.emit('error', err)) | ||
this.exec.once('ready', () => this.emit('ready')) | ||
} | ||
|
@@ -176,7 +178,8 @@ class Node extends EventEmitter { | |
return callback() | ||
} | ||
|
||
this.repo.teardown(callback) | ||
repoUtils.removeRepo(this.path) | ||
callback() | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* global self */ | ||
'use strict' | ||
|
||
const hat = require('hat') | ||
const Dexie = require('dexie') | ||
|
||
exports.createTempRepoPath = function createTempPathRepo () { | ||
return '/ipfs-' + hat() | ||
} | ||
|
||
exports.removeRepo = function removeRepo (repoPath) { | ||
Dexie.delete(repoPath) | ||
} | ||
|
||
exports.repoExists = function repoExists (repoPath, cb) { | ||
const db = new Dexie(repoPath) | ||
db.open(repoPath) | ||
.then((store) => { | ||
const table = store.table(repoPath) | ||
return table | ||
.count((cnt) => cb(null, cnt > 0)) | ||
.catch(cb) | ||
}).catch(cb) | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict' | ||
|
||
const os = require('os') | ||
const path = require('path') | ||
const hat = require('hat') | ||
const rimraf = require('rimraf') | ||
const fs = require('fs') | ||
|
||
exports.removeRepo = function removeRepo (dir) { | ||
try { | ||
fs.accessSync(dir) | ||
} catch (err) { | ||
// Does not exist so all good | ||
return | ||
} | ||
|
||
return rimraf.sync(dir) | ||
} | ||
|
||
exports.createTempRepoPath = function createTempRepo () { | ||
return path.join(os.tmpdir(), '/ipfs-test-' + hat()) | ||
} | ||
|
||
exports.repoExists = function (repoPath, cb) { | ||
fs.access(`${repoPath}/config`, (err) => { | ||
if (err) { return cb(null, false) } | ||
cb(null, true) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,13 @@ const chai = require('chai') | |
const dirtyChai = require('dirty-chai') | ||
const expect = chai.expect | ||
chai.use(dirtyChai) | ||
|
||
const fs = require('fs') | ||
const isNode = require('detect-node') | ||
const hat = require('hat') | ||
const IPFSFactory = require('../src') | ||
const JSIPFS = require('ipfs') | ||
const once = require('once') | ||
|
||
const tests = [ | ||
{ type: 'go', bits: 1024 }, | ||
|
@@ -160,7 +162,6 @@ describe('Spawn options', function () { | |
}) | ||
}) | ||
|
||
// TODO ?? What is this test trying to prove? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why delete? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It tries to spawn a node an then manually attach an ipfs-api to it - we do that in some places in ipfs-api tests and others... |
||
describe('spawn a node and attach api', () => { | ||
let ipfsd | ||
|
||
|
@@ -440,5 +441,35 @@ describe('Spawn options', function () { | |
}) | ||
}) | ||
}) | ||
|
||
describe(`don't callback twice on error`, () => { | ||
if (fOpts.type !== 'proc') { return } | ||
it('spawn with error', (done) => { | ||
this.timeout(20 * 1000) | ||
// `once.strict` should throw if its called more than once | ||
const callback = once.strict((err, ipfsd) => { | ||
if (err) { return done(err) } | ||
|
||
ipfsd.once('error', () => {}) // avoid EventEmitter throws | ||
|
||
// Do an operation, just to make sure we're working | ||
ipfsd.api.id((err) => { | ||
if (err) { | ||
return done(err) | ||
} | ||
|
||
// Do something to make stopping fail | ||
ipfsd.exec._repo.close((err) => { | ||
if (err) { return done(err) } | ||
ipfsd.stop((err) => { | ||
expect(err).to.exist() | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
f.spawn(callback) | ||
}) | ||
}) | ||
})) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update ipfs-repo to 0.20.0 and update all deps below 1.0.0 to be ~ rather then ^