Skip to content

Commit 738c36e

Browse files
jochem-brouwerholgerd77
authored andcommitted
client: fix tests
1 parent 4f846c0 commit 738c36e

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

packages/client/test/rpc/blockChainStub.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Block } from '@ethereumjs/block'
2+
13
export function blockChain(options: any) {
24
const block = {
35
toJSON: () => ({
@@ -12,5 +14,8 @@ export function blockChain(options: any) {
1214
getBlock: async function (_data: any) {
1315
return Promise.resolve(block)
1416
},
17+
getLatestHeader() {
18+
return Block.fromBlockData().header
19+
},
1520
}
1621
}

packages/client/test/sync/fetcher/blockfetcher.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ tape('[BlockFetcher]', async (t) => {
5858
count: new BN(0),
5959
})
6060
const blocks = [{ header: { number: 1 } }, { header: { number: 2 } }]
61-
t.deepEquals(fetcher.process({ task: { count: 2 } }, { blocks }), blocks, 'got results')
61+
//@ts-ignore
62+
t.deepEquals(fetcher.process({ task: { count: 2 } }, blocks), blocks, 'got results')
63+
//@ts-ignore
6264
t.notOk(fetcher.process({ task: { count: 2 } }, { blocks: [] }), 'bad results')
6365
t.end()
6466
})
@@ -75,7 +77,7 @@ tape('[BlockFetcher]', async (t) => {
7577
count: new BN(0),
7678
})
7779
td.when((fetcher as any).pool.idle(td.matchers.anything())).thenReturn('peer0')
78-
t.equals(fetcher.peer(undefined), 'peer0', 'found peer')
80+
t.equals(fetcher.peer(), 'peer0', 'found peer')
7981
t.end()
8082
})
8183

packages/client/test/sync/fetcher/fetcher.spec.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@ import tape from 'tape-catch'
22
import td from 'testdouble'
33
import { Config } from '../../../lib/config'
44
import { Fetcher } from '../../../lib/sync/fetcher/fetcher'
5+
import { Job } from '../../../lib/sync/fetcher/types'
6+
7+
class FetcherTest extends Fetcher<any, any, any> {
8+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
9+
process(_job: any, res: any) {
10+
return undefined // have to return undefined, otherwise the function return signature is void.
11+
}
12+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
13+
async request(_job: any, peer: any) {
14+
return
15+
}
16+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
17+
async store(store: any) {}
18+
}
519

