@@ -24,124 +24,126 @@ const switchOptions = {
24
24
}
25
25
26
26
describe ( `circuit` , function ( ) {
27
- let swarmA // TCP and WS
28
- let swarmB // WS
29
- let swarmC // no transports
30
- let dialSpyA
27
+ describe ( 'basic' , ( ) => {
28
+ let swarmA // TCP and WS
29
+ let swarmB // WS
30
+ let swarmC // no transports
31
+ let dialSpyA
31
32
32
- before ( ( done ) => createInfos ( 3 , ( err , infos ) => {
33
- expect ( err ) . to . not . exist ( )
33
+ before ( ( done ) => createInfos ( 3 , ( err , infos ) => {
34
+ expect ( err ) . to . not . exist ( )
34
35
35
- const peerA = infos [ 0 ]
36
- const peerB = infos [ 1 ]
37
- const peerC = infos [ 2 ]
36
+ const peerA = infos [ 0 ]
37
+ const peerB = infos [ 1 ]
38
+ const peerC = infos [ 2 ]
38
39
39
- peerA . multiaddrs . add ( '/ip4/0.0.0.0/tcp/9001' )
40
- peerB . multiaddrs . add ( '/ip4/127.0.0.1/tcp/9002/ws' )
40
+ peerA . multiaddrs . add ( '/ip4/0.0.0.0/tcp/9001' )
41
+ peerB . multiaddrs . add ( '/ip4/127.0.0.1/tcp/9002/ws' )
41
42
42
- swarmA = new Swarm ( peerA , new PeerBook ( ) , switchOptions )
43
- swarmB = new Swarm ( peerB , new PeerBook ( ) )
44
- swarmC = new Swarm ( peerC , new PeerBook ( ) )
43
+ swarmA = new Swarm ( peerA , new PeerBook ( ) , switchOptions )
44
+ swarmB = new Swarm ( peerB , new PeerBook ( ) )
45
+ swarmC = new Swarm ( peerC , new PeerBook ( ) )
45
46
46
- swarmA . transport . add ( 'tcp' , new TCP ( ) )
47
- swarmA . transport . add ( 'ws' , new WS ( ) )
48
- swarmB . transport . add ( 'ws' , new WS ( ) )
47
+ swarmA . transport . add ( 'tcp' , new TCP ( ) )
48
+ swarmA . transport . add ( 'ws' , new WS ( ) )
49
+ swarmB . transport . add ( 'ws' , new WS ( ) )
49
50
50
- dialSpyA = sinon . spy ( swarmA . transport , 'dial' )
51
+ dialSpyA = sinon . spy ( swarmA . transport , 'dial' )
51
52
52
- done ( )
53
- } ) )
53
+ done ( )
54
+ } ) )
54
55
55
- after ( ( done ) => {
56
- parallel ( [
57
- ( cb ) => swarmA . stop ( cb ) ,
58
- ( cb ) => swarmB . stop ( cb )
59
- ] , done )
60
- } )
56
+ after ( ( done ) => {
57
+ parallel ( [
58
+ ( cb ) => swarmA . stop ( cb ) ,
59
+ ( cb ) => swarmB . stop ( cb )
60
+ ] , done )
61
+ } )
61
62
62
- it ( 'circuit not enabled and all transports failed' , ( done ) => {
63
- swarmA . dial ( swarmC . _peerInfo , ( err , conn ) => {
64
- expect ( err ) . to . exist ( )
65
- expect ( err ) . to . match ( / C i r c u i t n o t e n a b l e d a n d a l l t r a n s p o r t s f a i l e d t o d i a l p e e r / )
66
- expect ( conn ) . to . not . exist ( )
67
- done ( )
63
+ it ( 'circuit not enabled and all transports failed' , ( done ) => {
64
+ swarmA . dial ( swarmC . _peerInfo , ( err , conn ) => {
65
+ expect ( err ) . to . exist ( )
66
+ expect ( err ) . to . match ( / C i r c u i t n o t e n a b l e d a n d a l l t r a n s p o r t s f a i l e d t o d i a l p e e r / )
67
+ expect ( conn ) . to . not . exist ( )
68
+ done ( )
69
+ } )
68
70
} )
69
- } )
70
71
71
- it ( '.enableCircuitRelay' , ( ) => {
72
- swarmA . connection . enableCircuitRelay ( { enabled : true } )
73
- expect ( Object . keys ( swarmA . transports ) . length ) . to . equal ( 3 )
72
+ it ( '.enableCircuitRelay' , ( ) => {
73
+ swarmA . connection . enableCircuitRelay ( { enabled : true } )
74
+ expect ( Object . keys ( swarmA . transports ) . length ) . to . equal ( 3 )
74
75
75
- swarmB . connection . enableCircuitRelay ( { enabled : true } )
76
- expect ( Object . keys ( swarmB . transports ) . length ) . to . equal ( 2 )
77
- } )
76
+ swarmB . connection . enableCircuitRelay ( { enabled : true } )
77
+ expect ( Object . keys ( swarmB . transports ) . length ) . to . equal ( 2 )
78
+ } )
78
79
79
- it ( 'listed on the transports map' , ( ) => {
80
- expect ( swarmA . transports . Circuit ) . to . exist ( )
81
- expect ( swarmB . transports . Circuit ) . to . exist ( )
82
- } )
80
+ it ( 'listed on the transports map' , ( ) => {
81
+ expect ( swarmA . transports . Circuit ) . to . exist ( )
82
+ expect ( swarmB . transports . Circuit ) . to . exist ( )
83
+ } )
83
84
84
- it ( 'add /p2p-circuit addrs on start' , ( done ) => {
85
- parallel ( [
86
- ( cb ) => swarmA . start ( cb ) ,
87
- ( cb ) => swarmB . start ( cb )
88
- ] , ( err ) => {
89
- expect ( err ) . to . not . exist ( )
90
- expect ( swarmA . _peerInfo . multiaddrs . toArray ( ) . filter ( ( a ) => a . toString ( )
91
- . includes ( `/p2p-circuit` ) ) . length ) . to . be . at . least ( 3 )
92
- // ensure swarmA has had 0.0.0.0 replaced in the addresses
93
- expect ( swarmA . _peerInfo . multiaddrs . toArray ( ) . filter ( ( a ) => a . toString ( )
94
- . includes ( `/0.0.0.0` ) ) . length ) . to . equal ( 0 )
95
- expect ( swarmB . _peerInfo . multiaddrs . toArray ( ) . filter ( ( a ) => a . toString ( )
96
- . includes ( `/p2p-circuit` ) ) . length ) . to . be . at . least ( 2 )
97
- done ( )
85
+ it ( 'add /p2p-circuit addrs on start' , ( done ) => {
86
+ parallel ( [
87
+ ( cb ) => swarmA . start ( cb ) ,
88
+ ( cb ) => swarmB . start ( cb )
89
+ ] , ( err ) => {
90
+ expect ( err ) . to . not . exist ( )
91
+ expect ( swarmA . _peerInfo . multiaddrs . toArray ( ) . filter ( ( a ) => a . toString ( )
92
+ . includes ( `/p2p-circuit` ) ) . length ) . to . be . at . least ( 3 )
93
+ // ensure swarmA has had 0.0.0.0 replaced in the addresses
94
+ expect ( swarmA . _peerInfo . multiaddrs . toArray ( ) . filter ( ( a ) => a . toString ( )
95
+ . includes ( `/0.0.0.0` ) ) . length ) . to . equal ( 0 )
96
+ expect ( swarmB . _peerInfo . multiaddrs . toArray ( ) . filter ( ( a ) => a . toString ( )
97
+ . includes ( `/p2p-circuit` ) ) . length ) . to . be . at . least ( 2 )
98
+ done ( )
99
+ } )
98
100
} )
99
- } )
100
101
101
- it ( 'dial circuit only once' , ( done ) => {
102
- swarmA . _peerInfo . multiaddrs . clear ( )
103
- swarmA . _peerInfo . multiaddrs
104
- . add ( `/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star` )
102
+ it ( 'dial circuit only once' , ( done ) => {
103
+ swarmA . _peerInfo . multiaddrs . clear ( )
104
+ swarmA . _peerInfo . multiaddrs
105
+ . add ( `/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star` )
105
106
106
- swarmA . dial ( swarmC . _peerInfo , ( err , conn ) => {
107
- expect ( err ) . to . exist ( )
108
- expect ( err ) . to . match ( / N o a v a i l a b l e t r a n s p o r t s t o d i a l p e e r / )
109
- expect ( conn ) . to . not . exist ( )
110
- expect ( dialSpyA . callCount ) . to . be . eql ( 1 )
111
- done ( )
107
+ swarmA . dial ( swarmC . _peerInfo , ( err , conn ) => {
108
+ expect ( err ) . to . exist ( )
109
+ expect ( err ) . to . match ( / N o a v a i l a b l e t r a n s p o r t s t o d i a l p e e r / )
110
+ expect ( conn ) . to . not . exist ( )
111
+ expect ( dialSpyA . callCount ) . to . be . eql ( 1 )
112
+ done ( )
113
+ } )
112
114
} )
113
- } )
114
115
115
- it ( 'dial circuit last' , ( done ) => {
116
- const peerC = swarmC . _peerInfo
117
- peerC . multiaddrs . clear ( )
118
- peerC . multiaddrs . add ( `/p2p-circuit/ipfs/ABCD` )
119
- peerC . multiaddrs . add ( `/ip4/127.0.0.1/tcp/9998/ipfs/ABCD` )
120
- peerC . multiaddrs . add ( `/ip4/127.0.0.1/tcp/9999/ws/ipfs/ABCD` )
121
-
122
- swarmA . dial ( peerC , ( err , conn ) => {
123
- expect ( err ) . to . exist ( )
124
- expect ( conn ) . to . not . exist ( )
125
- expect ( dialSpyA . lastCall . args [ 0 ] ) . to . be . eql ( 'Circuit' )
126
- done ( )
116
+ it ( 'dial circuit last' , ( done ) => {
117
+ const peerC = swarmC . _peerInfo
118
+ peerC . multiaddrs . clear ( )
119
+ peerC . multiaddrs . add ( `/p2p-circuit/ipfs/ABCD` )
120
+ peerC . multiaddrs . add ( `/ip4/127.0.0.1/tcp/9998/ipfs/ABCD` )
121
+ peerC . multiaddrs . add ( `/ip4/127.0.0.1/tcp/9999/ws/ipfs/ABCD` )
122
+
123
+ swarmA . dial ( peerC , ( err , conn ) => {
124
+ expect ( err ) . to . exist ( )
125
+ expect ( conn ) . to . not . exist ( )
126
+ expect ( dialSpyA . lastCall . args [ 0 ] ) . to . be . eql ( 'Circuit' )
127
+ done ( )
128
+ } )
127
129
} )
128
- } )
129
130
130
- it ( 'should not try circuit if no transports enabled' , ( done ) => {
131
- swarmC . dial ( swarmA . _peerInfo , ( err , conn ) => {
132
- expect ( err ) . to . exist ( )
133
- expect ( conn ) . to . not . exist ( )
131
+ it ( 'should not try circuit if no transports enabled' , ( done ) => {
132
+ swarmC . dial ( swarmA . _peerInfo , ( err , conn ) => {
133
+ expect ( err ) . to . exist ( )
134
+ expect ( conn ) . to . not . exist ( )
134
135
135
- expect ( err ) . to . match ( / N o t r a n s p o r t s r e g i s t e r e d , d i a l n o t p o s s i b l e / )
136
- done ( )
136
+ expect ( err ) . to . match ( / N o t r a n s p o r t s r e g i s t e r e d , d i a l n o t p o s s i b l e / )
137
+ done ( )
138
+ } )
137
139
} )
138
- } )
139
140
140
- it ( 'should not dial circuit if other transport succeed' , ( done ) => {
141
- swarmA . dial ( swarmB . _peerInfo , ( err ) => {
142
- expect ( err ) . not . to . exist ( )
143
- expect ( dialSpyA . lastCall . args [ 0 ] ) . to . not . be . eql ( 'Circuit' )
144
- done ( )
141
+ it ( 'should not dial circuit if other transport succeed' , ( done ) => {
142
+ swarmA . dial ( swarmB . _peerInfo , ( err ) => {
143
+ expect ( err ) . not . to . exist ( )
144
+ expect ( dialSpyA . lastCall . args [ 0 ] ) . to . not . be . eql ( 'Circuit' )
145
+ done ( )
146
+ } )
145
147
} )
146
148
} )
147
149
0 commit comments