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

Commit bc7dcef

Browse files
author
Pedro Santos
committed
refactor: dht before and after methods to async syntax
1 parent ba18c86 commit bc7dcef

File tree

6 files changed

+36
-137
lines changed

6 files changed

+36
-137
lines changed

src/dht/find-peer.js

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { spawnNodesWithId } = require('../utils/spawn')
54
const { getDescribe, getIt, expect } = require('../utils/mocha')
6-
const { connect } = require('../utils/swarm')
75

86
module.exports = (createCommon, options) => {
97
const describe = getDescribe(options)
@@ -16,30 +14,13 @@ module.exports = (createCommon, options) => {
1614
let nodeA
1715
let nodeB
1816

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-
27-
spawnNodesWithId(2, factory, (err, nodes) => {
28-
expect(err).to.not.exist()
29-
30-
nodeA = nodes[0]
31-
nodeB = nodes[1]
32-
33-
connect(nodeB, nodeA.peerId.addresses[0], done)
34-
})
35-
})
17+
before(async () => {
18+
nodeA = await common.setup()
19+
nodeB = await common.setup()
20+
await nodeB.swarm.connect(nodeA.peerId.addresses[0])
3621
})
3722

38-
after(function (done) {
39-
this.timeout(50 * 1000)
40-
41-
common.teardown(done)
42-
})
23+
after(() => common.teardown())
4324

