Skip to content

Commit 8e1c3cf

Browse files
authored
devp2p: remove async dep from integration tests (#1875)
1 parent 5851e72 commit 8e1c3cf

File tree

4 files changed

+103
-149
lines changed

4 files changed

+103
-149
lines changed

packages/devp2p/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
"devDependencies": {
6565
"@ethereumjs/block": "^3.6.2",
6666
"@ethereumjs/tx": "^3.5.1",
67-
"@types/async": "^2.4.1",
6867
"@types/chalk": "^2.2.0",
6968
"@types/debug": "^4.1.4",
7069
"@types/ip": "^1.1.0",
@@ -73,7 +72,6 @@
7372
"@types/node": "^16.11.7",
7473
"@types/secp256k1": "^4.0.1",
7574
"@types/tape": "^4.13.2",
76-
"async": "^2.6.0",
7775
"chalk": "^2.4.2",
7876
"eslint": "^6.8.0",
7977
"nyc": "^15.1.0",
Lines changed: 55 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import async from 'async'
21
import test from 'tape'
32
import * as util from './util'
43
import testdata from '../testdata.json'
54

6-
async function delay(ms: number) {
7-
await new Promise((resolve) => setTimeout(resolve, ms))
8-
}
9-
105
test('DPT: new working node', (t) => {
116
const dpts = util.initTwoPeerDPTSetup()
127

13-
dpts[0].on('peer:new', function (peer: any) {
8+
dpts[0].on('peer:new', async (peer: any) => {
149
t.equal(peer.address, '127.0.0.1', 'should have added peer on peer:new')
10+
await util.delay(500)
1511
util.destroyDPTs(dpts)
1612
t.end()
1713
})
@@ -20,8 +16,9 @@ test('DPT: new working node', (t) => {
2016
test('DPT: working node added', (t) => {
2117
const dpts = util.initTwoPeerDPTSetup()
2218

23-
dpts[0].on('peer:added', function () {
19+
dpts[0].on('peer:added', async () => {
2420
t.equal(dpts[0].getPeers().length, 1, 'should have added peer to k-bucket on peer:added')
21+
await util.delay(500)
2522
util.destroyDPTs(dpts)
2623
t.end()
2724
})
@@ -30,102 +27,74 @@ test('DPT: working node added', (t) => {
3027
test('DPT: remove node', (t) => {
3128
const dpts = util.initTwoPeerDPTSetup()
3229

33-
async.series(
34-
[
35-
function (cb) {
36-
dpts[0].on('peer:added', function (peer) {
37-
dpts[0].removePeer(peer)
38-
cb(null)
39-
})
40-
},
41-
function (cb) {
42-
dpts[0].on('peer:removed', function () {
43-
t.equal(
44-
dpts[0].getPeers().length,
45-
0,
46-
'should have removed peer from k-bucket on peer:removed'
47-
)
48-
cb(null)
49-
})
50-
},
51-
],
52-
function (err) {
53-
if (err) {
54-
t.fail('An unexpected error occured.')
55-
}
30+
try {
31+
dpts[0].on('peer:added', async (peer) => {
32+
await util.delay(400)
33+
dpts[0].removePeer(peer)
34+
})
35+
dpts[0].on('peer:removed', async () => {
36+
t.equal(
37+
dpts[0].getPeers().length,
38+
0,
39+
'should have removed peer from k-bucket on peer:removed'
40+
)
41+
await util.delay(500)
5642
util.destroyDPTs(dpts)
5743
t.end()
58-
}
59-
)
44+
})
45+
} catch (err) {
46+
t.fail(`An unexpected error occurred: ${err}`)
47+
}
6048
})
6149

6250
test('DPT: ban node', (t) => {
6351
const dpts = util.initTwoPeerDPTSetup()
6452

65-
async.series(
66-
[
67-
function (cb) {
68-
dpts[0].on('peer:added', function (peer) {
69-
dpts[0].banPeer(peer)
70-
cb(null)
71-
})
72-
},
73-
function (cb) {
74-
dpts[0].on('peer:removed', function (peer) {
75-
t.equal(dpts[0].banlist.has(peer), true, 'ban-list should contain peer')
76-
t.equal(
77-
dpts[0].getPeers().length,
78-
0,
79-
'should have removed peer from k-bucket on peer:removed'
80-
)
81-
cb(null)
82-
})
83-
},
84-
],
85-
function (err) {
86-
if (err) {
87-
t.fail('An unexpected error occured.')
88-
}
53+
try {
54+
dpts[0].once('peer:added', async (peer) => {
55+
await util.delay(400)
56+
dpts[0].banPeer(peer)
57+
})
58+
dpts[0].once('peer:removed', async (peer) => {
59+
t.equal(dpts[0].banlist.has(peer), true, 'ban-list should contain peer')
60+
t.equal(
61+
dpts[0].getPeers().length,
62+
0,
63+
'should have removed peer from k-bucket on peer:removed'
64+
)
65+
await util.delay(500)
8966
util.destroyDPTs(dpts)
9067
t.end()
91-
}
92-
)
68+
})
69+
} catch (err) {
70+
t.fail(`An unexpected error occurred: ${err}`)
71+
}
9372
})
9473

9574
test('DPT: k-bucket ping', (t) => {
9675
const dpts = util.initTwoPeerDPTSetup()
9776

98-
async.series(
99-
[
100-
function (cb) {
101-
dpts[0].on('peer:added', function (peer: any) {
102-
dpts[0]._onKBucketPing([peer], peer)
103-
setTimeout(function () {
104-
cb(null)
105-
}, 400)
106-
})
107-
},
108-
function (cb) {
109-
t.equal(dpts[0].getPeers().length, 1, 'should still have one peer in k-bucket')
110-
cb(null)
111-
},
112-
],
113-
function (err) {
114-
if (err) {
115-
t.fail('An unexpected error occured.')
116-
}
77+
try {
78+
dpts[0].once('peer:added', async (peer: any) => {
79+
dpts[0]._onKBucketPing([peer], peer)
80+
await util.delay(400)
81+
t.equal(dpts[0].getPeers().length, 1, 'should still have one peer in k-bucket')
82+
await util.delay(400)
11783
util.destroyDPTs(dpts)
11884
t.end()
119-
}
120-
)
85+
})
86+
} catch (err) {
87+
t.fail(`An unexpected error occurred: ${err}`)
88+
}
12189
})
12290

12391
test('DPT: add non-available node', async (t) => {
12492
const dpts = util.getTestDPTs(1)
12593
const peer = { address: util.localhost, udpPort: util.basePort + 1 }
12694

127-
await dpts[0].addPeer(peer).catch((e: Error) => {
95+
await dpts[0].addPeer(peer).catch(async (e: Error) => {
12896
t.equal(e.message, 'Timeout error: ping 127.0.0.1:30307', 'should throw Timeout error')
97+
await util.delay(400)
12998
util.destroyDPTs(dpts)
13099
})
131100
})
@@ -134,9 +103,9 @@ test('DPT: simulate bootstrap', async (t) => {
134103
const numDPTs = 6
135104
const dpts = util.getTestDPTs(numDPTs)
136105

137-
await delay(250)
106+
await util.delay(250)
138107
await dpts[0].addPeer({ address: util.localhost, udpPort: util.basePort + 1 })
139-
await delay(100)
108+
await util.delay(100)
140109

141110
for (const dpt of dpts.slice(2)) {
142111
await dpt.bootstrap({ address: util.localhost, udpPort: util.basePort + 1 })
@@ -146,16 +115,16 @@ test('DPT: simulate bootstrap', async (t) => {
146115
for (let i = 0; i < 10; i++) {
147116
dpt.refresh()
148117
}
149-
await delay(400)
118+
await util.delay(400)
150119
}
151120

152-
await delay(250)
121+
await util.delay(250)
153122

154123
// dpts.forEach((dpt, i) => console.log(`${i}:${dpt.getPeers().length}`))
155124
for (const dpt of dpts) {
156125
t.equal(dpt.getPeers().length, numDPTs, 'Peers should be distributed to all DPTs')
157126
}
158-
await delay(1000)
127+
await util.delay(1000)
159128

160129
util.destroyDPTs(dpts)
161130
})
@@ -171,7 +140,7 @@ test('DPT: simulate acquiring peers via DNS', async () => {
171140

172141
dpts[0].dns.__setNativeDNSModuleResolve(mockDns)
173142
dpts[0].refresh()
174-
await delay(400)
143+
await util.delay(400)
175144

176145
util.destroyDPTs(dpts)
177146
})
Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
import async from 'async'
21
import test from 'tape'
32
import * as util from './util'
43
import { DISCONNECT_REASONS } from '../../src/rlpx/peer'
54

65
test('RLPX: add working node', (t) => {
76
const rlpxs = util.initTwoPeerRLPXSetup()
87

9-
rlpxs[0].on('peer:added', function (peer: any) {
8+
rlpxs[0].on('peer:added', async (peer: any) => {
109
t.equal(peer._port, 30306, 'should have added peer on peer:added after successful handshake')
1110
t.equal(rlpxs[0].getPeers().length, 1, 'peer list length should be 1')
1211
t.equal(rlpxs[0]._getOpenSlots(), 9, 'should have maxPeers - 1 open slots left')
12+
await util.delay(500)
1313
util.destroyRLPXs(rlpxs)
1414
t.end()
1515
})
1616
})
1717

1818
test('RLPX: ban node with missing tcp port', (t) => {
1919
const rlpxs = util.initTwoPeerRLPXSetup()
20-
rlpxs[0].on('peer:added', function () {
20+
21+
rlpxs[0].on('peer:added', async () => {
2122
const peer = {
2223
id: Buffer.from('abcd', 'hex'),
2324
address: '127.0.0.1',
@@ -30,6 +31,7 @@ test('RLPX: ban node with missing tcp port', (t) => {
3031
)
3132
rlpxs[0]._dpt!.emit('peer:new', peer)
3233
t.ok(rlpxs[0]._dpt!.banlist.has(peer), 'should be in ban list after bad peer discovered')
34+
await util.delay(500)
3335
util.destroyRLPXs(rlpxs)
3436
t.end()
3537
})
@@ -38,70 +40,51 @@ test('RLPX: ban node with missing tcp port', (t) => {
3840
test('RLPX: remove node', (t) => {
3941
const rlpxs = util.initTwoPeerRLPXSetup()
4042

41-
async.series(
42-
[
43-
function (cb) {
44-
rlpxs[0].on('peer:added', function (peer: any) {
45-
rlpxs[0].disconnect(peer._remoteId)
46-
cb(null)
47-
})
48-
},
49-
function (cb) {
50-
rlpxs[0].on('peer:removed', function (peer: any, reason: any) {
51-
t.equal(
52-
reason,
53-
DISCONNECT_REASONS.CLIENT_QUITTING,
54-
'should close with CLIENT_QUITTING disconnect reason'
55-
)
56-
t.equal(rlpxs[0]._getOpenSlots(), 10, 'should have maxPeers open slots left')
57-
cb(null)
58-
})
59-
},
60-
],
61-
function (err) {
62-
if (err) {
63-
t.fail('An unexpected error occured.')
64-
}
43+
try {
44+
rlpxs[0].once('peer:added', (peer: any) => {
45+
rlpxs[0].disconnect(peer._remoteId)
46+
})
47+
rlpxs[0].once('peer:removed', async (peer: any, reason: any) => {
48+
t.equal(
49+
reason,
50+
DISCONNECT_REASONS.CLIENT_QUITTING,
51+
'should close with CLIENT_QUITTING disconnect reason'
52+
)
53+
t.equal(rlpxs[0]._getOpenSlots(), 10, 'should have maxPeers open slots left')
54+
await util.delay(500)
6555
util.destroyRLPXs(rlpxs)
6656
t.end()
67-
}
68-
)
57+
})
58+
} catch (err) {
59+
t.fail(`An unexpected error occurred: ${err}`)
60+
}
6961
})
7062

7163
test('RLPX: test peer queue / refill connections', (t) => {
7264
const rlpxs = util.getTestRLPXs(3, 1)
7365

7466
const peer = { address: util.localhost, udpPort: util.basePort + 1, tcpPort: util.basePort + 1 }
7567
rlpxs[0]._dpt!.addPeer(peer)
76-
77-
async.series(
78-
[
79-
function (cb) {
80-
rlpxs[0].once('peer:added', function () {
81-
t.equal(rlpxs[0]._peersQueue.length, 0, 'peers queue should contain no peers')
82-
const peer2 = {
83-
address: util.localhost,
84-
udpPort: util.basePort + 2,
85-
tcpPort: util.basePort + 2,
86-
}
87-
rlpxs[0]._dpt!.addPeer(peer2)
88-
cb(null)
89-
})
90-
},
91-
function (cb) {
92-
rlpxs[0].once('peer:added', function () {
93-
// FIXME: values not as expected
94-
// t.equal(rlpxs[0]._peersQueue.length, 1, 'peers queue should contain one peer')
95-
cb(null)
96-
})
97-
},
98-
],
99-
function (err) {
100-
if (err) {
101-
t.fail('An unexpected error occured.')
68+
try {
69+
rlpxs[0].on('peer:added', async (peer) => {
70+
if (peer._socket._peername.port === util.basePort + 1) {
71+
t.equal(rlpxs[0]._peersQueue.length, 0, 'peers queue should contain no peers')
72+
const peer2 = {
73+
address: util.localhost,
74+
udpPort: util.basePort + 2,
75+
tcpPort: util.basePort + 2,
76+
}
77+
rlpxs[0]._dpt!.addPeer(peer2)
78+
await util.delay(500)
79+
t.equal(rlpxs[0]._peersQueue.length, 1, 'peers queue should contain one peer')
10280
}
103-
util.destroyRLPXs(rlpxs)
104-
t.end()
105-
}
106-
)
81+
if (peer._socket._peername.port === util.basePort + 2) {
82+
t.equal(rlpxs[0]._peersQueue.length, 0, 'peers queue should contain no peers')
83+
util.destroyRLPXs(rlpxs)
84+
t.end()
85+
}
86+
})
87+
} catch (err) {
88+
t.fail(`An unexpected error occurred: ${err}`)
89+
}
10790
})

0 commit comments

Comments
 (0)