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