@@ -30,42 +30,57 @@ export function contentFetchingTests (factory: DaemonFactory): void {
30
30
31
31
function runContentFetchingTests ( factory : DaemonFactory , optionsA : SpawnOptions , optionsB : SpawnOptions ) : void {
32
32
describe ( 'dht.contentFetching' , ( ) => {
33
- let daemonA : Daemon
34
- let daemonB : Daemon
35
- let daemonC : Daemon
36
- let daemonD : Daemon
33
+ let nodes : Daemon [ ]
37
34
38
35
// Start Daemons
39
36
before ( async function ( ) {
40
37
this . timeout ( 20 * 1000 )
41
38
42
- daemonA = await factory . spawn ( optionsA )
43
- daemonB = await factory . spawn ( optionsA )
44
- daemonC = await factory . spawn ( optionsB )
45
- daemonD = await factory . spawn ( optionsB )
39
+ nodes = await Promise . all ( [
40
+ factory . spawn ( optionsA ) ,
41
+ ... new Array ( 3 ) . fill ( 0 ) . map ( async ( ) => factory . spawn ( optionsB ) )
42
+ ] )
46
43
47
- const identifyA = await daemonA . client . identify ( )
48
- const identifyB = await daemonB . client . identify ( )
49
- const identifyC = await daemonC . client . identify ( )
50
- const identifyD = await daemonD . client . identify ( )
44
+ const identify = await Promise . all (
45
+ nodes . map ( async node => node . client . identify ( ) )
46
+ )
47
+
48
+ // connect them all
49
+ for ( let i = 0 ; i < nodes . length ; i ++ ) {
50
+ for ( let k = 0 ; k < nodes . length ; k ++ ) {
51
+ if ( i === k ) {
52
+ continue
53
+ }
54
+
55
+ const a = nodes [ i ]
56
+ const b = identify [ k ]
51
57
52
- // connect them A -> B -> C -> D
53
- await daemonA . client . connect ( identifyB . peerId , identifyB . addrs )
54
- await daemonB . client . connect ( identifyC . peerId , identifyC . addrs )
55
- await daemonC . client . connect ( identifyD . peerId , identifyD . addrs )
58
+ await a . client . connect ( b . peerId , b . addrs )
59
+ }
60
+ }
56
61
57
62
// wait for identify
58
63
await delay ( 1000 )
59
64
60
- // B can find D and C can find A, so their routing tables are not empty
61
- await expect ( daemonB . client . dht . findPeer ( identifyD . peerId ) ) . to . eventually . be . ok ( )
62
- await expect ( daemonC . client . dht . findPeer ( identifyA . peerId ) ) . to . eventually . be . ok ( )
65
+ // ensure they can all find each other
66
+ for ( let i = 0 ; i < nodes . length ; i ++ ) {
67
+ for ( let k = 0 ; k < nodes . length ; k ++ ) {
68
+ if ( i === k ) {
69
+ continue
70
+ }
71
+
72
+ const a = nodes [ i ]
73
+ const b = identify [ k ]
74
+
75
+ await expect ( a . client . dht . findPeer ( b . peerId ) ) . to . eventually . be . ok ( )
76
+ }
77
+ }
63
78
} )
64
79
65
80
// Stop daemons
66
81
after ( async function ( ) {
67
82
await Promise . all (
68
- [ daemonA , daemonB , daemonC , daemonD ]
83
+ nodes
69
84
. filter ( Boolean )
70
85
. map ( async d => { await d . stop ( ) } )
71
86
)
@@ -74,9 +89,9 @@ function runContentFetchingTests (factory: DaemonFactory, optionsA: SpawnOptions
74
89
it ( `${ optionsA . type } peer to ${ optionsB . type } peer` , async function ( ) {
75
90
this . timeout ( 10 * 1000 )
76
91
77
- await daemonB . client . dht . put ( record . key , record . value )
92
+ await nodes [ 0 ] . client . dht . put ( record . key , record . value )
78
93
79
- const data = await daemonC . client . dht . get ( record . key )
94
+ const data = await nodes [ 1 ] . client . dht . get ( record . key )
80
95
expect ( data ) . to . equalBytes ( record . value )
81
96
} )
82
97
} )
0 commit comments