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 ) {
3234 req . socket . setTimeout ( 1000 ) ;
33- req . socket . on ( 'timeout' , function ( ) {
34- throw new Error ( 'Unexpected timeout' ) ;
35- } ) ;
35+ req . socket . on ( 'timeout' , ontimeout ) ;
3636 req . on ( 'end' , function ( ) {
3737 connections -- ;
3838 res . writeHead ( 200 ) ;
39+ req . socket . off ( 'timeout' , ontimeout ) ;
3940 res . end ( 'done\n' ) ;
4041 if ( connections === 0 ) {
4142 server . close ( ) ;
@@ -47,7 +48,7 @@ server.on('request', function(req, res) {
4748server . listen ( 0 , function ( ) {
4849 for ( let i = 0 ; i < 10 ; i ++ ) {
4950 connections ++ ;
50-
51+ let count = 0 ;
5152 setTimeout ( function ( ) {
5253 const request = http . request ( {
5354 port : server . address ( ) . port ,
@@ -56,13 +57,12 @@ server.listen(0, function() {
5657 } ) ;
5758
5859 function ping ( ) {
59- const nextPing = ( Math . random ( ) * 900 ) . toFixed ( ) ;
60- if ( nextPing > 600 ) {
60+ if ( ++ count === 10 ) {
6161 request . end ( ) ;
6262 return ;
6363 }
6464 request . write ( 'ping' ) ;
65- setTimeout ( ping , nextPing ) ;
65+ setTimeout ( ping , 300 ) ;
6666 }
6767 ping ( ) ;
6868 } , i * 50 ) ;
0 commit comments