Skip to content

Commit

Permalink
client: migrate tests to vite (#2797)
Browse files Browse the repository at this point in the history
* client: migrate tests to vite

* client: update test scripts

* client: fix rpc engine tests

* client: fix rpc engine tests

* client: remove unnecessary stringification

* client: fix more rpc tests

* client: misc test fixes

* client: fix ci script still using tape

* Rename libp2p tests to avoid vitest running them

* fix instanceof tests

* Update vitest.config so that test:CLI runs

* Update vitest.config so that test:CLI runs

* Fixes for client.spec.ts

* Fix error in sender.spec.ts

* Fix test formatting rlpxserver.spec.ts

* Update unit test config, randomize rpc port

* test fixes

* more test fixes

* Fix engine tests

* Partial test fixes

* Fix merge integration test

* fix fcu hex handling

* Add timeouts and fix lightsync

* Various test and type fixes

* fix txpool tests

* correct bytes2hex import

* Fix lightethereumservice tests

* client: fix lesprotocol test

* Fix most full ethereum service tests

* Fix fullethereumservice test

* "Fix" flow control test

* Fix rlxppeer test

* client: fix lightsync integration test timeouts

* client: update client ci

* client: increase timeout for some tests

* client: remove only from flowcontrol test

* client: more test fixes

* client: increase timeout for miner

* client: increase timeout for miner

* client: increase more timeouts and fix missing it statement

* fix integration tests

* fix lint rules

* fix npm script

* Fix lint file extension

* Fix lint config, again

* File path fix

---------

Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>
Co-authored-by: ScottyPoi <scott.simpson@ethereum.org>
Co-authored-by: Scotty <66335769+ScottyPoi@users.noreply.github.com>
Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com>
  • Loading branch information
5 people authored Jul 18, 2023
1 parent 6b23892 commit 8946609
Show file tree
Hide file tree
Showing 118 changed files with 7,436 additions and 7,357 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/client-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ jobs:
with:
timeout_minutes: 10
max_attempts: 3
command: cd ${{github.workspace}}/packages/client && npm run tape -- 'test/integration/cli.spec.ts'
command: cd ${{github.workspace}}/packages/client && npx vitest run test/integration/cli.spec.ts
1 change: 1 addition & 0 deletions config/eslint.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
'webpack.config.js',
'vitest.config.ts',
'vitest.config.browser.ts',
'vitest.config.unit.ts'
],
extends: [
'typestrict',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { spawn } from 'child_process'
import * as tape from 'tape'
import { assert, describe, it } from 'vitest'

import type { ChildProcessWithoutNullStreams } from 'child_process'

const end = (child: ChildProcessWithoutNullStreams, hasEnded: boolean, st: tape.Test) => {
const end = (child: ChildProcessWithoutNullStreams, hasEnded: boolean) => {
if (hasEnded) return
hasEnded = true
child.stdout.removeAllListeners()
child.stderr.removeAllListeners()
const res = child.kill('SIGINT')
st.ok(res, 'client shut down successfully')
st.end()
assert.ok(res, 'client shut down successfully')
}

tape('[CLI] rpc', (t) => {
t.test('libp2p should start up', (st) => {
describe('[CLI] rpc', () => {
it('libp2p should start up', () => {
const file = require.resolve('../../dist/bin/cli.js')
const child = spawn(process.execPath, [
file,
Expand All @@ -32,7 +31,7 @@ tape('[CLI] rpc', (t) => {
const message: string = data.toString()

if (message.includes('transport=libp2p')) {
st.pass('libp2p server started')
assert.ok(true, 'libp2p server started')
const bootnodeAddressArray = message.split(' ')
const bootnodeAddressIndex = bootnodeAddressArray.findIndex((chunk: string) =>
chunk.startsWith('url=')
Expand All @@ -54,25 +53,25 @@ tape('[CLI] rpc', (t) => {
child2.stdout.on('data', async (data) => {
const message: string = data.toString()
if (message.includes('Peer added')) {
st.pass('connected to peer over libp2p')
assert.ok(true, 'connected to peer over libp2p')
child2.kill('SIGINT')
child2.stdout.removeAllListeners()
end(child, false, st)
end(child, false)
}
})
}
})

child.stderr.on('data', (data) => {
const message: string = data.toString()
st.fail(`stderr: ${message}`)
end(child, hasEnded, st)
assert.fail(`stderr: ${message}`)
end(child, hasEnded)
})

child.on('close', (code) => {
if (typeof code === 'number' && code > 0) {
st.fail(`child process exited with code ${code}`)
end(child, hasEnded, st)
assert.fail(`child process exited with code ${code}`)
end(child, hasEnded)
}
})
})
Expand Down
19 changes: 0 additions & 19 deletions packages/client/libp2pBrowserBuild/net/test/libp2pnode.spec.ts

This file was deleted.

17 changes: 17 additions & 0 deletions packages/client/libp2pBrowserBuild/net/test/libp2pnode.spec.ts.old
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { assert, describe, it } from 'vitest'
import * as td from 'testdouble'

describe('[Libp2pNode]', async () => {
td.replace('libp2p')
const { Libp2pNode } = await import('../peer/libp2pnode')

it('should be a libp2p bundle', () => {
const peerId = td.object('PeerId') as any
const node = new Libp2pNode({ peerId })
assert.equal(node.constructor.name, Libp2pNode.name, 'is libp2p bundle')
})

it('should reset td', () => {
td.reset()
})
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { multiaddr } from 'multiaddr'
import * as tape from 'tape'
import { assert, describe, it } from 'vitest'
import * as td from 'testdouble'

import { Config } from '../../../src/config'
Expand All @@ -8,7 +8,7 @@ import { Event } from '../../../src/types'
import type { Libp2pPeer } from '../../../src/net/peer'
import type { Protocol } from '../../../src/net/protocol'

tape('[Libp2pPeer]', async (t) => {
describe('[Libp2pPeer]', async () => {
td.replace('peer-id')

const Libp2pNode = td.constructor(['start', 'stop', 'dial', 'dialProtocol'] as any)
Expand All @@ -20,39 +20,40 @@ tape('[Libp2pPeer]', async (t) => {

const { Libp2pPeer } = await import('../peer/libp2ppeer')

t.test('should initialize correctly', async (t) => {
it('should initialize correctly', async () => {
const config = new Config()
const multiaddrs = [
multiaddr('/ip4/192.0.2.1/tcp/12345'),
multiaddr('/ip4/192.0.2.1/tcp/23456'),
]
const peer = new Libp2pPeer({ config, multiaddrs })
t.equals(peer.address, '/ip4/192.0.2.1/tcp/12345,/ip4/192.0.2.1/tcp/23456', 'address correct')
t.end()
assert.equal(
peer.address,
'/ip4/192.0.2.1/tcp/12345,/ip4/192.0.2.1/tcp/23456',
'address correct'
)
})

t.test('should connect to peer', async (t) => {
it('should connect to peer', async () => {
const config = new Config()
const peer = new Libp2pPeer({ config })
config.events.on(Event.PEER_CONNECTED, (peer) => {
t.equals((peer as Libp2pPeer).address, '/ip4/0.0.0.0/tcp/0', 'connected')
t.end()
assert.equal((peer as Libp2pPeer).address, '/ip4/0.0.0.0/tcp/0', 'connected')
})
await peer.connect()
})

t.test('should accept peer connection', async (t) => {
it('should accept peer connection', async () => {
const config = new Config()
const peer: any = new Libp2pPeer({ config })
peer.bindProtocol = td.func<typeof peer['bindProtocol']>()
td.when(peer.bindProtocol('proto' as any, 'conn' as any)).thenResolve(null)
await peer.accept('proto', 'conn', 'server')
t.equals(peer.server, 'server', 'server set')
t.ok(peer.inbound, 'inbound set to true')
t.end()
assert.equal(peer.server, 'server', 'server set')
assert.ok(peer.inbound, 'inbound set to true')
})

t.test('should bind protocols', async (t) => {
it('should bind protocols', async () => {
const config = new Config()
const protocol = { name: 'proto', versions: [1], open: () => {} } as Protocol
const badProto = { name: 'bad', versions: [1], open: () => {} } as Protocol
Expand All @@ -66,13 +67,11 @@ tape('[Libp2pPeer]', async (t) => {
td.when(node.dialProtocol(td.matchers.anything(), '/proto/1')).thenResolve(null)
td.when(node.dialProtocol(td.matchers.anything(), '/bad/1')).thenReject(new Error('bad'))
await peer.bindProtocols(node, td.matchers.anything(), 'server')
t.equals(peer.server, 'server', 'server set')
t.ok((peer as any).connected, 'connected set to true')
t.end()
assert.equal(peer.server, 'server', 'server set')
assert.ok((peer as any).connected, 'connected set to true')
})

t.test('should reset td', (t) => {
it('should reset td', () => {
td.reset()
t.end()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,37 @@ import { Libp2pSender } from '../../../src/net/protocol'

const DuplexPair = require('it-pair/duplex')

tape('[Libp2pSender]', (t) => {
t.test('should send/receive status', (t) => {
describe('[Libp2pSender]', () => {
it('should send/receive status', () => {
const conn = DuplexPair()
const sender = new Libp2pSender(conn[0])
const receiver = new Libp2pSender(conn[1])
receiver.on('status', (status: any) => {
t.equal(bytesToHex(status.id), '05', 'status received')
t.equal(bytesToHex(receiver.status.id), '05', 'status getter')
t.end()
assert.equal(bytesToHex(status.id), '05', 'status received')
assert.equal(bytesToHex(receiver.status.id), '05', 'status getter')
})
sender.sendStatus({ id: hexToBytes('0x05') })
})

t.test('should send/receive message', (t) => {
it('should send/receive message', () => {
const conn = DuplexPair()
const sender = new Libp2pSender(conn[0])
const receiver = new Libp2pSender(conn[1])
receiver.on('message', (message: any) => {
t.equal(message.code, 1, 'message received (code)')
t.equal(bytesToHex(message.payload), '05', 'message received (payload)')
t.end()
assert.equal(message.code, 1, 'message received (code)')
assert.equal(bytesToHex(message.payload), '05', 'message received (payload)')
})
sender.sendMessage(1, hexToBytes('0x05'))
})

t.test('should catch errors', (t) => {
it('should catch errors', () => {
const [conn] = DuplexPair()
const err0 = {
...conn,
sink: () => {
throw new Error('err0')
},
} as any
t.throws(() => new Libp2pSender(err0), /err0/, 'catch error')
t.end()
assert.throws(() => new Libp2pSender(err0), /err0/, 'catch error')
})
})
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { bytesToUtf8, utf8ToBytes } from '@ethereumjs/util'
import { EventEmitter } from 'events'
import { multiaddr } from 'multiaddr'
import * as tape from 'tape'
import { assert, describe, it } from 'vitest'
import * as td from 'testdouble'

import { Config } from '../../../src/config'
import { getLogger } from '../../../src/logging'
import { Event } from '../../../src/types'
import { wait } from '../../../test/integration/util'

tape('[Libp2pServer]', async (t) => {
describe('[Libp2pServer]', async () => {
const Libp2pPeer = td.replace<any>('../../../src/net/peer/libp2ppeer')
Libp2pPeer.id = 'id0'

Expand Down Expand Up @@ -41,7 +41,7 @@ tape('[Libp2pServer]', async (t) => {

const { Libp2pServer } = await import('../server/libp2pserver')

t.test('should initialize correctly', async (t) => {
it('should initialize correctly', async () => {
const config = new Config({ transports: [], accountCache: 10000, storageCache: 1000 })
const multiaddrs = [
multiaddr('/ip4/192.0.2.1/tcp/12345'),
Expand All @@ -53,32 +53,30 @@ tape('[Libp2pServer]', async (t) => {
bootnodes: ['0.0.0.0:3030', '1.1.1.1:3031'],
key: utf8ToBytes('abcd'),
})
t.deepEquals((server as any).multiaddrs, multiaddrs, 'multiaddrs correct')
t.deepEquals(
assert.deepEqual((server as any).multiaddrs, multiaddrs, 'multiaddrs correct')
assert.deepEqual(
server.bootnodes,
[multiaddr('/ip4/0.0.0.0/tcp/3030'), multiaddr('/ip4/1.1.1.1/tcp/3031')],
'bootnodes split'
)
t.equals(bytesToUtf8(server.key!), 'abcd', 'key is correct')
t.equals(server.name, 'libp2p', 'get name')
t.equals(
assert.equal(bytesToUtf8(server.key!), 'abcd', 'key is correct')
assert.equal(server.name, 'libp2p', 'get name')
assert.equal(
(await server.getPeerId()).toB58String(),
'12D3KooWHnPxZvSVGxToTNaK1xd9z3J1TkQM2S2hLeX4bhraGE64',
'computes correct peerId'
)
t.end()
})

t.test('should get peer info', async (t) => {
it('should get peer info', async () => {
const config = new Config({ transports: [], accountCache: 10000, storageCache: 1000 })
const server = new Libp2pServer({ config })
const connection = td.object<any>()
connection.remotePeer = 'id0'
t.equals(server.getPeerInfo(connection)[0], 'id0', 'got id')
t.end()
assert.equal(server.getPeerInfo(connection)[0], 'id0', 'got id')
})

t.test('should create peer', async (t) => {
it('should create peer', async () => {
const config = new Config({ transports: [], accountCache: 10000, storageCache: 1000 })
const multiaddrs = [multiaddr('/ip4/6.6.6.6')]
const server = new Libp2pServer({ config, multiaddrs })
Expand All @@ -88,13 +86,11 @@ tape('[Libp2pServer]', async (t) => {
},
} as any
const peer = server.createPeer(peerId, [])
t.equals(peer.constructor.name, 'Libp2pPeer', 'created peer')
t.equals((server as any).peers.get(peer.id), peer, 'has peer')
t.end()
assert.equal(peer.constructor.name, 'Libp2pPeer', 'created peer')
assert.equal((server as any).peers.get(peer.id), peer, 'has peer')
})

t.test('should start/stop server and test banning', async (t) => {
t.plan(12)
it('should start/stop server and test banning', async () => {
const config = new Config({
transports: [],
logger: getLogger({ loglevel: 'off' }),
Expand Down Expand Up @@ -139,35 +135,34 @@ tape('[Libp2pServer]', async (t) => {
;(server as any).peers.set('id', peer)
server.addProtocols(protos)
config.events.on(Event.SERVER_LISTENING, (info) =>
t.deepEquals(info, { transport: 'libp2p', url: 'ma0/p2p/id' }, 'listening')
assert.deepEqual(info, { transport: 'libp2p', url: 'ma0/p2p/id' }, 'listening')
)
config.events.once(Event.PEER_CONNECTED, (p) => t.equals(p, peer, 'peer connected'))
config.events.on(Event.SERVER_ERROR, (err) => t.equals(err.message, 'err0', 'got err0'))
t.notOk(server.ban('peer'), 'unbannable')
t.notOk(await server.stop(), 'not started')
config.events.once(Event.PEER_CONNECTED, (p) => assert.equal(p, peer, 'peer connected'))
config.events.on(Event.SERVER_ERROR, (err) => assert.equal(err.message, 'err0', 'got err0'))
assert.notOk(server.ban('peer'), 'unbannable')
assert.notOk(await server.stop(), 'not started')
await server.start()
;(server as any).node.emit('error', new Error('err0'))
t.notOk(server.addProtocols([]), 'cannot add protocols after start')
assert.notOk(server.addProtocols([]), 'cannot add protocols after start')
server.ban('peer0', 10)
t.ok(server.isBanned('peer0'), 'banned')
assert.ok(server.isBanned('peer0'), 'banned')
await wait(100)
t.notOk(server.isBanned('peer0'), 'ban expired')
assert.notOk(server.isBanned('peer0'), 'ban expired')
const { node } = server as any
t.equals(node.constructor.name, 'Libp2pNode', 'libp2p node created')
assert.equal(node.constructor.name, 'Libp2pNode', 'libp2p node created')
node.emit('peer:discovery', peerId)
td.when(peer2.bindProtocols(node, 'id2', server)).thenResolve(null)
server.config.events.once(Event.PEER_CONNECTED, () => t.ok('peer2 connected'))
server.config.events.once(Event.PEER_CONNECTED, () => assert.ok('peer2 connected'))
node.emit('peer:discovery', peerId2)
td.when(server.getPeerInfo('conn3' as any)).thenReturn([peerId3, 'ma1' as any])
node.connectionManager.emit('peer:connect', 'conn3')
td.verify(server.createPeer(peerId3, ['ma1'] as any, td.matchers.anything()))
t.ok((await server.start()) === false, 'server already started')
assert.ok((await server.start()) === false, 'server already started')
await server.stop()
t.notOk(server.running, 'stopped')
assert.notOk(server.running, 'stopped')
})

t.test('should reset td', (t) => {
it('should reset td', () => {
td.reset()
t.end()
})
})
Loading

0 comments on commit 8946609

Please sign in to comment.