@ -1,23 +1,21 @@
'use strict' ;
'use strict' ;
var common = require ( '../common' ) ;
const common = require ( '../common' ) ;
var assert = require ( 'assert' ) ;
const assert = require ( 'assert' ) ;
var net = require ( 'net' ) ;
const net = require ( 'net' ) ;
// This test creates 200 connections to a server and sets the server's
// This test creates 20 connections to a server and sets the server's
// maxConnections property to 100 . The first 10 0 connections make it through
// maxConnections property to 10. The first 10 connections make it through
// and the last 100 connections are rejected.
// and the last 10 connections are rejected.
var N = 200 ;
const N = 20 ;
var count = 0 ;
var closes = 0 ;
var closes = 0 ;
var waits = [ ] ;
const waits = [ ] ;
var server = net . createServer ( function ( connection ) {
const server = net . createServer ( common . mustCall ( function ( connection ) {
console . error ( 'connect %d' , count ++ ) ;
connection . write ( 'hello' ) ;
connection . write ( 'hello' ) ;
waits . push ( function ( ) { connection . end ( ) ; } ) ;
waits . push ( function ( ) { connection . end ( ) ; } ) ;
} ) ;
} , N / 2 ) ) ;
server . listen ( 0 , function ( ) {
server . listen ( 0 , function ( ) {
makeConnection ( 0 ) ;
makeConnection ( 0 ) ;
@ -25,11 +23,9 @@ server.listen(0, function() {
server . maxConnections = N / 2 ;
server . maxConnections = N / 2 ;
console . error ( 'server.maxConnections = %d' , server . maxConnections ) ;
function makeConnection ( index ) {
function makeConnection ( index ) {
var c = net . createConnection ( server . address ( ) . port ) ;
const c = net . createConnection ( server . address ( ) . port ) ;
var gotData = false ;
var gotData = false ;
c . on ( 'connect' , function ( ) {
c . on ( 'connect' , function ( ) {
@ -42,10 +38,10 @@ function makeConnection(index) {
closes ++ ;
closes ++ ;
if ( closes < N / 2 ) {
if ( closes < N / 2 ) {
assert . ok ( server . maxConnections <= index ,
assert . ok (
index +
server . maxConnections <= index ,
' was one of the first closed connections ' +
` ${ index } should not have been one of the first closed connections `
'but shouldnt have been' ) ;
) ;
}
}
if ( closes === N / 2 ) {
if ( closes === N / 2 ) {
@ -58,11 +54,11 @@ function makeConnection(index) {
}
}
if ( index < server . maxConnections ) {
if ( index < server . maxConnections ) {
assert . e qual( true , gotData ,
assert . strictE qual( true , gotData ,
index + ' didn\'t get data, but should have' ) ;
` ${ index } didn't get data, but should have ` ) ;
} else {
} else {
assert . e qual( false , gotData ,
assert . strictE qual( false , gotData ,
index + ' got data, but shouldn\'t have' ) ;
` ${ index } got data, but shouldn't have ` ) ;
}
}
} ) ;
} ) ;
} ) ;
} ) ;
@ -86,5 +82,5 @@ function makeConnection(index) {
process . on ( 'exit' , function ( ) {
process . on ( 'exit' , function ( ) {
assert . e qual( N , closes ) ;
assert . strictE qual( N , closes ) ;
} ) ;
} ) ;