Skip to content

Commit

Permalink
feat: ipfsd-ctl server can find free ports
Browse files Browse the repository at this point in the history
tests can be ran in parallel in multiple ports and IPFSD_CTL_SERVER can be sent to the bundle to make tests connect to the right port
  • Loading branch information
hugomrdias committed Apr 20, 2020
1 parent b616ac2 commit db64997
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
42 changes: 23 additions & 19 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

const createServer = require('./src').createServer

const server = createServer(null, {
ipfsModule: require('ipfs'),
ipfsHttpModule: require('ipfs-http-client')
}, {
go: {
ipfsBin: require('go-ipfs-dep').path()
},
js: {
ipfsBin: require.resolve('ipfs/src/cli/bin.js')
const server = createServer(null,
{
ipfsModule: require('ipfs'),
ipfsHttpModule: require('ipfs-http-client')
},
{
go: {
ipfsBin: require('go-ipfs-dep').path()
},
js: {
ipfsBin: require.resolve('ipfs/src/cli/bin.js')
}
}
}) // using defaults
)

module.exports = {
bundlesize: { maxSize: '35kB' },
bundlesize: { maxSize: '33kB' },
karma: {
files: [{
pattern: 'test/fixtures/**/*',
Expand All @@ -24,14 +28,14 @@ module.exports = {
}]
},
hooks: {
pre: () => server.start(),
pre: async () => {
await server.start()
return {
env: {
IPFSD_CTL_SERVER: `http://localhost:${server.port}`
}
}
},
post: () => server.stop()
},
webpack: process.env.NODE_ENV === 'test' ? undefined : {
externals: {
ipfs: 'ipfs',
'ipfs-http-client': 'ipfs-http-client',
'go-ipfs-dep': 'go-ipfs-dep'
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const server = Ctl.createServer(port, {
const factory = Ctl.createFactory({
ipfsHttpModule: require('ipfs-http-client'),
remote: true,
endpoint: `http://localhost:${port}`
endpoint: `http://localhost:${port}` // or you can set process.env.IPFSD_CTL_SERVER to http://localhost:9090
})

await server.start()
Expand Down
7 changes: 4 additions & 3 deletions src/endpoint/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const Hapi = require('@hapi/hapi')
const getPort = require('aegir/utils/get-port')
const routes = require('./routes')

/**
Expand All @@ -15,10 +16,9 @@ class Server {
* @param {function} createFactory
*/
constructor (options, createFactory) {
options = options || { port: 43134 }

this.options = options || { port: 43134 }
this.server = null
this.port = options.port
this.port = this.options.port
this.createFactory = createFactory
}

Expand All @@ -28,6 +28,7 @@ class Server {
* @returns {Promise<Hapi.Server>}
*/
async start () {
this.port = await getPort(this.options.port, 'localhost')
this.server = new Hapi.Server({
port: this.port,
host: 'localhost',
Expand Down
2 changes: 1 addition & 1 deletion src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const testsConfig = require('./config')

const defaults = {
remote: !isNode,
endpoint: 'http://localhost:43134',
endpoint: process.env.IPFSD_CTL_SERVER || 'http://localhost:43134',
disposable: true,
test: false,
type: 'go',
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const Server = require('./endpoint/server')
const Factory = require('./factory')
const Server = require('./endpoint/server')

/** @typedef {import("./ipfsd-daemon")} Controller */

Expand Down

0 comments on commit db64997

Please sign in to comment.