@@ -12,6 +12,11 @@ const toSend = [Buffer.alloc(256, 'x'),
1212 'hello' ] ;
1313
1414const received = [ ] ;
15+ let totalBytesSent = 0 ;
16+ let totalBytesReceived = 0 ;
17+ const arrayBufferViewsCount = common . getArrayBufferViews (
18+ Buffer . from ( '' )
19+ ) . length ;
1520
1621client . on ( 'listening' , common . mustCall ( ( ) => {
1722 const port = client . address ( ) . port ;
@@ -21,24 +26,47 @@ client.on('listening', common.mustCall(() => {
2126 client . send ( [ toSend [ 2 ] ] , port ) ;
2227 client . send ( toSend [ 3 ] , 0 , toSend [ 3 ] . length , port ) ;
2328
24- client . send ( new Uint8Array ( toSend [ 0 ] ) , 0 , toSend [ 0 ] . length , port ) ;
25- client . send ( new Uint8Array ( toSend [ 1 ] ) , port ) ;
26- client . send ( [ new Uint8Array ( toSend [ 2 ] ) ] , port ) ;
27- client . send ( new Uint8Array ( Buffer . from ( toSend [ 3 ] ) ) ,
28- 0 , toSend [ 3 ] . length , port ) ;
29+ totalBytesSent += toSend . map ( ( buf ) => buf . length )
30+ . reduce ( ( a , b ) => a + b , 0 ) ;
31+
32+ for ( const msgBuf of common . getArrayBufferViews ( toSend [ 0 ] ) ) {
33+ client . send ( msgBuf , 0 , msgBuf . byteLength , port ) ;
34+ totalBytesSent += msgBuf . byteLength ;
35+ }
36+ for ( const msgBuf of common . getArrayBufferViews ( toSend [ 1 ] ) ) {
37+ client . send ( msgBuf , port ) ;
38+ totalBytesSent += msgBuf . byteLength ;
39+ }
40+ for ( const msgBuf of common . getArrayBufferViews ( toSend [ 2 ] ) ) {
41+ client . send ( [ msgBuf ] , port ) ;
42+ totalBytesSent += msgBuf . byteLength ;
43+ }
2944} ) ) ;
3045
3146client . on ( 'message' , common . mustCall ( ( buf , info ) => {
3247 received . push ( buf . toString ( ) ) ;
48+ totalBytesReceived += info . size ;
3349
34- if ( received . length === toSend . length * 2 ) {
35- // The replies may arrive out of order -> sort them before checking.
36- received . sort ( ) ;
37-
38- const expected = toSend . concat ( toSend ) . map ( String ) . sort ( ) ;
39- assert . deepStrictEqual ( received , expected ) ;
50+ if ( totalBytesReceived === totalBytesSent ) {
4051 client . close ( ) ;
4152 }
42- } , toSend . length * 2 ) ) ;
53+ // For every buffer in `toSend`, we send the raw Buffer,
54+ // as well as every TypedArray in getArrayBufferViews()
55+ } , toSend . length + ( toSend . length - 1 ) * arrayBufferViewsCount ) ) ;
56+
57+ client . on ( 'close' , common . mustCall ( ( buf , info ) => {
58+ // The replies may arrive out of order -> sort them before checking.
59+ received . sort ( ) ;
60+
61+ const repeated = [ ...toSend ] ;
62+ for ( let i = 0 ; i < arrayBufferViewsCount ; i ++ ) {
63+ repeated . push ( ...toSend . slice ( 0 , 3 ) ) ;
64+ }
65+
66+ assert . strictEqual ( totalBytesSent , totalBytesReceived ) ;
67+
68+ const expected = repeated . map ( String ) . sort ( ) ;
69+ assert . deepStrictEqual ( received , expected ) ;
70+ } ) ) ;
4371
4472client . bind ( 0 ) ;
0 commit comments