@@ -76,18 +76,41 @@ public function startTestServer($code)
76
76
throw new \Exception ("Error starting test server on pid {$ pid }, command failed: {$ command }" );
77
77
}
78
78
79
- while ($ r = \fgets ($ this ->serverStderr )) {
80
- if (\str_contains ($ r , 'started ' )) {
79
+ while (true ) {
80
+ $ conn = @\fsockopen ('localhost ' , $ this ->serverPort );
81
+ if (\is_resource ($ conn )) {
82
+ \fclose ($ conn );
83
+
81
84
break ;
82
85
}
83
- if (\str_contains ($ r , 'Failed ' )) {
84
- throw new \Exception ("Error starting test server on pid {$ pid }: " . $ r . ' Was the port ' . $ this ->serverPort . ' already taken? ' );
85
- }
86
+ \fclose ($ conn );
86
87
}
87
88
88
89
return 'localhost: ' . $ this ->serverPort ;
89
90
}
90
91
92
+ /**
93
+ * Dirty way to parse the stderr of `php -S` to see how many
94
+ * requests were sent. Not robust -- and the format of the
95
+ * output changes from PHP version to PHP version, so beware.
96
+ *
97
+ * @param mixed $s
98
+ */
99
+ private static function isPHPTestServerRequestLogLine ($ s )
100
+ {
101
+ if (\str_contains ($ s , 'Accepted ' )) {
102
+ return false ;
103
+ }
104
+ if (\str_contains ($ s , 'Closing ' )) {
105
+ return false ;
106
+ }
107
+
108
+ // Lines start with a left square bracket, and contain
109
+ // a status code in brackets followed by a colon, like [200]:
110
+ // or [404]:
111
+ return \preg_match ('/^\[.*\[[0-9]{3}\]:/ ' , $ s );
112
+ }
113
+
91
114
/**
92
115
* Stops the test server and returns the number of requests
93
116
* as indicated the logs in its stderr.
@@ -102,19 +125,33 @@ public function stopTestServer()
102
125
\stream_set_blocking ($ this ->serverStderr , false );
103
126
$ lines = \explode (\PHP_EOL , \stream_get_contents ($ this ->serverStderr ));
104
127
foreach ($ lines as $ line ) {
105
- if (\str_contains ($ line, ' Accepted ' )) {
128
+ if (self :: isPHPTestServerRequestLogLine ($ line )) {
106
129
++$ n ;
130
+ } else {
131
+ \flush ();
132
+ \ob_flush ();
107
133
}
108
134
}
109
- while (true ) {
135
+
136
+ for ($ i = 0 ; $ i < 20 ; ++$ i ) {
110
137
$ status = \proc_get_status ($ this ->serverProc );
111
138
if (!$ status ['running ' ]) {
112
139
break ;
113
140
}
114
141
$ pid = $ status ['pid ' ];
142
+ // Kills any child processes -- the php test server
143
+ // appears to start up a child.
115
144
\exec ("pkill -P {$ pid }" );
145
+
146
+ // Kill the parent process.
147
+ \exec ("kill {$ pid }" );
116
148
\usleep (100000 );
117
149
}
150
+
151
+ if ($ status ['running ' ]) {
152
+ throw new Exception ('Could not kill test server ' );
153
+ }
154
+
118
155
// echo "Terminated test server on pid $pid\n";
119
156
\fclose ($ this ->serverStderr );
120
157
\proc_close ($ this ->serverProc );
0 commit comments