620
tape('[Fetcher]', (t) => {
721
t.test('should handle bad result', (t) => {
822
t.plan(2)
923
const config = new Config({ loglevel: 'error', transports: [] })
10-
const fetcher = new Fetcher({ config, pool: td.object() })
24+
const fetcher = new FetcherTest({ config, pool: td.object() })
1125
const job: any = { peer: {}, state: 'active' }
1226
;(fetcher as any).running = true
13-
fetcher.next = td.func<Fetcher['next']>()
14-
fetcher.wait = td.func<Fetcher['wait']>()
27+
fetcher.next = td.func<FetcherTest['next']>()
28+
fetcher.wait = td.func<FetcherTest['wait']>()
1529
td.when(fetcher.wait()).thenResolve(undefined)
1630
fetcher.success(job, undefined)
1731
t.equals((fetcher as any).in.size(), 1, 'enqueued job')
@@ -21,27 +35,27 @@ tape('[Fetcher]', (t) => {
2135
t.test('should handle failure', (t) => {
2236
t.plan(2)
2337
const config = new Config({ loglevel: 'error', transports: [] })
24-
const fetcher = new Fetcher({ config, pool: td.object() })
38+
const fetcher = new FetcherTest({ config, pool: td.object() })
2539
const job = { peer: {}, state: 'active' }
2640
;(fetcher as any).running = true
27-
fetcher.next = td.func<Fetcher['next']>()
41+
fetcher.next = td.func<FetcherTest['next']>()
2842
fetcher.on('error', (err: Error) => t.equals(err.message, 'err0', 'got error'))
29-
fetcher.failure(job, new Error('err0'))
43+
fetcher.failure(job as Job<any, any, any>, new Error('err0'))
3044
t.equals((fetcher as any).in.size(), 1, 'enqueued job')
3145
})
3246

3347
t.test('should handle expiration', (t) => {
3448
t.plan(2)
3549
const config = new Config({ loglevel: 'error', transports: [] })
36-
const fetcher = new Fetcher({
50+
const fetcher = new FetcherTest({
3751
config,
3852
pool: td.object(),
3953
timeout: 5,
4054
})
4155
const job = { index: 0 }
4256
const peer = { idle: true }
43-
fetcher.peer = td.func<Fetcher['peer']>()
44-
fetcher.request = td.func<Fetcher['request']>()
57+
fetcher.peer = td.func<FetcherTest['peer']>()
58+
fetcher.request = td.func<FetcherTest['request']>()
4559
td.when(fetcher.peer()).thenReturn(peer)
4660
td.when(fetcher.request(td.matchers.anything(), { idle: false }), { delay: 10 }).thenReject(
4761
new Error('err0')

packages/client/test/sync/fetcher/headerfetcher.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EventEmitter } from 'events'
22
import tape from 'tape-catch'
33
import td from 'testdouble'
44
import { Config } from '../../../lib/config'
5+
import { BN } from 'ethereumjs-util'
56

67
tape('[HeaderFetcher]', async (t) => {
78
class PeerPool extends EventEmitter {
@@ -21,11 +22,13 @@ tape('[HeaderFetcher]', async (t) => {
2122
const fetcher = new HeaderFetcher({ config, pool, flow })
2223
const headers = [{ number: 1 }, { number: 2 }]
2324
t.deepEquals(
24-
fetcher.process({ task: { count: 2 }, peer: 'peer0' }, { headers, bv: 1 }),
25+
//@ts-ignore
26+
fetcher.process({ task: { count: 2 }, peer: 'peer0' }, { headers, bv: new BN(1) }),
2527
headers,
2628
'got results'
2729
)
28-
t.notOk(fetcher.process({ task: { count: 2 } }, { headers: [] }), 'bad results')
30+
//@ts-ignore
31+
t.notOk(fetcher.process({ task: { count: 2 } }, { headers: [], bv: new BN(1) }), 'bad results')
2932
td.verify((fetcher as any).flow.handleReply('peer0', 1))
3033
t.end()
3134
})
@@ -35,6 +38,7 @@ tape('[HeaderFetcher]', async (t) => {
3538
const pool = new PeerPool()
3639
const fetcher = new HeaderFetcher({ config, pool })
3740
td.when((fetcher as any).pool.idle(td.matchers.anything())).thenReturn('peer0')
41+
//@ts-ignore
3842
t.equals(fetcher.peer(undefined), 'peer0', 'found peer')
3943
t.end()
4044
})

packages/client/test/sync/sync.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import tape from 'tape-catch'
33
import td from 'testdouble'
44
import { Config } from '../../lib/config'
55
import { Chain } from '../../lib/blockchain'
6+
import { Synchronizer } from '../../lib/sync/sync'
7+
8+
class SynchronizerTest extends Synchronizer {
9+
async sync() {
10+
return false
11+
}
12+
}
613

714
tape('[Synchronizer]', async (t) => {
815
class PeerPool extends EventEmitter {
@@ -13,13 +20,11 @@ tape('[Synchronizer]', async (t) => {
1320
PeerPool.prototype.close = td.func<any>()
1421
td.replace('../../lib/net/peerpool', { PeerPool })
1522

16-
const { Synchronizer } = await import('../../lib/sync/sync')
17-
1823
t.test('should sync', async (t) => {
1924
const config = new Config({ loglevel: 'error', transports: [] })
2025
const pool = new PeerPool() as any
2126
const chain = new Chain({ config })
22-
const sync = new Synchronizer({ config, pool, chain })
27+
const sync = new SynchronizerTest({ config, pool, chain })
2328
;(sync as any).sync = td.func()
2429
td.when((sync as any).sync()).thenResolve(true)
2530
sync.on('synchronized', async () => {

0 commit comments

Comments
 (0)