Skip to content

Commit

Permalink
chore: add options for start daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Mar 25, 2019
1 parent c43a24f commit c14b887
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 21 deletions.
24 changes: 17 additions & 7 deletions src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@ class Daemon {
/**
* @async
* Starts a daemon and a client associated with it.
* @param {Object} options daemon options
* @param {bool} options.dht dht enabled (false)
* @returns {void}
*/
async start () {
async start (options = {}) {
if (this._client) {
throw new Error('Daemon has already started')
}

// start daemon
await this._startDaemon()
await this._startDaemon(options)

// start client
this._client = new Client(this._addr)
Expand All @@ -89,19 +91,27 @@ class Daemon {
/**
* Starts the specifiec daemon and wait for its start.
* @private
* @param {Object} options daemon options
* @param {bool} options.dht dht enabled (false)
* @returns {Promise}
*/
_startDaemon () {
_startDaemon (options) {
return new Promise((resolve, reject) => {
let options
let execOptions

console.log('options', options)

if (this._type === 'go') {
options = ['-listen', this._addr, '-dht']
execOptions = ['-listen', this._addr]

options.dht && execOptions.push('-dht')
} else {
options = ['--listen', this._addr, '--dht']
execOptions = ['--listen', this._addr]

options.dht && execOptions.push('--dht')
}

const daemon = execa(this._binPath, options)
const daemon = execa(this._binPath, execOptions)

daemon.stdout.once('data', () => {
return resolve()
Expand Down
2 changes: 1 addition & 1 deletion test/dht/content-fetching/go2go.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe.skip('dht.contentFetching', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(2, 'go')
daemons = await spawnDaemons(2, 'go', { dht: true })

// connect them
const identify0 = await daemons[0].client.identify()
Expand Down
2 changes: 1 addition & 1 deletion test/dht/content-fetching/go2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe.skip('dht.contentFetching', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(2, ['go', 'js'])
daemons = await spawnDaemons(2, ['go', 'js'], { dht: true })

// connect them
const identify0 = await daemons[0].client.identify()
Expand Down
2 changes: 1 addition & 1 deletion test/dht/content-fetching/js2go.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe.skip('dht.contentFetching', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(2, ['js', 'go'])
daemons = await spawnDaemons(2, ['js', 'go'], { dht: true })

// connect them
const identify0 = await daemons[0].client.identify()
Expand Down
2 changes: 1 addition & 1 deletion test/dht/content-fetching/js2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('dht.contentFetching', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(2, 'js')
daemons = await spawnDaemons(2, 'js', { dht: true })

// connect them
const identify0 = await daemons[0].client.identify()
Expand Down
2 changes: 1 addition & 1 deletion test/dht/content-routing/go2go.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('dht.contentRouting', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(2, 'go')
daemons = await spawnDaemons(2, 'go', { dht: true })

// connect them
identify0 = await daemons[0].client.identify()
Expand Down
2 changes: 1 addition & 1 deletion test/dht/content-routing/go2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('dht.contentRouting', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(2, ['go', 'js'])
daemons = await spawnDaemons(2, ['go', 'js'], { dht: true })

// connect them
identify0 = await daemons[0].client.identify()
Expand Down
2 changes: 1 addition & 1 deletion test/dht/content-routing/js2go.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('dht.contentRouting', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(2, ['js', 'go'])
daemons = await spawnDaemons(2, ['js', 'go'], { dht: true })

// connect them
identify0 = await daemons[0].client.identify()
Expand Down
2 changes: 1 addition & 1 deletion test/dht/content-routing/js2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('dht.contentRouting', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(2, 'js')
daemons = await spawnDaemons(2, 'js', { dht: true })

// connect them
identify0 = await daemons[0].client.identify()
Expand Down
2 changes: 1 addition & 1 deletion test/dht/peer-routing/go2go.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('dht.peerRouting', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(3, 'go')
daemons = await spawnDaemons(3, 'go', { dht: true })
})

// Stop daemons
Expand Down
2 changes: 1 addition & 1 deletion test/dht/peer-routing/go2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('dht.peerRouting', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(3, ['go', 'go', 'js'])
daemons = await spawnDaemons(3, ['go', 'go', 'js'], { dht: true })
})

// Stop daemons
Expand Down
2 changes: 1 addition & 1 deletion test/dht/peer-routing/js2go.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('dht.peerRouting', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(3, ['js', 'js', 'go'])
daemons = await spawnDaemons(3, ['js', 'js', 'go'], { dht: true })
})

// Stop daemons
Expand Down
2 changes: 1 addition & 1 deletion test/dht/peer-routing/js2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('dht.peerRouting', () => {
before(async function () {
this.timeout(20 * 1000)

daemons = await spawnDaemons(3, 'js')
daemons = await spawnDaemons(3, 'js', { dht: true })
})

// Stop daemons
Expand Down
11 changes: 9 additions & 2 deletions test/utils/spawnDaemons.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const startPortNumber = 9000
/**
* @param {number} n number of nodes to spawn
* @param {string|array} type nodes type (default: js)
* @param {Object|array} options daemon options
*/
async function spawnDaemons (n, type = 'js') {
async function spawnDaemons (n, type = 'js', options) {
assert(n, 'spawnDaemons require a number of nodes to start')
assert(validType(n, type), 'spawnDaemons type is not valid')

Expand All @@ -20,6 +21,12 @@ async function spawnDaemons (n, type = 'js') {
types = new Array(n).fill(type)
}

let daemonOptions = options

if (!Array.isArray(daemonOptions)) {
daemonOptions = new Array(n).fill(options)
}

const daemons = []
let daemon

Expand All @@ -28,7 +35,7 @@ async function spawnDaemons (n, type = 'js') {
daemons.push(daemon)
}

await Promise.all(daemons.map((daemon) => daemon.start()))
await Promise.all(daemons.map((daemon, i) => daemon.start(daemonOptions[i])))

return daemons
}
Expand Down

0 comments on commit c14b887

Please sign in to comment.