Skip to content
This repository has been archived by the owner on Dec 3, 2018. It is now read-only.

Commit

Permalink
change isSiadRunning to async isRunning
Browse files Browse the repository at this point in the history
  • Loading branch information
avahowell committed Jul 19, 2016
1 parent 14bda20 commit 43fc8e9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["es2015", "react"]
"presets": ["es2015", "stage-3"],
"plugins": ["transform-runtime"]
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"main": "lib/sia.js",
"scripts": {
"build": "babel --presets es2015,stage-3 -d lib/ src/",
"build": "babel -d lib/ src/",
"test": "npm run lint && mocha --compilers js:babel-register --recursive ./test",
"prepublish": "npm run build",
"lint": "eslint --max-warnings 0 ./src ./test",
Expand All @@ -32,6 +32,8 @@
"devDependencies": {
"babel-cli": "^6.10.1",
"babel-eslint": "^6.1.2",
"babel-plugin-transform-runtime": "^6.9.0",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-3": "^6.11.0",
Expand Down
26 changes: 17 additions & 9 deletions src/sia.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const hastingsPerSiacoin = new BigNumber('10').toPower(24)
const siacoinsToHastings = (siacoins) => new BigNumber(siacoins).times(hastingsPerSiacoin)
const hastingsToSiacoins = (hastings) => new BigNumber(hastings).dividedBy(hastingsPerSiacoin)

// Call makes a call to the Sia API at `address`, with the request options defined by `opts`.
// returns a promise which resolves with the response if the request completes successfully
// and rejects with the error if the request fails.
const call = (address, opts) => new Promise((resolve, reject) => {
let callOptions = opts
if (typeof opts === 'string') {
Expand All @@ -30,8 +33,10 @@ const call = (address, opts) => new Promise((resolve, reject) => {
}
})
})

// launch launches a new instance of siad using `settings`.
// this function can `throw`, callers should catch errors from `spawn`.
// this function can `throw`, callers should catch errors.
// callers should also handle the lifecycle of the spawned process.
const launch = (settings) => {
const opts = { }
if (process.geteuid) {
Expand All @@ -40,17 +45,20 @@ const launch = (settings) => {
return spawn(settings.path, [ '--sia-directory=' + settings.datadir ], opts)
}

// isSiadRunning returns a promise which resolves with true if siad is running at address,
// or false if siad is not running.
const isSiadRunning = (address) => new Promise((resolve) => {
call(address, '/daemon/version')
.catch(() => resolve(false))
.then(() => resolve(true))
})
// isRunning returns true if a successful call can be to /daemon/version
// using the address provided in `address`.
async function isRunning(address) {
try {
await call(address, '/daemon/version')
return true
} catch (e) {
return false
}
}

export {
launch,
isSiadRunning,
isRunning,
call,
siacoinsToHastings,
hastingsToSiacoins,
Expand Down
28 changes: 8 additions & 20 deletions test/sia.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-unused-expressions */
import 'babel-polyfill'
import BigNumber from 'bignumber.js'
import { siacoinsToHastings, hastingsToSiacoins, isSiadRunning } from '../src/sia.js'
import { siacoinsToHastings, hastingsToSiacoins, isRunning } from '../src/sia.js'
import { expect } from 'chai'
import proxyquire from 'proxyquire'
import { spy } from 'sinon'
Expand Down Expand Up @@ -49,32 +49,20 @@ describe('sia.js wrapper library', () => {
})
})
describe('siad interaction functions', () => {
describe('isSiadRunning', () => {
it('resolves true when siad is running', (done) => {
describe('isRunning', () => {
it('returns true when siad is running', async function() {
nock('http://localhost:9980')
.get('/daemon/version')
.reply(200, 'test-version')

isSiadRunning('localhost:9980').then((running) => {
if (running) {
done()
} else {
throw new Error('isSiadRunning resolved false when siad was running')
}
})
const running = await isRunning('localhost:9980')
expect(running).to.be.true
})
it('resolves false when siad is not running', (done) => {
it('returns false when siad is not running', async function() {
nock('http://localhost:9980')
.get('/daemon/version')
.replyWithError('error')

isSiadRunning('localhost:9980').then((running) => {
if (!running) {
done()
} else {
throw new Error('isSiadRunning resolved true when siad was not running')
}
})
const running = await isRunning('localhost:9980')
expect(running).to.be.false
})
})
describe('launch', () => {
Expand Down

0 comments on commit 43fc8e9

Please sign in to comment.