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

Commit 4479df9

Browse files
author
Pedro Santos
committed
refactor: pubsub before and after methods to async syntax
1 parent 9953c7f commit 4479df9

File tree

5 files changed

+24
-92
lines changed

5 files changed

+24
-92
lines changed

src/pubsub/ls.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,8 @@ module.exports = (createCommon, options) => {
1616
let ipfs
1717
let subscribedTopics = []
1818

19-
before(function (done) {
20-
// CI takes longer to instantiate the daemon, so we need to increase the
21-
// timeout for the before step
22-
this.timeout(60 * 1000)
23-
24-
common.setup((err, factory) => {
25-
expect(err).to.not.exist()
26-
factory.spawnNode((err, node) => {
27-
expect(err).to.not.exist()
28-
ipfs = node
29-
done()
30-
})
31-
})
19+
before(async () => {
20+
ipfs = await common.setup()
3221
})
3322

3423
afterEach(async () => {
@@ -39,7 +28,7 @@ module.exports = (createCommon, options) => {
3928
await delay(100)
4029
})
4130

42-
after((done) => common.teardown(done))
31+
after(() => common.teardown())
4332

4433
it('should return an empty list when no topics are subscribed', async () => {
4534
const topics = await ipfs.pubsub.ls()

src/pubsub/peers.js

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const parallel = require('async/parallel')
5-
const { spawnNodesWithId } = require('../utils/spawn')
64
const { waitForPeers, getTopic } = require('./utils')
75
const { getDescribe, getIt, expect } = require('../utils/mocha')
8-
const { connect } = require('../utils/swarm')
96
const delay = require('delay')
107

118
module.exports = (createCommon, options) => {
@@ -21,24 +18,17 @@ module.exports = (createCommon, options) => {
2118
let ipfs3
2219
let subscribedTopics = []
2320

24-
before(function (done) {
25-
// CI takes longer to instantiate the daemon, so we need to increase the
26-
// timeout for the before step
27-
this.timeout(100 * 1000)
21+
before(async () => {
22+
ipfs1 = await common.setup()
23+
ipfs2 = await common.setup()
24+
ipfs3 = await common.setup()
2825

29-
common.setup((err, factory) => {
30-
if (err) return done(err)
31-
32-
spawnNodesWithId(3, factory, (err, nodes) => {
33-
if (err) return done(err)
34-
35-
ipfs1 = nodes[0]
36-
ipfs2 = nodes[1]
37-
ipfs3 = nodes[2]
26+
const ipfs2Addr = ipfs2.peerId.addresses.find((a) => a.includes('127.0.0.1'))
27+
const ipfs3Addr = ipfs3.peerId.addresses.find((a) => a.includes('127.0.0.1'))
3828

39-
done()
40-
})
41-
})
29+
await ipfs1.swarm.connect(ipfs2Addr)
30+
await ipfs1.swarm.connect(ipfs3Addr)
31+
await ipfs2.swarm.connect(ipfs3Addr)
4232
})
4333

4434
afterEach(async () => {
@@ -51,17 +41,7 @@ module.exports = (createCommon, options) => {
5141
await delay(100)
5242
})
5343

54-
after((done) => common.teardown(done))
55-
56-
before((done) => {
57-
const ipfs2Addr = ipfs2.peerId.addresses.find((a) => a.includes('127.0.0.1'))
58-
const ipfs3Addr = ipfs3.peerId.addresses.find((a) => a.includes('127.0.0.1'))
59-
60-
parallel([
61-
(cb) => connect(ipfs1, [ipfs2Addr, ipfs3Addr], cb),
62-
(cb) => connect(ipfs2, ipfs3Addr, cb)
63-
], done)
64-
})
44+
after(() => common.teardown())
6545

6646
it('should not error when not subscribed to a topic', async () => {
6747
const topic = getTopic()

src/pubsub/publish.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const hat = require('hat')
55
const { getTopic } = require('./utils')
6-
const { getDescribe, getIt, expect } = require('../utils/mocha')
6+
const { getDescribe, getIt } = require('../utils/mocha')
77

88
module.exports = (createCommon, options) => {
99
const describe = getDescribe(options)
@@ -15,22 +15,11 @@ module.exports = (createCommon, options) => {
1515

1616
let ipfs
1717

18-
before(function (done) {
19-
// CI takes longer to instantiate the daemon, so we need to increase the
20-
// timeout for the before step
21-
this.timeout(60 * 1000)
22-
23-
common.setup((err, factory) => {
24-
expect(err).to.not.exist()
25-
factory.spawnNode((err, node) => {
26-
expect(err).to.not.exist()
27-
ipfs = node
28-
done()
29-
})
30-
})
18+
before(async () => {
19+
ipfs = await common.setup()
3120
})
3221

33-
after((done) => common.teardown(done))
22+
after(() => common.teardown())
3423

3524
it('should publish message from string', () => {
3625
const topic = getTopic()

src/pubsub/subscribe.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
const pushable = require('it-pushable')
66
const { collect } = require('streaming-iterables')
7-
const { spawnNodesWithId } = require('../utils/spawn')
87
const { waitForPeers, getTopic } = require('./utils')
98
const { getDescribe, getIt, expect } = require('../utils/mocha')
109
const delay = require('delay')
@@ -22,23 +21,9 @@ module.exports = (createCommon, options) => {
2221
let topic
2322
let subscribedTopics = []
2423

25-
before(function (done) {
26-
// CI takes longer to instantiate the daemon, so we need to increase the
27-
// timeout for the before step
28-
this.timeout(100 * 1000)
29-
30-
common.setup((err, factory) => {
31-
if (err) return done(err)
32-
33-
spawnNodesWithId(2, factory, (err, nodes) => {
34-
if (err) return done(err)
35-
36-
ipfs1 = nodes[0]
37-
ipfs2 = nodes[1]
38-
39-
done()
40-
})
41-
})
24+
before(async () => {
25+
ipfs1 = await common.setup()
26+
ipfs2 = await common.setup()
4227
})
4328

4429
beforeEach(() => {
@@ -56,7 +41,7 @@ module.exports = (createCommon, options) => {
5641
await delay(100)
5742
})
5843

59-
after((done) => common.teardown(done))
44+
after(() => common.teardown())
6045

6146
describe('single node', () => {
6247
it('should subscribe to one topic', async () => {

src/pubsub/unsubscribe.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,11 @@ module.exports = (createCommon, options) => {
1616

1717
let ipfs
1818

19-
before(function (done) {
20-
// CI takes longer to instantiate the daemon, so we need to increase the
21-
// timeout for the before step
22-
this.timeout(60 * 1000)
23-
24-
common.setup((err, factory) => {
25-
expect(err).to.not.exist()
26-
factory.spawnNode((err, node) => {
27-
expect(err).to.not.exist()
28-
ipfs = node
29-
done()
30-
})
31-
})
19+
before(async () => {
20+
ipfs = await common.setup()
3221
})
3322

34-
after((done) => common.teardown(done))
23+
after(() => common.teardown())
3524

3625
// Browser/worker has max ~5 open HTTP requests to the same origin
3726
const count = isBrowser || isWebWorker || isElectronRenderer ? 5 : 10

0 commit comments

Comments
 (0)