Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 2cd11f0

Browse files
committedOct 25, 2015
gulpify start and stop of daemons
1 parent ec0336d commit 2cd11f0

File tree

3 files changed

+102
-74
lines changed

3 files changed

+102
-74
lines changed
 

‎gulpfile.js

+92-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,106 @@
11
var gulp = require('gulp')
22
// var Server = require('karma').Server;
33
var mocha = require('gulp-mocha')
4+
var ipfsd = require('ipfsd-ctl')
5+
var fs = require('fs')
46

57
gulp.task('default', function () {
68
gulp.start('test:node')
79
})
810

911
gulp.task('test:node', function (done) {
10-
// gulp.start('start-disposable-daemons')
11-
gulp.src('test/tests.js')
12-
// gulp-mocha needs filepaths so you can't have any plugins before it
13-
.pipe(mocha())
14-
.once('error', function () {
15-
process.exit(1)
12+
startDisposableDaemons(function (daemons) {
13+
gulp.src('test/tests.js')
14+
// gulp-mocha needs filepaths so you can't have any plugins before it
15+
.pipe(mocha())
16+
.once('error', function () {
17+
process.exit(1)
18+
})
19+
.once('end', function () {
20+
stopDisposableDaemons(daemons, function () {
21+
process.exit()
22+
})
23+
})
24+
})
25+
})
26+
27+
function startDisposableDaemons (callback) {
28+
var ipfsNodes = {} // a, b, c
29+
var apiAddrs = {} // a, b, c
30+
31+
var counter = 0
32+
startIndependentNode(ipfsNodes, apiAddrs, 'a', finish)
33+
startIndependentNode(ipfsNodes, apiAddrs, 'b', finish)
34+
startIndependentNode(ipfsNodes, apiAddrs, 'c', finish)
35+
36+
function finish () {
37+
counter++
38+
if (counter === 3) {
39+
fs.writeFileSync(__dirname + '/test/tmp-disposable-nodes-addrs.json', JSON.stringify(apiAddrs))
40+
callback(ipfsNodes)
41+
}
42+
}
43+
44+
function startIndependentNode (ipfsNodes, apiAddrs, key, cb) {
45+
ipfsd.disposable(function (err, node) {
46+
if (err) {
47+
throw err
48+
}
49+
50+
console.log('ipfs init done - ' + key)
51+
ipfsNodes[key] = node
52+
53+
console.log('ipfs config (bootstrap and mdns off) - ' + key)
54+
55+
ipfsNodes[key].setConfig('Bootstrap', null, function (err) {
56+
if (err) {
57+
throw err
58+
}
59+
ipfsNodes[key].setConfig('Mdns', false, function (err) {
60+
if (err) {
61+
throw err
62+
}
63+
64+
ipfsNodes[key].startDaemon(function (err, ignore) {
65+
if (err) {
66+
throw err
67+
}
68+
69+
apiAddrs[key] = ipfsNodes[key].apiAddr
70+
cb()
71+
})
72+
})
73+
})
1674
})
17-
.once('end', function () {
18-
// gulp.start('stop-disposable-daemons')
19-
process.exit()
75+
}
76+
}
77+
78+
function stopDisposableDaemons (daemons, callback) {
79+
stopIPFSNode(daemons, 'a', finish)
80+
stopIPFSNode(daemons, 'b', finish)
81+
stopIPFSNode(daemons, 'c', finish)
82+
83+
var counter = 0
84+
function finish () {
85+
counter++
86+
if (counter === 3) {
87+
callback()
88+
}
89+
}
90+
91+
function stopIPFSNode (daemons, key, cb) {
92+
var nodeStopped
93+
daemons[key].stopDaemon(function (err) {
94+
if (err) {
95+
throw err
96+
}
97+
if (!nodeStopped) {
98+
nodeStopped = true
99+
cb()
100+
}
20101
})
21-
})
102+
}
103+
}
22104

23105
/*
24106
gulp.task('test', function (done) {

‎test/tests.js

+9-64
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,26 @@ var fs = require('fs')
44
var path = require('path')
55
var File = require('vinyl')
66

7-
var isNode = !global.window
8-
9-
if (isNode) {
10-
var ipfsd = require('ipfsd-ctl')
11-
}
7+
// var isNode = !global.window
128

139
// this comment is used by mocha, do not delete
1410
/* global describe, before, it */
1511

16-
function log () {
17-
var args = [' #']
18-
args = args.concat(Array.prototype.slice.call(arguments))
19-
console.log.apply(console, args)
20-
}
21-
2212
var testfilePath = __dirname + '/testfile.txt'
2313
var testfile = fs.readFileSync(__dirname + '/testfile.txt')
2414

2515
describe('IPFS Node.js API wrapper tests', function () {
2616
var apiClients = {} // a, b, c
27-
var ipfsNodes = {} // a, b, c
17+
var apiAddrs = require('./tmp-disposable-nodes-addrs.json')
2818

2919
before(function (done) {
3020
this.timeout(20000)
31-
log('ipfs node setup')
32-
var counter = 0
33-
34-
if (isNode) {
35-
startIndependentNode(ipfsNodes, apiClients, 'a', finish)
36-
startIndependentNode(ipfsNodes, apiClients, 'b', finish)
37-
startIndependentNode(ipfsNodes, apiClients, 'c', finish)
38-
} else {
39-
apiClients['a'] = ipfsAPI('localhost', '5001')
40-
done()
41-
}
42-
43-
function finish () {
44-
counter++
45-
if (counter === 3) {
46-
done()
47-
}
48-
}
49-
50-
function startIndependentNode (ipfsNodes, apiClients, key, cb) {
51-
ipfsd.disposable(function (err, node) {
52-
if (err) {
53-
throw err
54-
}
55-
56-
log('ipfs init done - ' + key)
57-
ipfsNodes[key] = node
58-
59-
log('ipfs config (bootstrap and mdns off) - ' + key)
60-
61-
ipfsNodes[key].setConfig('Bootstrap', null, function (err) {
62-
if (err) {
63-
throw err
64-
}
65-
ipfsNodes[key].setConfig('Mdns', false, function (err) {
66-
if (err) {
67-
throw err
68-
}
6921

70-
ipfsNodes[key].startDaemon(function (err, ignore) {
71-
if (err) {
72-
throw err
73-
}
74-
log('ipfs daemon running - ' + key)
22+
Object.keys(apiAddrs).forEach(function (key) {
23+
apiClients[key] = ipfsAPI(apiAddrs[key])
24+
})
7525

76-
apiClients[key] = ipfsAPI(ipfsNodes[key].apiAddr)
77-
cb()
78-
})
79-
})
80-
})
81-
})
82-
}
26+
done()
8327
})
8428

8529
it('connect Node a to b and c', function (done) {
@@ -100,7 +44,7 @@ describe('IPFS Node.js API wrapper tests', function () {
10044
if (err) {
10145
throw err
10246
}
103-
addrs[key] = ipfsNodes[key].apiAddr + '/ipfs/' + id.ID
47+
addrs[key] = apiAddrs[key] + '/ipfs/' + id.ID
10448
cb()
10549
})
10650
}
@@ -564,7 +508,7 @@ describe('IPFS Node.js API wrapper tests', function () {
564508

565509
it('.dht.findproovs')
566510
})
567-
511+
/*
568512
describe('closing tests', function () {
569513
if (isNode) {
570514
// Not available in the browser
@@ -601,4 +545,5 @@ describe('IPFS Node.js API wrapper tests', function () {
601545
})
602546
}
603547
})
548+
*/
604549
})

‎test/tmp-disposable-nodes-addrs.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"c":"/ip4/127.0.0.1/tcp/51626","b":"/ip4/127.0.0.1/tcp/51637","a":"/ip4/127.0.0.1/tcp/51652"}

0 commit comments

Comments
 (0)
This repository has been archived.