@ -1,87 +1,87 @@
'use strict' ;
'use strict' ;
var common = require ( '../common' ) ;
const common = require ( '../common' ) ;
var assert = require ( 'assert' ) ;
const assert = require ( 'assert' ) ;
const net = require ( 'net' ) ;
var net = require ( 'net' ) ;
var tests_run = 0 ;
function pingPongTest ( port , host ) {
function pingPongTest ( port , host ) {
var N = 1000 ;
const N = 1000 ;
var count = 0 ;
var count = 0 ;
var sentPongs = 0 ;
var sentPongs = 0 ;
var sent_final_ping = false ;
var sent_final_ping = false ;
var server = net . createServer ( { allowHalfOpen : true } , function ( socket ) {
const server = net . createServer (
console . log ( 'connection: ' + socket . remoteAddress ) ;
{ allowHalfOpen : true } ,
assert . equal ( server , socket . server ) ;
common . mustCall ( onSocket )
assert . equal ( 1 , server . connections ) ;
) ;
function onSocket ( socket ) {
assert . strictEqual ( socket . server , server ) ;
server . getConnections ( common . mustCall ( function ( err , connections ) {
assert . ifError ( err ) ;
assert . strictEqual ( connections , 1 ) ;
} ) ) ;
socket . setNoDelay ( ) ;
socket . setNoDelay ( ) ;
socket . timeout = 0 ;
socket . timeout = 0 ;
socket . setEncoding ( 'utf8' ) ;
socket . setEncoding ( 'utf8' ) ;
socket . on ( 'data' , function ( data ) {
socket . on ( 'data' , common . mustCall ( function ( data ) {
// Since we never queue data (we're always waiting for the PING
// Since we never queue data (we're always waiting for the PING
// before sending a pong) the writeQueueSize should always be less
// before sending a pong) the writeQueueSize should always be less
// than one message.
// than one message.
assert . ok ( 0 <= socket . bufferSize && socket . bufferSize <= 4 ) ;
assert . ok ( 0 <= socket . bufferSize && socket . bufferSize <= 4 ) ;
assert . equal ( true , socket . writabl e) ;
assert . strictEqual ( socket . writable , tru e) ;
assert . equal ( true , socket . readabl e) ;
assert . strictEqual ( socket . readable , tru e) ;
assert . equal ( true , count <= N ) ;
assert . ok ( count <= N ) ;
assert . e qual( data , 'PING' ) ;
assert . strictE qual( data , 'PING' ) ;
socket . write ( 'PONG' , function ( ) {
socket . write ( 'PONG' , common . mustCall ( function ( ) {
sentPongs ++ ;
sentPongs ++ ;
} ) ;
} ) ) ;
} ) ;
} , N + 1 ) ) ;
socket . on ( 'end' , function ( ) {
socket . on ( 'end' , common . mustCall ( function ( ) {
assert . equal ( true , socket . allowHalfOpen ) ;
assert . strictEqual ( socket . allowHalfOpen , true ) ;
assert . equal ( true , socket . writabl e) ; // because allowHalfOpen
assert . strictEqual ( socket . writable , tru e) ; // because allowHalfOpen
assert . equal ( false , socket . readabl e) ;
assert . strictEqual ( socket . readable , fals e) ;
socket . end ( ) ;
socket . end ( ) ;
} ) ;
} ) ) ;
socket . on ( 'error' , function ( e ) {
socket . on ( 'error' , common . fail ) ;
throw e ;
} ) ;
socket . on ( 'close' , function ( ) {
socket . on ( 'close' , common . mustCall ( function ( ) {
console . log ( 'server socket.end' ) ;
assert . strictEqual ( socket . writable , false ) ;
assert . equal ( false , socket . writable ) ;
assert . strictEqual ( socket . readable , false ) ;
assert . equal ( false , socket . readable ) ;
socket . server . close ( ) ;
socket . server . close ( ) ;
} ) ;
} ) ) ;
} ) ;
}
server . listen ( port , host , function ( ) {
server . listen ( port , host , common . mustCall ( function ( ) {
if ( this . address ( ) . port )
if ( this . address ( ) . port )
port = this . address ( ) . port ;
port = this . address ( ) . port ;
console . log ( ` server listening on ${ port } ${ host } ` ) ;
var client = net . createConnection ( port , host ) ;
const client = net . createConnection ( port , host ) ;
client . setEncoding ( 'ascii' ) ;
client . setEncoding ( 'ascii' ) ;
client . on ( 'connect' , function ( ) {
client . on ( 'connect' , common . mustCall ( function ( ) {
assert . equal ( true , client . readabl e) ;
assert . strictEqual ( client . readable , tru e) ;
assert . equal ( true , client . writabl e) ;
assert . strictEqual ( client . writable , tru e) ;
client . write ( 'PING' ) ;
client . write ( 'PING' ) ;
} ) ;
} ) ) ;
client . on ( 'data' , function ( data ) {
client . on ( 'data' , common . mustCall ( function ( data ) {
assert . equal ( 'PONG' , data ) ;
assert . strictEqual ( data , 'PONG' ) ;
count += 1 ;
count += 1 ;
if ( sent_final_ping ) {
if ( sent_final_ping ) {
assert . equal ( false , client . writabl e) ;
assert . strictEqual ( client . writable , fals e) ;
assert . equal ( true , client . readabl e) ;
assert . strictEqual ( client . readable , tru e) ;
return ;
return ;
} else {
} else {
assert . equal ( true , client . writabl e) ;
assert . strictEqual ( client . writable , tru e) ;
assert . equal ( true , client . readabl e) ;
assert . strictEqual ( client . readable , tru e) ;
}
}
if ( count < N ) {
if ( count < N ) {
@ -91,20 +91,16 @@ function pingPongTest(port, host) {
client . write ( 'PING' ) ;
client . write ( 'PING' ) ;
client . end ( ) ;
client . end ( ) ;
}
}
} ) ;
} , N + 1 ) ) ;
client . on ( 'close' , function ( ) {
client . on ( 'close' , common . mustCall ( function ( ) {
console . log ( 'client.end' ) ;
assert . strictEqual ( count , N + 1 ) ;
assert . equal ( N + 1 , count ) ;
assert . strictEqual ( sentPongs , N + 1 ) ;
assert . equal ( N + 1 , sentPongs ) ;
assert . strictEqual ( sent_final_ping , true ) ;
assert . equal ( true , sent_final_ping ) ;
} ) ) ;
tests_run += 1 ;
} ) ;
client . on ( 'error' , common . fail ) ;
} ) ) ;
client . on ( 'error' , function ( e ) {
throw e ;
} ) ;
} ) ;
}
}
/* All are run at once, so run on different ports */
/* All are run at once, so run on different ports */
@ -114,11 +110,3 @@ pingPongTest(0);
pingPongTest ( 0 , 'localhost' ) ;
pingPongTest ( 0 , 'localhost' ) ;
if ( common . hasIPv6 )
if ( common . hasIPv6 )
pingPongTest ( 0 , '::1' ) ;
pingPongTest ( 0 , '::1' ) ;
process . on ( 'exit' , function ( ) {
if ( common . hasIPv6 )
assert . equal ( 4 , tests_run ) ;
else
assert . equal ( 3 , tests_run ) ;
console . log ( 'done' ) ;
} ) ;