From 7c32e4b57bb6fa6f9608d48dc6eac11612f7b90a Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 19 Jan 2022 21:18:41 +0900 Subject: [PATCH 1/2] refactor: use usleep() instead of sleep() --- phpstan-baseline.neon.dist | 5 ----- system/HTTP/CURLRequest.php | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/phpstan-baseline.neon.dist b/phpstan-baseline.neon.dist index bc424a91445c..46aa7e5d1ae3 100644 --- a/phpstan-baseline.neon.dist +++ b/phpstan-baseline.neon.dist @@ -560,11 +560,6 @@ parameters: count: 1 path: system/Filters/Filters.php - - - message: "#^Parameter \\#1 \\$seconds of function sleep expects int, float given\\.$#" - count: 1 - path: system/HTTP/CURLRequest.php - - message: "#^Expression on left side of \\?\\? is not nullable\\.$#" count: 1 diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index dd031753b1fa..c8aceb9cdac0 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -354,7 +354,7 @@ public function send(string $method, string $url) // Do we need to delay this request? if ($this->delay > 0) { - sleep($this->delay); + usleep((int) $this->delay * 1_000_000); } $output = $this->sendRequest($curlOptions); From 98927283a9284ed8eee839b43beb681ec738f2cb Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 19 Jan 2022 21:19:45 +0900 Subject: [PATCH 2/2] test: change `delay` from 1000 to 100 To improve test speed. --- .../HTTP/CURLRequestDoNotShareOptionsTest.php | 22 +++++++++---------- tests/system/HTTP/CURLRequestTest.php | 22 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php b/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php index 4d35d4510271..21070736d741 100644 --- a/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php +++ b/tests/system/HTTP/CURLRequestDoNotShareOptionsTest.php @@ -267,13 +267,13 @@ public function testHeaderContentLengthNotSharedBetweenClients() public function testOptionsDelay() { + $request = $this->getRequest(); + $this->assertSame(0.0, $request->getDelay()); + $options = [ 'delay' => 2000, 'headers' => ['fruit' => 'apple'], ]; - $request = $this->getRequest(); - $this->assertSame(0.0, $request->getDelay()); - $request = $this->getRequest($options); $this->assertSame(2.0, $request->getDelay()); } @@ -715,20 +715,20 @@ public function testSendWithDelay() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->get('products'); // we still need to check the code coverage to make sure this was done - $this->assertSame(1.0, $request->getDelay()); + $this->assertSame(0.1, $request->getDelay()); } public function testSendContinued() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->setOutput("HTTP/1.1 100 Continue\x0d\x0a\x0d\x0aHi there"); @@ -743,7 +743,7 @@ public function testSendContinuedWithManyHeaders() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $output = "HTTP/1.1 100 Continue @@ -787,7 +787,7 @@ public function testSplitResponse() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->setOutput("Accept: text/html\x0d\x0a\x0d\x0aHi there"); @@ -799,7 +799,7 @@ public function testApplyBody() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->setBody('name=George'); @@ -814,7 +814,7 @@ public function testResponseHeaders() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->setOutput("HTTP/2.0 234 Ohoh\x0d\x0aAccept: text/html\x0d\x0a\x0d\x0aHi there"); @@ -828,7 +828,7 @@ public function testResponseHeadersShortProtocol() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->setOutput("HTTP/2 235 Ohoh\x0d\x0aAccept: text/html\x0d\x0a\x0d\x0aHi there shortie"); diff --git a/tests/system/HTTP/CURLRequestTest.php b/tests/system/HTTP/CURLRequestTest.php index e978944ad116..64b824cbad56 100644 --- a/tests/system/HTTP/CURLRequestTest.php +++ b/tests/system/HTTP/CURLRequestTest.php @@ -250,13 +250,13 @@ public function testHeaderContentLengthNotSharedBetweenClients() public function testOptionsDelay() { + $request = $this->getRequest(); + $this->assertSame(0.0, $request->getDelay()); + $options = [ 'delay' => 2000, 'headers' => ['fruit' => 'apple'], ]; - $request = $this->getRequest(); - $this->assertSame(0.0, $request->getDelay()); - $request = $this->getRequest($options); $this->assertSame(2.0, $request->getDelay()); } @@ -698,20 +698,20 @@ public function testSendWithDelay() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->get('products'); // we still need to check the code coverage to make sure this was done - $this->assertSame(1.0, $request->getDelay()); + $this->assertSame(0.1, $request->getDelay()); } public function testSendContinued() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->setOutput("HTTP/1.1 100 Continue\x0d\x0a\x0d\x0aHi there"); @@ -726,7 +726,7 @@ public function testSendContinuedWithManyHeaders() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $output = "HTTP/1.1 100 Continue @@ -770,7 +770,7 @@ public function testSplitResponse() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->setOutput("Accept: text/html\x0d\x0a\x0d\x0aHi there"); @@ -782,7 +782,7 @@ public function testApplyBody() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->setBody('name=George'); @@ -797,7 +797,7 @@ public function testResponseHeaders() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->setOutput("HTTP/2.0 234 Ohoh\x0d\x0aAccept: text/html\x0d\x0a\x0d\x0aHi there"); @@ -811,7 +811,7 @@ public function testResponseHeadersShortProtocol() { $request = $this->getRequest([ 'base_uri' => 'http://www.foo.com/api/v1/', - 'delay' => 1000, + 'delay' => 100, ]); $request->setOutput("HTTP/2 235 Ohoh\x0d\x0aAccept: text/html\x0d\x0a\x0d\x0aHi there shortie");