Skip to content

Commit

Permalink
fix: routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed Jun 7, 2018
1 parent e7df7de commit 6cd92d9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/ipfsd-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,21 @@ class DaemonClient {
/**
* Stop the daemon.
*
* @param {integer} - Grace period to wait before force stopping the node
* @param {integer|undefined} timeout - Grace period to wait before force stopping the node
* @param {function(Error)} [cb]
* @returns {undefined}
*/
stop (timeout, cb) {
if (typeof timeout === 'function') {
cb = timeout
timeout = null
timeout = undefined
}

cb = cb || (() => {})
request
.post(`${this.baseUrl}/stop`)
.query({ id: this._id, timeout })
.query({ id: this._id })
.send({ timeout })
.end((err) => {
if (err) {
return cb(new Error(err.response.body.message))
Expand All @@ -178,20 +179,21 @@ class DaemonClient {
* First `SIGTERM` is sent, after 10.5 seconds `SIGKILL` is sent
* if the process hasn't exited yet.
*
* @param {integer} - Grace period to wait before force stopping the node
* @param {integer|undefined} timeout - Grace period to wait before force stopping the node
* @param {function()} [cb] - Called when the process was killed.
* @returns {undefined}
*/
killProcess (timeout, cb) {
if (typeof timeout === 'function') {
cb = timeout
timeout = null
timeout = undefined
}

cb = cb || (() => {})
request
.post(`${this.baseUrl}/kill`)
.query({ id: this._id, timeout })
.query({ id: this._id })
.send({ timeout })
.end((err) => {
if (err) {
return cb(new Error(err.response.body.message))
Expand Down
4 changes: 2 additions & 2 deletions src/ipfsd-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class Daemon {
/**
* Stop the daemon.
*
* @param {integer} - Grace period to wait before force stopping the node
* @param {integer|undefined} timeout - Grace period to wait before force stopping the node
* @param {function(Error)} callback
* @returns {undefined}
*/
Expand All @@ -314,7 +314,7 @@ class Daemon {
* process.kill(`SIGTERM`) is used. In either case, if the process
* does not exit after 10.5 seconds then a `SIGKILL` is used.
*
* @param {integer} - Grace period to wait before force stopping the node
* @param {integer|undefined} timeout - Grace period to wait before force stopping the node
* @param {function()} callback - Called when the process was killed.
* @returns {undefined}
*/
Expand Down
26 changes: 25 additions & 1 deletion test/endpoint/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,19 @@ describe('routes', () => {
})

describe('POST /stop', () => {
it('should return 200', (done) => {
it('should return 200 without timeout', (done) => {
server.inject({
method: 'POST',
url: `/stop?id=${id}`,
headers: { 'content-type': 'application/json' },
payload: { id }
}, (res) => {
expect(res.statusCode).to.equal(200)
done()
})
})

it('should return 200 with timeout', (done) => {
server.inject({
method: 'POST',
url: `/stop?id=${id}`,
Expand Down Expand Up @@ -250,6 +262,18 @@ describe('routes', () => {
})
})

it('should return 200 with timeout', (done) => {
server.inject({
method: 'POST',
url: `/kill?id=${id}`,
headers: { 'content-type': 'application/json' },
payload: { id, timeout: 1000 }
}, (res) => {
expect(res.statusCode).to.equal(200)
done()
})
})

it('should return 400', (done) => {
server.inject({
method: 'POST',
Expand Down
5 changes: 2 additions & 3 deletions test/start-stop.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,11 @@ tests.forEach((fOpts) => {
})
})


it('.stop with timeout', function (done) {
this.timeout(15000) // should not take longer than timeout
this.timeout(15000 + 10) // should not take longer than timeout
ipfsd.stop(15000, (err) => {
expect(err).to.not.exist()
const running = isrunning(pid)
expect(isrunning(pid)).to.not.be.ok()
done()
})
})
Expand Down

0 comments on commit 6cd92d9

Please sign in to comment.