@@ -4,14 +4,19 @@ const WORKER_ARGV_VALUE = 'RUN_WORKER';
4
4
5
5
const WORKER_DEFAULT_NAME = 'server ' ;
6
6
7
- function phpt_notify ($ worker = WORKER_DEFAULT_NAME )
7
+ function phpt_notify (string $ worker = WORKER_DEFAULT_NAME , string $ message = "" ): void
8
8
{
9
- ServerClientTestCase::getInstance ()->notify ($ worker );
9
+ ServerClientTestCase::getInstance ()->notify ($ worker, $ message );
10
10
}
11
11
12
- function phpt_wait ($ worker = WORKER_DEFAULT_NAME , $ timeout = null )
12
+ function phpt_wait ($ worker = WORKER_DEFAULT_NAME , $ timeout = null ): ? string
13
13
{
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 );
15
20
}
16
21
17
22
function phpt_has_sslv3 () {
@@ -149,43 +154,73 @@ class ServerClientTestCase
149
154
eval ($ code );
150
155
}
151
156
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
153
167
{
154
168
if (!is_array ($ workerCode )) {
155
169
$ workerCode = [WORKER_DEFAULT_NAME => $ workerCode ];
156
170
}
157
- foreach ($ workerCode as $ worker => $ code ) {
171
+ reset ($ workerCode );
172
+ $ code = current ($ workerCode );
173
+ $ worker = key ($ workerCode );
174
+ while ($ worker != null ) {
158
175
$ 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 );
159
189
}
160
- eval ($ this ->stripPhpTagsFromCode ($ masterCode ));
190
+
191
+ eval ($ this ->stripPhpTagsFromCode ($ clientCode ));
161
192
foreach ($ workerCode as $ worker => $ code ) {
162
193
$ this ->cleanupWorkerProcess ($ worker );
163
194
}
164
195
}
165
196
166
- public function wait ($ worker , $ timeout = null )
197
+ public function wait ($ worker , $ timeout = null ): ? string
167
198
{
168
199
$ handle = $ this ->isWorker ? STDIN : $ this ->workerStdOut [$ worker ];
169
200
if ($ timeout === null ) {
170
- fgets ($ handle );
171
- return true ;
201
+ return fgets ($ handle );
172
202
}
173
203
174
204
stream_set_blocking ($ handle , false );
175
205
$ read = [$ handle ];
176
206
$ result = stream_select ($ read , $ write , $ except , $ timeout );
177
207
if (!$ result ) {
178
- return false ;
208
+ return null ;
179
209
}
180
210
181
- fgets ($ handle );
211
+ $ result = fgets ($ handle );
182
212
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" );
184
219
}
185
220
186
- public function notify ( $ worker )
221
+ public function notify_server_start ( $ server ): void
187
222
{
188
- fwrite ( $ this -> isWorker ? STDOUT : $ this -> workerStdIn [ $ worker ], "\n" ) ;
223
+ echo stream_socket_get_name ( $ server , false ) . "\n" ;
189
224
}
190
225
}
191
226
0 commit comments