File tree 2 files changed +40
-1
lines changed
2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ const errCode = require('err-code')
8
8
const crypto = require ( 'libp2p-crypto' )
9
9
const pipe = require ( 'it-pipe' )
10
10
const { toBuffer } = require ( 'it-buffer' )
11
- const { collect } = require ( 'streaming-iterables' )
11
+ const { collect, take } = require ( 'streaming-iterables' )
12
12
13
13
const { PROTOCOL , PING_LENGTH } = require ( './constants' )
14
14
@@ -29,6 +29,7 @@ async function ping (node, peer) {
29
29
const [ result ] = await pipe (
30
30
[ data ] ,
31
31
stream ,
32
+ stream => take ( 1 , stream ) ,
32
33
toBuffer ,
33
34
collect
34
35
)
Original file line number Diff line number Diff line change @@ -6,9 +6,11 @@ chai.use(require('dirty-chai'))
6
6
const { expect } = chai
7
7
8
8
const pTimes = require ( 'p-times' )
9
+ const pipe = require ( 'it-pipe' )
9
10
10
11
const peerUtils = require ( '../utils/creators/peer' )
11
12
const baseOptions = require ( '../utils/base-options' )
13
+ const { PROTOCOL } = require ( '../../src/ping/constants' )
12
14
13
15
describe ( 'ping' , ( ) => {
14
16
let nodes
@@ -32,4 +34,40 @@ describe('ping', () => {
32
34
const averageLatency = latencies . reduce ( ( p , c ) => p + c , 0 ) / latencies . length
33
35
expect ( averageLatency ) . to . be . a ( 'Number' )
34
36
} )
37
+
38
+ it ( 'only waits for the first response to arrive' , async ( ) => {
39
+ nodes [ 1 ] . handle ( PROTOCOL , async ( { connection, stream } ) => {
40
+ let firstInvocation = true
41
+
42
+ await pipe (
43
+ stream ,
44
+ function ( stream ) {
45
+ const output = {
46
+ [ Symbol . asyncIterator ] : ( ) => output ,
47
+ next : async ( ) => {
48
+ if ( firstInvocation ) {
49
+ firstInvocation = false
50
+
51
+ for await ( const data of stream ) {
52
+ return {
53
+ value : data ,
54
+ done : false
55
+ }
56
+ }
57
+ } else {
58
+ return new Promise ( ) // never resolve
59
+ }
60
+ }
61
+ }
62
+
63
+ return output
64
+ } ,
65
+ stream
66
+ )
67
+ } )
68
+
69
+ const latency = await nodes [ 0 ] . ping ( nodes [ 1 ] . peerInfo )
70
+
71
+ expect ( latency ) . to . be . a ( 'Number' )
72
+ } )
35
73
} )
You can’t perform that action at this time.
0 commit comments