20
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
22
22
'use strict' ;
23
- require ( '../common' ) ;
23
+ const common = require ( '../common' ) ;
24
24
const assert = require ( 'assert' ) ;
25
25
const net = require ( 'net' ) ;
26
26
27
- let dataWritten = false ;
28
- let connectHappened = false ;
29
-
30
- const tcp = net . Server ( function ( s ) {
27
+ const tcp = net . Server ( common . mustCall ( ( s ) => {
31
28
tcp . close ( ) ;
32
29
33
- console . log ( 'tcp server connection' ) ;
34
-
35
30
let buf = '' ;
31
+ s . setEncoding ( 'utf8' ) ;
36
32
s . on ( 'data' , function ( d ) {
37
33
buf += d ;
38
34
} ) ;
39
35
40
36
s . on ( 'end' , function ( ) {
41
- console . error ( 'SERVER: end' , buf . toString ( ) ) ;
37
+ console . error ( 'SERVER: end' , buf ) ;
42
38
assert . strictEqual ( buf , "L'État, c'est moi" ) ;
43
- console . log ( 'tcp socket disconnect' ) ;
44
39
s . end ( ) ;
45
40
} ) ;
41
+ } ) ) ;
46
42
47
- s . on ( 'error' , function ( e ) {
48
- console . log ( `tcp server-side error: ${ e . message } ` ) ;
49
- process . exit ( 1 ) ;
50
- } ) ;
51
- } ) ;
52
-
53
- tcp . listen ( 0 , function ( ) {
43
+ tcp . listen ( 0 , common . mustCall ( function ( ) {
54
44
const socket = net . Stream ( { highWaterMark : 0 } ) ;
55
45
56
- console . log ( 'Connecting to socket ' ) ;
57
-
58
- socket . connect ( this . address ( ) . port , function ( ) {
59
- console . log ( 'socket connected' ) ;
60
- connectHappened = true ;
61
- } ) ;
62
-
63
- console . log ( `connecting = ${ socket . connecting } ` ) ;
46
+ let connected = false ;
47
+ socket . connect ( this . address ( ) . port , common . mustCall ( ( ) => connected = true ) ) ;
64
48
65
- assert . strictEqual ( 'opening' , socket . readyState ) ;
49
+ assert . strictEqual ( socket . connecting , true ) ;
50
+ assert . strictEqual ( socket . readyState , 'opening' ) ;
66
51
67
52
// Make sure that anything besides a buffer or a string throws.
68
- [ null ,
69
- true ,
70
- false ,
71
- undefined ,
72
- 1 ,
73
- 1.0 ,
74
- 1 / 0 ,
75
- + Infinity ,
76
- - Infinity ,
77
- [ ] ,
78
- { }
79
- ] . forEach ( function ( v ) {
80
- function f ( ) {
81
- console . error ( 'write' , v ) ;
82
- socket . write ( v ) ;
83
- }
84
- assert . throws ( f , TypeError ) ;
53
+ common . expectsError ( ( ) => socket . write ( null ) ,
54
+ {
55
+ code : 'ERR_STREAM_NULL_VALUES' ,
56
+ type : TypeError ,
57
+ message : 'May not write null values to stream'
58
+ } ) ;
59
+ [
60
+ true ,
61
+ false ,
62
+ undefined ,
63
+ 1 ,
64
+ 1.0 ,
65
+ 1 / 0 ,
66
+ + Infinity ,
67
+ - Infinity ,
68
+ [ ] ,
69
+ { }
70
+ ] . forEach ( ( v ) => {
71
+ common . expectsError ( ( ) => socket . write ( v ) , {
72
+ code : 'ERR_INVALID_ARG_TYPE' ,
73
+ type : TypeError ,
74
+ message : 'The "chunk" argument must be one of type string or Buffer'
75
+ } ) ;
85
76
} ) ;
86
77
87
78
// Write a string that contains a multi-byte character sequence to test that
@@ -92,26 +83,15 @@ tcp.listen(0, function() {
92
83
// We're still connecting at this point so the datagram is first pushed onto
93
84
// the connect queue. Make sure that it's not added to `bytesWritten` again
94
85
// when the actual write happens.
95
- const r = socket . write ( a , function ( er ) {
86
+ const r = socket . write ( a , common . mustCall ( ( er ) => {
96
87
console . error ( 'write cb' ) ;
97
- dataWritten = true ;
98
- assert . ok ( connectHappened ) ;
99
- console . error ( 'socket.bytesWritten' , socket . bytesWritten ) ;
100
- //assert.strictEqual(socket.bytesWritten, Buffer.from(a + b).length);
101
- console . error ( 'data written' ) ;
102
- } ) ;
103
- console . error ( 'socket.bytesWritten' , socket . bytesWritten ) ;
104
- console . error ( 'write returned' , r ) ;
88
+ assert . ok ( connected ) ;
89
+ assert . strictEqual ( socket . bytesWritten , Buffer . from ( a + b ) . length ) ;
90
+ } ) ) ;
105
91
106
92
assert . strictEqual ( socket . bytesWritten , Buffer . from ( a ) . length ) ;
107
-
108
- assert . strictEqual ( false , r ) ;
93
+ assert . strictEqual ( r , false ) ;
109
94
socket . end ( b ) ;
110
95
111
- assert . strictEqual ( 'opening' , socket . readyState ) ;
112
- } ) ;
113
-
114
- process . on ( 'exit' , function ( ) {
115
- assert . ok ( connectHappened ) ;
116
- assert . ok ( dataWritten ) ;
117
- } ) ;
96
+ assert . strictEqual ( socket . readyState , 'opening' ) ;
97
+ } ) ) ;
0 commit comments