@@ -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 () {
@@ -119,43 +124,73 @@ class ServerClientTestCase
119
124
eval ($ code );
120
125
}
121
126
122
- public function run ($ masterCode , $ workerCode )
127
+ /**
128
+ * Run client and all workers
129
+ *
130
+ * @param string $clientCode The client PHP code
131
+ * @param string|array $workerCode
132
+ * @param bool $ephemeral Select whether automatic port selection and automatic awaiting is used
133
+ * @return void
134
+ * @throws Exception
135
+ */
136
+ public function run (string $ clientCode , string |array $ workerCode , bool $ ephemeral = true ): void
123
137
{
124
138
if (!is_array ($ workerCode )) {
125
139
$ workerCode = [WORKER_DEFAULT_NAME => $ workerCode ];
126
140
}
127
- foreach ($ workerCode as $ worker => $ code ) {
141
+ reset ($ workerCode );
142
+ $ code = current ($ workerCode );
143
+ $ worker = key ($ workerCode );
144
+ while ($ worker != null ) {
128
145
$ this ->spawnWorkerProcess ($ worker , $ this ->stripPhpTagsFromCode ($ code ));
146
+ $ code = next ($ workerCode );
147
+ if ($ ephemeral ) {
148
+ $ addr = trim ($ this ->wait ($ worker ));
149
+ if (empty ($ addr )) {
150
+ throw new \Exception ("Failed server start " );
151
+ }
152
+ if ($ code === false ) {
153
+ $ clientCode = preg_replace ('/{{\s*ADDR\s*}}/ ' , $ addr , $ clientCode );
154
+ } else {
155
+ $ code = preg_replace ('/{{\s*ADDR\s*}}/ ' , $ addr , $ code );
156
+ }
157
+ }
158
+ $ worker = key ($ workerCode );
129
159
}
130
- eval ($ this ->stripPhpTagsFromCode ($ masterCode ));
160
+
161
+ eval ($ this ->stripPhpTagsFromCode ($ clientCode ));
131
162
foreach ($ workerCode as $ worker => $ code ) {
132
163
$ this ->cleanupWorkerProcess ($ worker );
133
164
}
134
165
}
135
166
136
- public function wait ($ worker , $ timeout = null )
167
+ public function wait ($ worker , $ timeout = null ): ? string
137
168
{
138
169
$ handle = $ this ->isWorker ? STDIN : $ this ->workerStdOut [$ worker ];
139
170
if ($ timeout === null ) {
140
- fgets ($ handle );
141
- return true ;
171
+ return fgets ($ handle );
142
172
}
143
173
144
174
stream_set_blocking ($ handle , false );
145
175
$ read = [$ handle ];
146
176
$ result = stream_select ($ read , $ write , $ except , $ timeout );
147
177
if (!$ result ) {
148
- return false ;
178
+ return null ;
149
179
}
150
180
151
- fgets ($ handle );
181
+ $ result = fgets ($ handle );
152
182
stream_set_blocking ($ handle , true );
153
- return true ;
183
+ return $ result ;
184
+ }
185
+
186
+ public function notify (string $ worker , string $ message = "" ): void
187
+ {
188
+ fwrite ($ this ->isWorker ? STDOUT : $ this ->workerStdIn [$ worker ], "$ message \n" );
154
189
}
155
190
156
- public function notify ( $ worker )
191
+ public function notify_server_start ( $ server ): void
157
192
{
158
- fwrite ( $ this -> isWorker ? STDOUT : $ this -> workerStdIn [ $ worker ], "\n" ) ;
193
+ echo stream_socket_get_name ( $ server , false ) . "\n" ;
159
194
}
160
195
}
161
196
0 commit comments