@@ -24,27 +24,13 @@ const config = {
2424}
2525
2626describe . only ( 'ping' , function ( ) {
27- this . timeout ( 80 * 1000 )
28-
27+ this . timeout ( 60 * 1000 )
2928 let ipfsdA
3029 let ipfsdB
30+ let bMultiaddr
3131 let ipfsdBId
3232 let cli
3333
34- before ( function ( done ) {
35- this . timeout ( 60 * 1000 )
36-
37- df . spawn ( {
38- exec : './src/cli/bin.js' ,
39- config,
40- initoptions : { bits : 512 }
41- } , ( err , _ipfsd ) => {
42- expect ( err ) . to . not . exist ( )
43- ipfsdA = _ipfsd
44- done ( )
45- } )
46- } )
47-
4834 before ( ( done ) => {
4935 this . timeout ( 60 * 1000 )
5036 series ( [
@@ -62,38 +48,102 @@ describe.only('ping', function () {
6248 ( cb ) => {
6349 ipfsdB . api . id ( ( err , peerInfo ) => {
6450 expect ( err ) . to . not . exist ( )
65- console . log ( peerInfo )
6651 ipfsdBId = peerInfo . id
52+ bMultiaddr = peerInfo . addresses [ 0 ]
6753 cb ( )
6854 } )
6955 }
7056 ] , done )
7157 } )
7258
73- after ( ( done ) => ipfsdA . stop ( done ) )
74- after ( ( done ) => ipfsdB . stop ( done ) )
59+ before ( function ( done ) {
60+ this . timeout ( 60 * 1000 )
61+
62+ df . spawn ( {
63+ exec : './src/cli/bin.js' ,
64+ config,
65+ initoptions : { bits : 512 }
66+ } , ( err , _ipfsd ) => {
67+ expect ( err ) . to . not . exist ( )
68+ ipfsdA = _ipfsd
69+ ipfsdA . api . swarm . connect ( bMultiaddr , done )
70+ } )
71+ } )
7572
7673 before ( ( done ) => {
74+ this . timeout ( 60 * 1000 )
7775 cli = ipfsExec ( ipfsdA . repoPath )
7876 done ( )
7977 } )
8078
79+ after ( ( done ) => ipfsdA . stop ( done ) )
80+ after ( ( done ) => ipfsdB . stop ( done ) )
81+
8182 it ( 'ping host' , ( done ) => {
83+ this . timeout ( 60 * 1000 )
8284 const ping = cli ( `ping ${ ipfsdBId } ` )
8385 const result = [ ]
84- ping . stdout . on ( 'data' , ( packet ) => {
85- console . log ( 'ON DATA' )
86- result . push ( packet . toString ( ) )
86+ ping . stdout . on ( 'data' , ( output ) => {
87+ const packets = output . toString ( ) . split ( '\n' ) . slice ( 0 , - 1 )
88+ result . push ( ... packets )
8789 } )
8890
89- ping . stdout . on ( 'end' , ( c ) => {
90- console . log ( 'END' , result )
91+ ping . stdout . on ( 'end' , ( ) => {
92+ expect ( result ) . to . have . lengthOf ( 12 )
93+ expect ( result [ 0 ] ) . to . equal ( `PING ${ ipfsdBId } ` )
94+ for ( let i = 1 ; i < 11 ; i ++ ) {
95+ expect ( result [ i ] ) . to . match ( / ^ P o n g r e c e i v e d : t i m e = \d + m s $ / )
96+ }
97+ expect ( result [ 11 ] ) . to . match ( / ^ A v e r a g e l a t e n c y : \d + ( .\d + ) ? m s $ / )
9198 done ( )
9299 } )
93100
94101 ping . catch ( ( err ) => {
95102 expect ( err ) . to . not . exist ( )
103+ } )
104+ } )
105+
106+ it ( 'ping host with --n option' , ( done ) => {
107+ this . timeout ( 60 * 1000 )
108+ const ping = cli ( `ping --n 1 ${ ipfsdBId } ` )
109+ const result = [ ]
110+ ping . stdout . on ( 'data' , ( output ) => {
111+ const packets = output . toString ( ) . split ( '\n' ) . slice ( 0 , - 1 )
112+ result . push ( ...packets )
113+ } )
114+
115+ ping . stdout . on ( 'end' , ( ) => {
116+ expect ( result ) . to . have . lengthOf ( 3 )
117+ expect ( result [ 0 ] ) . to . equal ( `PING ${ ipfsdBId } ` )
118+ expect ( result [ 1 ] ) . to . match ( / ^ P o n g r e c e i v e d : t i m e = \d + m s $ / )
119+ expect ( result [ 2 ] ) . to . match ( / ^ A v e r a g e l a t e n c y : \d + ( .\d + ) ? m s $ / )
96120 done ( )
97121 } )
122+
123+ ping . catch ( ( err ) => {
124+ expect ( err ) . to . not . exist ( )
125+ } )
126+ } )
127+
128+ it ( 'ping host with --count option' , ( done ) => {
129+ this . timeout ( 60 * 1000 )
130+ const ping = cli ( `ping --count 1 ${ ipfsdBId } ` )
131+ const result = [ ]
132+ ping . stdout . on ( 'data' , ( output ) => {
133+ const packets = output . toString ( ) . split ( '\n' ) . slice ( 0 , - 1 )
134+ result . push ( ...packets )
135+ } )
136+
137+ ping . stdout . on ( 'end' , ( ) => {
138+ expect ( result ) . to . have . lengthOf ( 3 )
139+ expect ( result [ 0 ] ) . to . equal ( `PING ${ ipfsdBId } ` )
140+ expect ( result [ 1 ] ) . to . match ( / ^ P o n g r e c e i v e d : t i m e = \d + m s $ / )
141+ expect ( result [ 2 ] ) . to . match ( / ^ A v e r a g e l a t e n c y : \d + ( .\d + ) ? m s $ / )
142+ done ( )
143+ } )
144+
145+ ping . catch ( ( err ) => {
146+ expect ( err ) . to . not . exist ( )
147+ } )
98148 } )
99149} )
0 commit comments