4425
it('should find other peers', async () => {
4526
const res = await nodeA.dht.findPeer(nodeB.peerId.id)

src/dht/find-provs.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
'use strict'
33

44
const multihashing = require('multihashing-async')
5-
const parallel = require('async/parallel')
65
const CID = require('cids')
7-
const { spawnNodesWithId } = require('../utils/spawn')
86
const { getDescribe, getIt, expect } = require('../utils/mocha')
9-
const { connect } = require('../utils/swarm')
107

118
async function fakeCid () {
129
const bytes = Buffer.from(`TEST${Date.now()}`)
@@ -26,29 +23,18 @@ module.exports = (createCommon, options) => {
2623
let nodeB
2724
let nodeC
2825

29-
before(function (done) {
30-
// CI takes longer to instantiate the daemon, so we need to increase the
31-
// timeout for the before step
32-
this.timeout(60 * 1000)
33-
34-
common.setup((err, factory) => {
35-
expect(err).to.not.exist()
36-
37-
spawnNodesWithId(3, factory, (err, nodes) => {
38-
expect(err).to.not.exist()
39-
40-
nodeA = nodes[0]
41-
nodeB = nodes[1]
42-
nodeC = nodes[2]
43-
44-
parallel([
45-
(cb) => connect(nodeB, nodeA.peerId.addresses[0], cb),
46-
(cb) => connect(nodeC, nodeB.peerId.addresses[0], cb)
47-
], done)
48-
})
49-
})
26+
before(async () => {
27+
nodeA = await common.setup()
28+
nodeB = await common.setup()
29+
nodeC = await common.setup()
30+
await Promise.all([
31+
nodeB.swarm.connect(nodeA.peerId.addresses[0]),
32+
nodeC.swarm.connect(nodeB.peerId.addresses[0])
33+
])
5034
})
5135

36+
after(() => common.teardown())
37+
5238
let providedCid
5339
before('add providers for the same cid', async function () {
5440
const cids = await Promise.all([

src/dht/get.js

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
'use strict'
33

44
const hat = require('hat')
5-
const { spawnNodesWithId } = require('../utils/spawn')
65
const { getDescribe, getIt, expect } = require('../utils/mocha')
7-
const { connect } = require('../utils/swarm')
86

97
module.exports = (createCommon, options) => {
108
const describe = getDescribe(options)
@@ -17,30 +15,13 @@ module.exports = (createCommon, options) => {
1715
let nodeA
1816
let nodeB
1917

20-
before(function (done) {
21-
// CI takes longer to instantiate the daemon, so we need to increase the
22-
// timeout for the before step
23-
this.timeout(60 * 1000)
24-
25-
common.setup((err, factory) => {
26-
expect(err).to.not.exist()
27-
28-
spawnNodesWithId(2, factory, (err, nodes) => {
29-
expect(err).to.not.exist()
30-
31-
nodeA = nodes[0]
32-
nodeB = nodes[1]
33-
34-
connect(nodeA, nodeB.peerId.addresses[0], done)
35-
})
36-
})
18+
before(async () => {
19+
nodeA = await common.setup()
20+
nodeB = await common.setup()
21+
await nodeA.swarm.connect(nodeB.peerId.addresses[0])
3722
})
3823

39-
after(function (done) {
40-
this.timeout(50 * 1000)
41-
42-
common.teardown(done)
43-
})
24+
after(() => common.teardown())
4425

4526
it('should error when getting a non-existent key from the DHT', () => {
4627
return expect(nodeA.dht.get('non-existing', { timeout: 100 })).to.eventually.be.rejected

src/dht/provide.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
'use strict'
33

44
const CID = require('cids')
5-
const { spawnNodesWithId } = require('../utils/spawn')
65
const { getDescribe, getIt, expect } = require('../utils/mocha')
7-
const { connect } = require('../utils/swarm')
86

97
module.exports = (createCommon, options) => {
108
const describe = getDescribe(options)
@@ -16,27 +14,13 @@ module.exports = (createCommon, options) => {
1614

1715
let ipfs
1816

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-
27-
spawnNodesWithId(2, factory, (err, nodes) => {
28-
expect(err).to.not.exist()
29-
ipfs = nodes[0]
30-
connect(ipfs, nodes[1].peerId.addresses[0], done)
31-
})
32-
})
17+
before(async () => {
18+
ipfs = await common.setup()
19+
const nodeB = await common.setup()
20+
await ipfs.swarm.connect(nodeB.peerId.addresses[0])
3321
})
3422

35-
after(function (done) {
36-
this.timeout(50 * 1000)
37-
38-
common.teardown(done)
39-
})
23+
after(() => common.teardown())
4024

4125
it('should provide local CID', async () => {
4226
const res = await ipfs.add(Buffer.from('test'))

src/dht/put.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { spawnNodesWithId } = require('../utils/spawn')
5-
const { getDescribe, getIt, expect } = require('../utils/mocha')
6-
const { connect } = require('../utils/swarm')
4+
const { getDescribe, getIt } = require('../utils/mocha')
75

86
module.exports = (createCommon, options) => {
97
const describe = getDescribe(options)
@@ -16,25 +14,13 @@ module.exports = (createCommon, options) => {
1614
let nodeA
1715
let nodeB
1816

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-
27-
spawnNodesWithId(2, factory, (err, nodes) => {
28-
expect(err).to.not.exist()
29-
30-
nodeA = nodes[0]
31-
nodeB = nodes[1]
32-
connect(nodeA, nodeB.peerId.addresses[0], done)
33-
})
34-
})
17+
before(async () => {
18+
nodeA = await common.setup()
19+
nodeB = await common.setup()
20+
await nodeA.swarm.connect(nodeB.peerId.addresses[0])
3521
})
3622

37-
after((done) => common.teardown(done))
23+
after(() => common.teardown())
3824

3925
it('should put a value to the DHT', async () => {
4026
const key = Buffer.from('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')

src/dht/query.js

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
'use strict'
33

44
const pTimeout = require('p-timeout')
5-
const { spawnNodesWithId } = require('../utils/spawn')
65
const { getDescribe, getIt, expect } = require('../utils/mocha')
7-
const { connect } = require('../utils/swarm')
86

97
module.exports = (createCommon, options) => {
108
const describe = getDescribe(options)
@@ -17,30 +15,13 @@ module.exports = (createCommon, options) => {
1715
let nodeA
1816
let nodeB
1917

20-
before(function (done) {
21-
// CI takes longer to instantiate the daemon, so we need to increase the
22-
// timeout for the before step
23-
this.timeout(60 * 1000)
24-
25-
common.setup((err, factory) => {
26-
expect(err).to.not.exist()
27-
28-
spawnNodesWithId(2, factory, (err, nodes) => {
29-
expect(err).to.not.exist()
30-
31-
nodeA = nodes[0]
32-
nodeB = nodes[1]
33-
34-
connect(nodeB, nodeA.peerId.addresses[0], done)
35-
})
36-
})
18+
before(async () => {
19+
nodeA = await common.setup()
20+
nodeB = await common.setup()
21+
await nodeB.swarm.connect(nodeA.peerId.addresses[0])
3722
})
3823

39-
after(function (done) {
40-
this.timeout(50 * 1000)
41-
42-
common.teardown(done)
43-
})
24+
after(() => common.teardown())
4425

4526
it('should return the other node in the query', async function () {
4627
const timeout = 150 * 1000

0 commit comments

Comments
 (0)