@@ -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 () {
@@ -148,43 +153,73 @@ class ServerClientTestCase
148153 eval ($ code );
149154 }
150155
151- public function run ($ masterCode , $ workerCode )
156+ /**
157+ * Run client and all workers
158+ *
159+ * @param string $clientCode The client PHP code
160+ * @param string|array $workerCode
161+ * @param bool $ephemeral Select whether automatic port selection and automatic awaiting is used
162+ * @return void
163+ * @throws Exception
164+ */
165+ public function run (string $ clientCode , string |array $ workerCode , bool $ ephemeral = true ): void
152166 {
153167 if (!is_array ($ workerCode )) {
154168 $ workerCode = [WORKER_DEFAULT_NAME => $ workerCode ];
155169 }
156- foreach ($ workerCode as $ worker => $ code ) {
170+ reset ($ workerCode );
171+ $ code = current ($ workerCode );
172+ $ worker = key ($ workerCode );
173+ while ($ worker != null ) {
157174 $ this ->spawnWorkerProcess ($ worker , $ this ->stripPhpTagsFromCode ($ code ));
175+ $ code = next ($ workerCode );
176+ if ($ ephemeral ) {
177+ $ addr = trim ($ this ->wait ($ worker ));
178+ if (empty ($ addr )) {
179+ throw new \Exception ("Failed server start " );
180+ }
181+ if ($ code === false ) {
182+ $ clientCode = preg_replace ('/{{\s*ADDR\s*}}/ ' , $ addr , $ clientCode );
183+ } else {
184+ $ code = preg_replace ('/{{\s*ADDR\s*}}/ ' , $ addr , $ code );
185+ }
186+ }
187+ $ worker = key ($ workerCode );
158188 }
159- eval ($ this ->stripPhpTagsFromCode ($ masterCode ));
189+
190+ eval ($ this ->stripPhpTagsFromCode ($ clientCode ));
160191 foreach ($ workerCode as $ worker => $ code ) {
161192 $ this ->cleanupWorkerProcess ($ worker );
162193 }
163194 }
164195
165- public function wait ($ worker , $ timeout = null )
196+ public function wait ($ worker , $ timeout = null ): ? string
166197 {
167198 $ handle = $ this ->isWorker ? STDIN : $ this ->workerStdOut [$ worker ];
168199 if ($ timeout === null ) {
169- fgets ($ handle );
170- return true ;
200+ return fgets ($ handle );
171201 }
172202
173203 stream_set_blocking ($ handle , false );
174204 $ read = [$ handle ];
175205 $ result = stream_select ($ read , $ write , $ except , $ timeout );
176206 if (!$ result ) {
177- return false ;
207+ return null ;
178208 }
179209
180- fgets ($ handle );
210+ $ result = fgets ($ handle );
181211 stream_set_blocking ($ handle , true );
182- return true ;
212+ return $ result ;
213+ }
214+
215+ public function notify (string $ worker , string $ message = "" ): void
216+ {
217+ fwrite ($ this ->isWorker ? STDOUT : $ this ->workerStdIn [$ worker ], "$ message \n" );
183218 }
184219
185- public function notify ( $ worker )
220+ public function notify_server_start ( $ server ): void
186221 {
187- fwrite ( $ this -> isWorker ? STDOUT : $ this -> workerStdIn [ $ worker ], "\n" ) ;
222+ echo stream_socket_get_name ( $ server , false ) . "\n" ;
188223 }
189224}
190225
0 commit comments