1
1
'use strict' ;
2
- var common = require ( '../common' ) ;
3
- var assert = require ( 'assert' ) ;
4
-
5
- var net = require ( 'net' ) ;
6
-
7
- var tests_run = 0 ;
2
+ const common = require ( '../common' ) ;
3
+ const assert = require ( 'assert' ) ;
4
+ const net = require ( 'net' ) ;
8
5
9
6
function pingPongTest ( port , host ) {
10
- var N = 1000 ;
7
+ const N = 1000 ;
11
8
var count = 0 ;
12
9
var sentPongs = 0 ;
13
10
var sent_final_ping = false ;
14
11
15
- var server = net . createServer ( { allowHalfOpen : true } , function ( socket ) {
16
- console . log ( 'connection: ' + socket . remoteAddress ) ;
17
- assert . equal ( server , socket . server ) ;
18
- assert . equal ( 1 , server . connections ) ;
12
+ const server = net . createServer (
13
+ { allowHalfOpen : true } ,
14
+ common . mustCall ( onSocket )
15
+ ) ;
16
+
17
+ function onSocket ( socket ) {
18
+ assert . strictEqual ( socket . server , server ) ;
19
+ server . getConnections ( common . mustCall ( function ( err , connections ) {
20
+ assert . ifError ( err ) ;
21
+ assert . strictEqual ( connections , 1 ) ;
22
+ } ) ) ;
19
23
20
24
socket . setNoDelay ( ) ;
21
25
socket . timeout = 0 ;
22
26
23
27
socket . setEncoding ( 'utf8' ) ;
24
- socket . on ( 'data' , function ( data ) {
28
+ socket . on ( 'data' , common . mustCall ( function ( data ) {
25
29
// Since we never queue data (we're always waiting for the PING
26
30
// before sending a pong) the writeQueueSize should always be less
27
31
// than one message.
28
32
assert . ok ( 0 <= socket . bufferSize && socket . bufferSize <= 4 ) ;
29
33
30
- assert . equal ( true , socket . writable ) ;
31
- assert . equal ( true , socket . readable ) ;
32
- assert . equal ( true , count <= N ) ;
33
- assert . equal ( data , 'PING' ) ;
34
+ assert . strictEqual ( socket . writable , true ) ;
35
+ assert . strictEqual ( socket . readable , true ) ;
36
+ assert . ok ( count <= N ) ;
37
+ assert . strictEqual ( data , 'PING' ) ;
34
38
35
- socket . write ( 'PONG' , function ( ) {
39
+ socket . write ( 'PONG' , common . mustCall ( function ( ) {
36
40
sentPongs ++ ;
37
- } ) ;
38
- } ) ;
41
+ } ) ) ;
42
+ } , N + 1 ) ) ;
39
43
40
- socket . on ( 'end' , function ( ) {
41
- assert . equal ( true , socket . allowHalfOpen ) ;
42
- assert . equal ( true , socket . writable ) ; // because allowHalfOpen
43
- assert . equal ( false , socket . readable ) ;
44
+ socket . on ( 'end' , common . mustCall ( function ( ) {
45
+ assert . strictEqual ( socket . allowHalfOpen , true ) ;
46
+ assert . strictEqual ( socket . writable , true ) ; // because allowHalfOpen
47
+ assert . strictEqual ( socket . readable , false ) ;
44
48
socket . end ( ) ;
45
- } ) ;
49
+ } ) ) ;
46
50
47
- socket . on ( 'error' , function ( e ) {
48
- throw e ;
49
- } ) ;
51
+ socket . on ( 'error' , common . fail ) ;
50
52
51
- socket . on ( 'close' , function ( ) {
52
- console . log ( 'server socket.end' ) ;
53
- assert . equal ( false , socket . writable ) ;
54
- assert . equal ( false , socket . readable ) ;
53
+ socket . on ( 'close' , common . mustCall ( function ( ) {
54
+ assert . strictEqual ( socket . writable , false ) ;
55
+ assert . strictEqual ( socket . readable , false ) ;
55
56
socket . server . close ( ) ;
56
- } ) ;
57
- } ) ;
57
+ } ) ) ;
58
+ }
58
59
59
60
60
- server . listen ( port , host , function ( ) {
61
+ server . listen ( port , host , common . mustCall ( function ( ) {
61
62
if ( this . address ( ) . port )
62
63
port = this . address ( ) . port ;
63
- console . log ( `server listening on ${ port } ${ host } ` ) ;
64
64
65
- var client = net . createConnection ( port , host ) ;
65
+ const client = net . createConnection ( port , host ) ;
66
66
67
67
client . setEncoding ( 'ascii' ) ;
68
- client . on ( 'connect' , function ( ) {
69
- assert . equal ( true , client . readable ) ;
70
- assert . equal ( true , client . writable ) ;
68
+ client . on ( 'connect' , common . mustCall ( function ( ) {
69
+ assert . strictEqual ( client . readable , true ) ;
70
+ assert . strictEqual ( client . writable , true ) ;
71
71
client . write ( 'PING' ) ;
72
- } ) ;
72
+ } ) ) ;
73
73
74
- client . on ( 'data' , function ( data ) {
75
- assert . equal ( 'PONG' , data ) ;
74
+ client . on ( 'data' , common . mustCall ( function ( data ) {
75
+ assert . strictEqual ( data , 'PONG' ) ;
76
76
count += 1 ;
77
77
78
78
if ( sent_final_ping ) {
79
- assert . equal ( false , client . writable ) ;
80
- assert . equal ( true , client . readable ) ;
79
+ assert . strictEqual ( client . writable , false ) ;
80
+ assert . strictEqual ( client . readable , true ) ;
81
81
return ;
82
82
} else {
83
- assert . equal ( true , client . writable ) ;
84
- assert . equal ( true , client . readable ) ;
83
+ assert . strictEqual ( client . writable , true ) ;
84
+ assert . strictEqual ( client . readable , true ) ;
85
85
}
86
86
87
87
if ( count < N ) {
@@ -91,20 +91,16 @@ function pingPongTest(port, host) {
91
91
client . write ( 'PING' ) ;
92
92
client . end ( ) ;
93
93
}
94
- } ) ;
95
-
96
- client . on ( 'close' , function ( ) {
97
- console . log ( 'client.end' ) ;
98
- assert . equal ( N + 1 , count ) ;
99
- assert . equal ( N + 1 , sentPongs ) ;
100
- assert . equal ( true , sent_final_ping ) ;
101
- tests_run += 1 ;
102
- } ) ;
103
-
104
- client . on ( 'error' , function ( e ) {
105
- throw e ;
106
- } ) ;
107
- } ) ;
94
+ } , N + 1 ) ) ;
95
+
96
+ client . on ( 'close' , common . mustCall ( function ( ) {
97
+ assert . strictEqual ( count , N + 1 ) ;
98
+ assert . strictEqual ( sentPongs , N + 1 ) ;
99
+ assert . strictEqual ( sent_final_ping , true ) ;
100
+ } ) ) ;
101
+
102
+ client . on ( 'error' , common . fail ) ;
103
+ } ) ) ;
108
104
}
109
105
110
106
/* All are run at once, so run on different ports */
@@ -114,11 +110,3 @@ pingPongTest(0);
114
110
pingPongTest ( 0 , 'localhost' ) ;
115
111
if ( common . hasIPv6 )
116
112
pingPongTest ( 0 , '::1' ) ;
117
-
118
- process . on ( 'exit' , function ( ) {
119
- if ( common . hasIPv6 )
120
- assert . equal ( 4 , tests_run ) ;
121
- else
122
- assert . equal ( 3 , tests_run ) ;
123
- console . log ( 'done' ) ;
124
- } ) ;
0 commit comments