Skip to content

Commit ca3f202

Browse files
committedOct 2, 2021
Merge branch 'PHP-7.4' into PHP-8.0
2 parents 5db6e35 + 08f52b1 commit ca3f202

File tree

3 files changed

+149
-14
lines changed

3 files changed

+149
-14
lines changed
 

‎sapi/fpm/tests/proc-idle-timeout.phpt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
FPM: Process manager config pm.process_idle_timeout
3+
--SKIPIF--
4+
<?php
5+
include "skipif.inc";
6+
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
7+
?>
8+
--FILE--
9+
<?php
10+
11+
require_once "tester.inc";
12+
13+
$cfg = <<<EOT
14+
[global]
15+
error_log = {{FILE:LOG}}
16+
[unconfined]
17+
listen = {{ADDR}}
18+
pm = ondemand
19+
pm.max_children = 3
20+
pm.process_idle_timeout = 1
21+
pm.status_path = /status
22+
EOT;
23+
24+
$code = <<<EOT
25+
<?php
26+
usleep(200);
27+
EOT;
28+
29+
$tester = new FPM\Tester($cfg, $code);
30+
$tester->start();
31+
$tester->expectLogStartNotices();
32+
$tester->multiRequest(2);
33+
$tester->status([
34+
'total processes' => 2,
35+
]);
36+
// wait for process idle timeout
37+
sleep(4);
38+
$tester->status([
39+
'total processes' => 1,
40+
]);
41+
$tester->terminate();
42+
$tester->expectLogTerminatingNotices();
43+
$tester->close();
44+
45+
?>
46+
Done
47+
--EXPECT--
48+
Done
49+
--CLEAN--
50+
<?php
51+
require_once "tester.inc";
52+
FPM\Tester::clean();
53+
?>

‎sapi/fpm/tests/status.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Status
8787
if ($startTimeTimestamp && $fields['start time'][0] === '\\') {
8888
$fields['start time'] = '\d+';
8989
}
90-
$pattern = '|' . $header;
90+
$pattern = '(' . $header;
9191
foreach ($fields as $name => $value) {
9292
if ($nameTransformer) {
9393
$name = call_user_func($nameTransformer, $name);
@@ -102,7 +102,7 @@ class Status
102102
}
103103
}
104104
$pattern = rtrim($pattern, $rowPattern[strlen($rowPattern) - 1]);
105-
$pattern .= $footer . '|';
105+
$pattern .= $footer . ')';
106106

107107
if (!preg_match($pattern, $body)) {
108108
echo "ERROR: Expected body does not match pattern\n";

‎sapi/fpm/tests/tester.inc

Lines changed: 94 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ class Tester
522522
}
523523

524524
/**
525-
* Execute request.
525+
* Get request params array.
526526
*
527527
* @param string $query
528528
* @param array $headers
@@ -531,20 +531,13 @@ class Tester
531531
* @param string|null $successMessage
532532
* @param string|null $errorMessage
533533
* @param bool $connKeepAlive
534-
* @return Response
534+
* @return array
535535
*/
536-
public function request(
536+
private function getRequestParams(
537537
string $query = '',
538538
array $headers = [],
539-
string $uri = null,
540-
string $address = null,
541-
string $successMessage = null,
542-
string $errorMessage = null,
543-
bool $connKeepAlive = false
539+
string $uri = null
544540
) {
545-
if ($this->hasError()) {
546-
return new Response(null, true);
547-
}
548541
if (is_null($uri)) {
549542
$uri = $this->makeSourceFile();
550543
}
@@ -571,9 +564,38 @@ class Tester
571564
],
572565
$headers
573566
);
574-
$params = array_filter($params, function($value) {
567+
568+
return array_filter($params, function($value) {
575569
return !is_null($value);
576570
});
571+
}
572+
573+
/**
574+
* Execute request.
575+
*
576+
* @param string $query
577+
* @param array $headers
578+
* @param string|null $uri
579+
* @param string|null $address
580+
* @param string|null $successMessage
581+
* @param string|null $errorMessage
582+
* @param bool $connKeepAlive
583+
* @return Response
584+
*/
585+
public function request(
586+
string $query = '',
587+
array $headers = [],
588+
string $uri = null,
589+
string $address = null,
590+
string $successMessage = null,
591+
string $errorMessage = null,
592+
bool $connKeepAlive = false
593+
) {
594+
if ($this->hasError()) {
595+
return new Response(null, true);
596+
}
597+
598+
$params = $this->getRequestParams($query, $headers, $uri);
577599

578600
try {
579601
$this->response = new Response(
@@ -594,6 +616,66 @@ class Tester
594616
return $this->response;
595617
}
596618

619+
/**
620+
* Execute multiple requests in parallel.
621+
*
622+
* @param array|int $requests
623+
* @param string|null $address
624+
* @param string|null $successMessage
625+
* @param string|null $errorMessage
626+
* @param bool $connKeepAlive
627+
* @return Response[]
628+
* @throws \Exception
629+
*/
630+
public function multiRequest(
631+
$requests,
632+
string $address = null,
633+
string $successMessage = null,
634+
string $errorMessage = null,
635+
bool $connKeepAlive = false
636+
) {
637+
if ($this->hasError()) {
638+
return new Response(null, true);
639+
}
640+
641+
if (is_numeric($requests)) {
642+
$requests = array_fill(0, $requests, []);
643+
} elseif (!is_array($requests)) {
644+
throw new \Exception('Requests can be either numeric or array');
645+
}
646+
647+
try {
648+
$connections = array_map(function ($requestData) use ($address, $connKeepAlive) {
649+
$client = $this->getClient($address, $connKeepAlive);
650+
$params = $this->getRequestParams(
651+
$requestData['query'] ?? '',
652+
$requestData['headers'] ?? [],
653+
$requestData['uri'] ?? null
654+
);
655+
return [
656+
'client' => $client,
657+
'requestId' => $client->async_request($params, false),
658+
];
659+
}, $requests);
660+
661+
$responses = array_map(function ($conn) {
662+
$response = new Response($conn['client']->wait_for_response_data($conn['requestId']));
663+
if ($this->debug) {
664+
$response->debugOutput();
665+
}
666+
return $response;
667+
}, $connections);
668+
$this->message($successMessage);
669+
return $responses;
670+
} catch (\Exception $exception) {
671+
if ($errorMessage === null) {
672+
$this->error("Request failed", $exception);
673+
} else {
674+
$this->message($errorMessage);
675+
}
676+
}
677+
}
678+
597679
/**
598680
* Get client.
599681
*

0 commit comments

Comments
 (0)