@@ -12,6 +12,11 @@ const toSend = [Buffer.alloc(256, 'x'),
12
12
'hello' ] ;
13
13
14
14
const received = [ ] ;
15
+ let totalBytesSent = 0 ;
16
+ let totalBytesReceived = 0 ;
17
+ const arrayBufferViewsCount = common . getArrayBufferViews (
18
+ Buffer . from ( '' )
19
+ ) . length ;
15
20
16
21
client . on ( 'listening' , common . mustCall ( ( ) => {
17
22
const port = client . address ( ) . port ;
@@ -21,24 +26,47 @@ client.on('listening', common.mustCall(() => {
21
26
client . send ( [ toSend [ 2 ] ] , port ) ;
22
27
client . send ( toSend [ 3 ] , 0 , toSend [ 3 ] . length , port ) ;
23
28
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
+ }
29
44
} ) ) ;
30
45
31
46
client . on ( 'message' , common . mustCall ( ( buf , info ) => {
32
47
received . push ( buf . toString ( ) ) ;
48
+ totalBytesReceived += info . size ;
33
49
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 ) {
40
51
client . close ( ) ;
41
52
}
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
+ } ) ) ;
43
71
44
72
client . bind ( 0 ) ;
0 commit comments