@@ -9,48 +9,64 @@ function curl_cli_server_start() {
9
9
return getenv ('PHP_CURL_HTTP_REMOTE_SERVER ' );
10
10
}
11
11
12
- $ php_executable = getenv ('TEST_PHP_EXECUTABLE ' );
13
- $ doc_root = __DIR__ ;
14
- $ router = "responder/get.php " ;
15
-
16
- $ descriptorspec = array (
17
- 0 => STDIN ,
18
- 1 => STDOUT ,
19
- 2 => STDERR ,
20
- );
21
-
22
- if (substr (PHP_OS , 0 , 3 ) == 'WIN ' ) {
23
- $ cmd = "{$ php_executable } -t {$ doc_root } -n -S " . PHP_CURL_SERVER_ADDRESS ;
12
+ $ php_executable = getenv ('TEST_PHP_EXECUTABLE ' );
13
+ $ doc_root = __DIR__ ;
14
+ $ router = "responder/get.php " ;
15
+
16
+ $ descriptorspec = array (
17
+ 0 => STDIN ,
18
+ 1 => STDOUT ,
19
+ 2 => STDERR ,
20
+ );
21
+
22
+ if (substr (PHP_OS , 0 , 3 ) == 'WIN ' ) {
23
+ $ cmd = "{$ php_executable } -t {$ doc_root } -n -S " . PHP_CURL_SERVER_ADDRESS ;
24
+ $ cmd .= " {$ router }" ;
25
+ $ handle = proc_open (addslashes ($ cmd ), $ descriptorspec , $ pipes , $ doc_root , NULL , array ("bypass_shell " => true , "suppress_errors " => true ));
26
+ } else {
27
+ $ cmd = "exec {$ php_executable } -t {$ doc_root } -n -S " . PHP_CURL_SERVER_ADDRESS ;
24
28
$ cmd .= " {$ router }" ;
25
- $ handle = proc_open (addslashes ($ cmd ), $ descriptorspec , $ pipes , $ doc_root , NULL , array ("bypass_shell " => true , "suppress_errors " => true ));
26
- } else {
27
- $ cmd = "exec {$ php_executable } -t {$ doc_root } -n -S " . PHP_CURL_SERVER_ADDRESS ;
28
- $ cmd .= " {$ router }" ;
29
- $ cmd .= " 2>/dev/null " ;
30
-
31
- $ handle = proc_open ($ cmd , $ descriptorspec , $ pipes , $ doc_root );
32
- }
29
+ $ cmd .= " 2>/dev/null " ;
30
+
31
+ $ handle = proc_open ($ cmd , $ descriptorspec , $ pipes , $ doc_root );
32
+ }
33
33
34
- // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
35
- // it might not be listening yet...need to wait until fsockopen() call returns
36
- $ i = 0 ;
37
- while (($ i ++ < 30 ) && !($ fp = @fsockopen (PHP_CURL_SERVER_HOSTNAME , PHP_CURL_SERVER_PORT ))) {
38
- usleep (10000 );
34
+ // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
35
+ // it might not be listening yet...need to wait until fsockopen() call returns
36
+ $ error = "Unable to connect to servers \n" ;
37
+ for ($ i =0 ; $ i < 60 ; $ i ++) {
38
+ usleep (25000 ); // 25ms per try
39
+ $ status = proc_get_status ($ handle );
40
+ $ fp = @fsockopen (PHP_CURL_SERVER_HOSTNAME , PHP_CURL_SERVER_PORT );
41
+ // Failure, the server is no longer running
42
+ if (!($ status && $ status ['running ' ])) {
43
+ $ error = "Server is not running \n" ;
44
+ break ;
45
+ }
46
+ // Success, Connected to servers
47
+ if ($ fp ) {
48
+ $ error = '' ;
49
+ break ;
50
+ }
39
51
}
40
52
41
53
if ($ fp ) {
42
54
fclose ($ fp );
43
55
}
44
56
45
- register_shutdown_function (
46
- function ($ handle ) use ($ router ) {
47
- proc_terminate ($ handle );
48
- },
49
- $ handle
57
+ if ($ error ) {
58
+ echo $ error ;
59
+ proc_close ($ handle );
60
+ exit (1 );
61
+ }
62
+
63
+ register_shutdown_function (
64
+ function ($ handle ) use ($ router ) {
65
+ proc_terminate ($ handle );
66
+ },
67
+ $ handle
50
68
);
51
- // don't bother sleeping, server is already up
52
- // server can take a variable amount of time to be up, so just sleeping a guessed amount of time
53
- // does not work. this is why tests sometimes pass and sometimes fail. to get a reliable pass
54
- // sleeping doesn't work.
69
+
55
70
return PHP_CURL_SERVER_ADDRESS ;
56
71
}
72
+
0 commit comments