@@ -15,7 +15,12 @@ const IPFSApi = require('../src')
15
15
const PingMessageStream = require ( '../src/utils/ping-message-stream' )
16
16
const f = require ( './utils/factory' )
17
17
18
- describe ( '.ping' , function ( ) {
18
+ // Determine if a ping response object is a pong, or something else, like a status message
19
+ function isPong ( pingResponse ) {
20
+ return Boolean ( pingResponse && pingResponse . time )
21
+ }
22
+
23
+ describe . only ( '.ping' , function ( ) {
19
24
let ipfs
20
25
let ipfsd
21
26
let other
@@ -76,7 +81,7 @@ describe('.ping', function () {
76
81
ipfs . ping ( otherId , ( err , res ) => {
77
82
expect ( err ) . to . not . exist ( )
78
83
expect ( res ) . to . be . an ( 'array' )
79
- expect ( res ) . to . have . lengthOf ( 3 )
84
+ expect ( res . filter ( isPong ) ) . to . have . lengthOf ( 1 )
80
85
res . forEach ( packet => {
81
86
expect ( packet ) . to . have . keys ( 'success' , 'time' , 'text' )
82
87
expect ( packet . time ) . to . be . a ( 'number' )
@@ -91,7 +96,7 @@ describe('.ping', function () {
91
96
ipfs . ping ( otherId , { count : 2 } , ( err , res ) => {
92
97
expect ( err ) . to . not . exist ( )
93
98
expect ( res ) . to . be . an ( 'array' )
94
- expect ( res ) . to . have . lengthOf ( 4 )
99
+ expect ( res . filter ( isPong ) ) . to . have . lengthOf ( 2 )
95
100
res . forEach ( packet => {
96
101
expect ( packet ) . to . have . keys ( 'success' , 'time' , 'text' )
97
102
expect ( packet . time ) . to . be . a ( 'number' )
@@ -106,7 +111,7 @@ describe('.ping', function () {
106
111
ipfs . ping ( otherId , { n : 2 } , ( err , res ) => {
107
112
expect ( err ) . to . not . exist ( )
108
113
expect ( res ) . to . be . an ( 'array' )
109
- expect ( res ) . to . have . lengthOf ( 4 )
114
+ expect ( res . filter ( isPong ) ) . to . have . lengthOf ( 2 )
110
115
res . forEach ( packet => {
111
116
expect ( packet ) . to . have . keys ( 'success' , 'time' , 'text' )
112
117
expect ( packet . time ) . to . be . a ( 'number' )
@@ -130,7 +135,7 @@ describe('.ping', function () {
130
135
return ipfs . ping ( otherId )
131
136
. then ( ( res ) => {
132
137
expect ( res ) . to . be . an ( 'array' )
133
- expect ( res ) . to . have . lengthOf ( 3 )
138
+ expect ( res . filter ( isPong ) ) . to . have . lengthOf ( 1 )
134
139
res . forEach ( packet => {
135
140
expect ( packet ) . to . have . keys ( 'success' , 'time' , 'text' )
136
141
expect ( packet . time ) . to . be . a ( 'number' )
@@ -146,7 +151,7 @@ describe('.ping', function () {
146
151
collect ( ( err , data ) => {
147
152
expect ( err ) . to . not . exist ( )
148
153
expect ( data ) . to . be . an ( 'array' )
149
- expect ( data ) . to . have . lengthOf ( 3 )
154
+ expect ( data . filter ( isPong ) ) . to . have . lengthOf ( 1 )
150
155
data . forEach ( packet => {
151
156
expect ( packet ) . to . have . keys ( 'success' , 'time' , 'text' )
152
157
expect ( packet . time ) . to . be . a ( 'number' )
@@ -164,13 +169,13 @@ describe('.ping', function () {
164
169
. on ( 'data' , data => {
165
170
expect ( data ) . to . be . an ( 'object' )
166
171
expect ( data ) . to . have . keys ( 'success' , 'time' , 'text' )
167
- packetNum ++
172
+ if ( isPong ( data ) ) packetNum ++
168
173
} )
169
174
. on ( 'error' , err => {
170
175
expect ( err ) . not . to . exist ( )
171
176
} )
172
177
. on ( 'end' , ( ) => {
173
- expect ( packetNum ) . to . be . above ( 2 )
178
+ expect ( packetNum ) . to . equal ( 1 )
174
179
done ( )
175
180
} )
176
181
} )
0 commit comments