23
23
// This tests setTimeout() by having multiple clients connecting and sending
24
24
// data in random intervals. Clients are also randomly disconnecting until there
25
25
// are no more clients left. If no false timeout occurs, this test has passed.
26
- require ( '../common' ) ;
26
+ const common = require ( '../common' ) ;
27
27
const http = require ( 'http' ) ;
28
28
const server = http . createServer ( ) ;
29
29
let connections = 0 ;
30
30
31
+ const ontimeout = common . mustNotCall ( 'Unexpected timeout' ) ;
32
+
31
33
server . on ( 'request' , function ( req , res ) {
32
34
req . socket . setTimeout ( 1000 ) ;
33
- req . socket . on ( 'timeout' , function ( ) {
34
- throw new Error ( 'Unexpected timeout' ) ;
35
- } ) ;
35
+ req . socket . on ( 'timeout' , ontimeout ) ;
36
36
req . on ( 'end' , function ( ) {
37
37
connections -- ;
38
38
res . writeHead ( 200 ) ;
39
+ req . socket . off ( 'timeout' , ontimeout ) ;
39
40
res . end ( 'done\n' ) ;
40
41
if ( connections === 0 ) {
41
42
server . close ( ) ;
@@ -47,7 +48,7 @@ server.on('request', function(req, res) {
47
48
server . listen ( 0 , function ( ) {
48
49
for ( let i = 0 ; i < 10 ; i ++ ) {
49
50
connections ++ ;
50
-
51
+ let count = 0 ;
51
52
setTimeout ( function ( ) {
52
53
const request = http . request ( {
53
54
port : server . address ( ) . port ,
@@ -56,13 +57,12 @@ server.listen(0, function() {
56
57
} ) ;
57
58
58
59
function ping ( ) {
59
- const nextPing = ( Math . random ( ) * 900 ) . toFixed ( ) ;
60
- if ( nextPing > 600 ) {
60
+ if ( ++ count === 10 ) {
61
61
request . end ( ) ;
62
62
return ;
63
63
}
64
64
request . write ( 'ping' ) ;
65
- setTimeout ( ping , nextPing ) ;
65
+ setTimeout ( ping , 300 ) ;
66
66
}
67
67
ping ( ) ;
68
68
} , i * 50 ) ;
0 commit comments