@@ -4,14 +4,19 @@ const WORKER_ARGV_VALUE = 'RUN_WORKER';
44
55const WORKER_DEFAULT_NAME = 'server ' ;
66
7- function phpt_notify ($ worker = WORKER_DEFAULT_NAME )
7+ function phpt_notify (string $ worker = WORKER_DEFAULT_NAME , string $ message = "" ): void
88{
9- ServerClientTestCase::getInstance ()->notify ($ worker );
9+ ServerClientTestCase::getInstance ()->notify ($ worker, $ message );
1010}
1111
12- function phpt_wait ($ worker = WORKER_DEFAULT_NAME , $ timeout = null )
12+ function phpt_wait ($ worker = WORKER_DEFAULT_NAME , $ timeout = null ): ? string
1313{
14- ServerClientTestCase::getInstance ()->wait ($ worker , $ timeout );
14+ return ServerClientTestCase::getInstance ()->wait ($ worker , $ timeout );
15+ }
16+
17+ function phpt_notify_server_start ($ server ): void
18+ {
19+ ServerClientTestCase::getInstance ()->notify_server_start ($ server );
1520}
1621
1722function phpt_has_sslv3 () {
@@ -149,43 +154,73 @@ class ServerClientTestCase
149154 eval ($ code );
150155 }
151156
152- public function run ($ masterCode , $ workerCode )
157+ /**
158+ * Run client and all workers
159+ *
160+ * @param string $clientCode The client PHP code
161+ * @param string|array $workerCode
162+ * @param bool $ephemeral Select whether automatic port selection and automatic awaiting is used
163+ * @return void
164+ * @throws Exception
165+ */
166+ public function run (string $ clientCode , string |array $ workerCode , bool $ ephemeral = true ): void
153167 {
154168 if (!is_array ($ workerCode )) {
155169 $ workerCode = [WORKER_DEFAULT_NAME => $ workerCode ];
156170 }
157- foreach ($ workerCode as $ worker => $ code ) {
171+ reset ($ workerCode );
172+ $ code = current ($ workerCode );
173+ $ worker = key ($ workerCode );
174+ while ($ worker != null ) {
158175 $ this ->spawnWorkerProcess ($ worker , $ this ->stripPhpTagsFromCode ($ code ));
176+ $ code = next ($ workerCode );
177+ if ($ ephemeral ) {
178+ $ addr = trim ($ this ->wait ($ worker ));
179+ if (empty ($ addr )) {
180+ throw new \Exception ("Failed server start " );
181+ }
182+ if ($ code === false ) {
183+ $ clientCode = preg_replace ('/{{\s*ADDR\s*}}/ ' , $ addr , $ clientCode );
184+ } else {
185+ $ code = preg_replace ('/{{\s*ADDR\s*}}/ ' , $ addr , $ code );
186+ }
187+ }
188+ $ worker = key ($ workerCode );
159189 }
160- eval ($ this ->stripPhpTagsFromCode ($ masterCode ));
190+
191+ eval ($ this ->stripPhpTagsFromCode ($ clientCode ));
161192 foreach ($ workerCode as $ worker => $ code ) {
162193 $ this ->cleanupWorkerProcess ($ worker );
163194 }
164195 }
165196
166- public function wait ($ worker , $ timeout = null )
197+ public function wait ($ worker , $ timeout = null ): ? string
167198 {
168199 $ handle = $ this ->isWorker ? STDIN : $ this ->workerStdOut [$ worker ];
169200 if ($ timeout === null ) {
170- fgets ($ handle );
171- return true ;
201+ return fgets ($ handle );
172202 }
173203
174204 stream_set_blocking ($ handle , false );
175205 $ read = [$ handle ];
176206 $ result = stream_select ($ read , $ write , $ except , $ timeout );
177207 if (!$ result ) {
178- return false ;
208+ return null ;
179209 }
180210
181- fgets ($ handle );
211+ $ result = fgets ($ handle );
182212 stream_set_blocking ($ handle , true );
183- return true ;
213+ return $ result ;
214+ }
215+
216+ public function notify (string $ worker , string $ message = "" ): void
217+ {
218+ fwrite ($ this ->isWorker ? STDOUT : $ this ->workerStdIn [$ worker ], "$ message \n" );
184219 }
185220
186- public function notify ( $ worker )
221+ public function notify_server_start ( $ server ): void
187222 {
188- fwrite ( $ this -> isWorker ? STDOUT : $ this -> workerStdIn [ $ worker ], "\n" ) ;
223+ echo stream_socket_get_name ( $ server , false ) . "\n" ;
189224 }
190225}
191226
0 commit comments