@@ -5,47 +5,48 @@ const common = require('../common');
5
5
const assert = require ( 'assert' ) ;
6
6
const http = require ( 'http' ) ;
7
7
8
- let time = Date . now ( ) ;
9
8
let intervalWasInvoked = false ;
10
- const TIMEOUT = common . platformTimeout ( 200 ) ;
11
-
12
- const server = http . createServer ( ( req , res ) => {
13
- server . close ( ) ;
14
-
15
- res . writeHead ( 200 ) ;
16
- res . flushHeaders ( ) ;
17
-
18
- req . setTimeout ( TIMEOUT , ( ) => {
19
- if ( ! intervalWasInvoked )
20
- return common . skip ( 'interval was not invoked quickly enough for test' ) ;
21
- assert . fail ( 'Request timeout should not fire' ) ;
9
+ const TIMEOUT = common . platformTimeout ( 50 ) ;
10
+
11
+ runTest ( TIMEOUT ) ;
12
+
13
+ function runTest ( timeoutDuration ) {
14
+ const server = http . createServer ( ( req , res ) => {
15
+ server . close ( ) ;
16
+
17
+ res . writeHead ( 200 ) ;
18
+ res . flushHeaders ( ) ;
19
+
20
+ req . setTimeout ( timeoutDuration , ( ) => {
21
+ if ( ! intervalWasInvoked ) {
22
+ // Interval wasn't invoked, probably because the machine is busy with
23
+ // other things. Try again with a longer timeout.
24
+ console . error ( `Retrying with timeout of ${ timeoutDuration * 2 } .` ) ;
25
+ return setImmediate ( ( ) => { runTest ( timeoutDuration * 2 ) ; } ) ;
26
+ }
27
+ assert . fail ( 'Request timeout should not fire' ) ;
28
+ } ) ;
29
+
30
+ req . resume ( ) ;
31
+ req . once ( 'end' , ( ) => {
32
+ res . end ( ) ;
33
+ } ) ;
22
34
} ) ;
23
35
24
- req . resume ( ) ;
25
- req . once ( 'end' , ( ) => {
26
- res . end ( ) ;
27
- } ) ;
28
- } ) ;
29
-
30
- server . listen ( 0 , common . mustCall ( ( ) => {
31
- const req = http . request ( {
32
- port : server . address ( ) . port ,
33
- method : 'POST'
34
- } , ( ) => {
35
- const interval = setInterval ( ( ) => {
36
- intervalWasInvoked = true ;
37
- // If machine is busy enough that the interval takes more than TIMEOUT ms
38
- // to be invoked, skip the test.
39
- const now = Date . now ( ) ;
40
- if ( now - time > TIMEOUT )
41
- return common . skip ( 'interval is not invoked quickly enough for test' ) ;
42
- time = now ;
43
- req . write ( 'a' ) ;
44
- } , common . platformTimeout ( 25 ) ) ;
45
- setTimeout ( ( ) => {
46
- clearInterval ( interval ) ;
47
- req . end ( ) ;
48
- } , TIMEOUT ) ;
49
- } ) ;
50
- req . write ( '.' ) ;
51
- } ) ) ;
36
+ server . listen ( 0 , common . mustCall ( ( ) => {
37
+ const req = http . request ( {
38
+ port : server . address ( ) . port ,
39
+ method : 'POST'
40
+ } , ( ) => {
41
+ const interval = setInterval ( ( ) => {
42
+ intervalWasInvoked = true ;
43
+ req . write ( 'a' ) ;
44
+ } , common . platformTimeout ( 25 ) ) ;
45
+ setTimeout ( ( ) => {
46
+ clearInterval ( interval ) ;
47
+ req . end ( ) ;
48
+ } , timeoutDuration ) ;
49
+ } ) ;
50
+ req . write ( '.' ) ;
51
+ } ) ) ;
52
+ }
0 commit comments