2323// This tests setTimeout() by having multiple clients connecting and sending
2424// data in random intervals. Clients are also randomly disconnecting until there
2525// are no more clients left. If no false timeout occurs, this test has passed.
26- require ( '../common' ) ;
26+ const common = require ( '../common' ) ;
2727const http = require ( 'http' ) ;
2828const server = http . createServer ( ) ;
2929let connections = 0 ;
3030
31+ const ontimeout = common . mustNotCall ( 'Unexpected timeout' ) ;
32+
3133server . on ( 'request' , function ( req , res ) {
3234req . socket . setTimeout ( 1000 ) ;
33- req . socket . on ( 'timeout' , function ( ) {
34- throw new Error ( 'Unexpected timeout' ) ;
35- } ) ;
35+ req . socket . on ( 'timeout' , ontimeout ) ;
3636req . on ( 'end' , function ( ) {
3737connections -- ;
3838res . writeHead ( 200 ) ;
39+ req . socket . off ( 'timeout' , ontimeout ) ;
3940res . end ( 'done\n' ) ;
4041if ( connections === 0 ) {
4142server . close ( ) ;
@@ -47,7 +48,7 @@ server.on('request', function(req, res){
4748server . listen ( 0 , function ( ) {
4849for ( let i = 0 ; i < 10 ; i ++ ) {
4950connections ++ ;
50-
51+ let count = 0 ;
5152setTimeout ( function ( ) {
5253const request = http . request ( {
5354port : server . address ( ) . port ,
@@ -56,13 +57,12 @@ server.listen(0, function(){
5657} ) ;
5758
5859function ping ( ) {
59- const nextPing = ( Math . random ( ) * 900 ) . toFixed ( ) ;
60- if ( nextPing > 600 ) {
60+ if ( ++ count === 10 ) {
6161request . end ( ) ;
6262return ;
6363}
6464request . write ( 'ping' ) ;
65- setTimeout ( ping , nextPing ) ;
65+ setTimeout ( ping , 300 ) ;
6666}
6767ping ( ) ;
6868} , i * 50 ) ;
0 commit comments