From cfd09daa81a93550c0d9498d0b51a7f674a84d1a Mon Sep 17 00:00:00 2001 From: Sander De la Marche Date: Mon, 27 Mar 2023 17:19:10 +0200 Subject: [PATCH 1/8] Update old mentions of newWithRequestContent --- README.md | 6 +++--- tests/Integration/FileUpload/FileUploadTest.php | 2 +- tests/Unit/Requests/DeleteRequestTest.php | 2 +- tests/Unit/Requests/GetRequestTest.php | 2 +- tests/Unit/Requests/PatchRequestTest.php | 2 +- tests/Unit/Requests/PostRequestTest.php | 2 +- tests/Unit/Requests/PutRequestTest.php | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index ac226b7..11b79bf 100644 --- a/README.md +++ b/README.md @@ -656,7 +656,7 @@ $urlEncodedContent = new UrlEncodedFormData( ] ); -$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $urlEncodedContent ); +$postRequest = new PostRequest( '/path/to/target/script.php', $urlEncodedContent ); $response = $client->sendRequest( $connection, $postRequest ); ``` @@ -713,7 +713,7 @@ $multipartContent = new MultipartFormData( ] ); -$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $multipartContent ); +$postRequest = new PostRequest( '/path/to/target/script.php', $multipartContent ); $response = $client->sendRequest( $connection, $postRequest ); ``` @@ -812,7 +812,7 @@ $jsonContent = new JsonData( ] ); -$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $jsonContent ); +$postRequest = new PostRequest( '/path/to/target/script.php', $jsonContent ); $response = $client->sendRequest( $connection, $postRequest ); ``` diff --git a/tests/Integration/FileUpload/FileUploadTest.php b/tests/Integration/FileUpload/FileUploadTest.php index bf45c16..4ab4b65 100644 --- a/tests/Integration/FileUpload/FileUploadTest.php +++ b/tests/Integration/FileUpload/FileUploadTest.php @@ -61,7 +61,7 @@ public function testCanUploadFiles( array $files ) : void ]; $multipartFormData = new MultipartFormData( $formData, $files ); - $postRequest = PostRequest::newWithRequestContent( + $postRequest = new PostRequest( dirname( __DIR__ ) . '/Workers/fileUploadWorker.php', $multipartFormData ); diff --git a/tests/Unit/Requests/DeleteRequestTest.php b/tests/Unit/Requests/DeleteRequestTest.php index cb99872..50b52d5 100644 --- a/tests/Unit/Requests/DeleteRequestTest.php +++ b/tests/Unit/Requests/DeleteRequestTest.php @@ -34,7 +34,7 @@ public function testCanCreateInstanceWithRequestContent() : void ] ); - $request = DeleteRequest::newWithRequestContent( '/path/to/script.php', $urlEncodedContent ); + $request = new DeleteRequest( '/path/to/script.php', $urlEncodedContent ); self::assertSame( 'application/x-www-form-urlencoded', $request->getContentType() ); self::assertSame( 'unit=test&test=unit', $request->getContent() ); diff --git a/tests/Unit/Requests/GetRequestTest.php b/tests/Unit/Requests/GetRequestTest.php index 76f708c..2b002c6 100644 --- a/tests/Unit/Requests/GetRequestTest.php +++ b/tests/Unit/Requests/GetRequestTest.php @@ -34,7 +34,7 @@ public function testCanCreateInstanceWithRequestContent() : void ] ); - $request = GetRequest::newWithRequestContent( '/path/to/script.php', $urlEncodedContent ); + $request = new GetRequest( '/path/to/script.php', $urlEncodedContent ); self::assertSame( 'application/x-www-form-urlencoded', $request->getContentType() ); self::assertSame( 'unit=test&test=unit', $request->getContent() ); diff --git a/tests/Unit/Requests/PatchRequestTest.php b/tests/Unit/Requests/PatchRequestTest.php index fdd52a3..b3b542a 100644 --- a/tests/Unit/Requests/PatchRequestTest.php +++ b/tests/Unit/Requests/PatchRequestTest.php @@ -34,7 +34,7 @@ public function testCanCreateInstanceWithRequestContent() : void ] ); - $request = PatchRequest::newWithRequestContent( '/path/to/script.php', $urlEncodedContent ); + $request = new PatchRequest( '/path/to/script.php', $urlEncodedContent ); self::assertSame( 'application/x-www-form-urlencoded', $request->getContentType() ); self::assertSame( 'unit=test&test=unit', $request->getContent() ); diff --git a/tests/Unit/Requests/PostRequestTest.php b/tests/Unit/Requests/PostRequestTest.php index df5fce9..088ebe9 100644 --- a/tests/Unit/Requests/PostRequestTest.php +++ b/tests/Unit/Requests/PostRequestTest.php @@ -34,7 +34,7 @@ public function testCanCreateInstanceWithRequestContent() : void ] ); - $request = PostRequest::newWithRequestContent( '/path/to/script.php', $urlEncodedContent ); + $request = new PostRequest( '/path/to/script.php', $urlEncodedContent ); self::assertSame( 'application/x-www-form-urlencoded', $request->getContentType() ); self::assertSame( 'unit=test&test=unit', $request->getContent() ); diff --git a/tests/Unit/Requests/PutRequestTest.php b/tests/Unit/Requests/PutRequestTest.php index d042e6d..108e510 100644 --- a/tests/Unit/Requests/PutRequestTest.php +++ b/tests/Unit/Requests/PutRequestTest.php @@ -34,7 +34,7 @@ public function testCanCreateInstanceWithRequestContent() : void ] ); - $request = PutRequest::newWithRequestContent( '/path/to/script.php', $urlEncodedContent ); + $request = new PutRequest( '/path/to/script.php', $urlEncodedContent ); self::assertSame( 'application/x-www-form-urlencoded', $request->getContentType() ); self::assertSame( 'unit=test&test=unit', $request->getContent() ); From 7d3c0676d8b3e4de8e1fa44309bfe5a359fcf3ba Mon Sep 17 00:00:00 2001 From: Sander De la Marche Date: Mon, 27 Mar 2023 17:25:00 +0200 Subject: [PATCH 2/8] Update all old cases of the AbstractRequest constructor Passing a string to any Request is deprecated in 4.x --- tests/Integration/NetworkSocket/NetworkSocketTest.php | 2 +- .../UnixDomainSocket/UnixDomainSocketTest.php | 2 +- tests/Unit/Requests/DeleteRequestTest.php | 2 +- tests/Unit/Requests/GetRequestTest.php | 2 +- tests/Unit/Requests/PatchRequestTest.php | 2 +- tests/Unit/Requests/PostRequestTest.php | 2 +- tests/Unit/Requests/PutRequestTest.php | 2 +- tests/Unit/Sockets/SocketCollectionTest.php | 8 +++----- tests/Unit/Sockets/SocketTest.php | 11 ++++++----- 9 files changed, 16 insertions(+), 17 deletions(-) diff --git a/tests/Integration/NetworkSocket/NetworkSocketTest.php b/tests/Integration/NetworkSocket/NetworkSocketTest.php index abfe6ff..cfabc49 100644 --- a/tests/Integration/NetworkSocket/NetworkSocketTest.php +++ b/tests/Integration/NetworkSocket/NetworkSocketTest.php @@ -418,7 +418,7 @@ static function ( $buffer ) use ( $unitTest, $data, &$passCounter ) */ public function testCanGetLengthOfSentContent( int $length ) : void { - $content = str_repeat( 'a', $length ); + $content = new UrlEncodedFormData(['test' => str_repeat( 'a', $length )]); $request = new PostRequest( $this->getWorkerPath( 'lengthWorker.php' ), $content ); $request->setContentType( '*/*' ); $result = $this->client->sendRequest( $this->connection, $request ); diff --git a/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php b/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php index 8a6de77..995523f 100644 --- a/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php +++ b/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php @@ -419,7 +419,7 @@ static function ( $buffer ) use ( $unitTest, $data, &$passCounter ) */ public function testCanGetLengthOfSentContent( int $length ) : void { - $content = str_repeat( 'a', $length ); + $content = new UrlEncodedFormData(['test' => str_repeat( 'a', $length )]); $request = new PostRequest( $this->getWorkerPath( 'lengthWorker.php' ), $content ); $response = $this->client->sendRequest( $this->connection, $request ); diff --git a/tests/Unit/Requests/DeleteRequestTest.php b/tests/Unit/Requests/DeleteRequestTest.php index 50b52d5..67f650e 100644 --- a/tests/Unit/Requests/DeleteRequestTest.php +++ b/tests/Unit/Requests/DeleteRequestTest.php @@ -16,7 +16,7 @@ final class DeleteRequestTest extends TestCase */ public function testRequestMethodIsGet() : void { - $request = new DeleteRequest( '/path/to/script.php', 'Unit-Test' ); + $request = new DeleteRequest( '/path/to/script.php' ); self::assertSame( 'DELETE', $request->getRequestMethod() ); } diff --git a/tests/Unit/Requests/GetRequestTest.php b/tests/Unit/Requests/GetRequestTest.php index 2b002c6..5ab597c 100644 --- a/tests/Unit/Requests/GetRequestTest.php +++ b/tests/Unit/Requests/GetRequestTest.php @@ -16,7 +16,7 @@ final class GetRequestTest extends TestCase */ public function testRequestMethodIsGet() : void { - $request = new GetRequest( '/path/to/script.php', 'Unit-Test' ); + $request = new GetRequest( '/path/to/script.php' ); self::assertSame( 'GET', $request->getRequestMethod() ); } diff --git a/tests/Unit/Requests/PatchRequestTest.php b/tests/Unit/Requests/PatchRequestTest.php index b3b542a..8be05f0 100644 --- a/tests/Unit/Requests/PatchRequestTest.php +++ b/tests/Unit/Requests/PatchRequestTest.php @@ -16,7 +16,7 @@ final class PatchRequestTest extends TestCase */ public function testRequestMethodIsGet() : void { - $request = new PatchRequest( '/path/to/script.php', 'Unit-Test' ); + $request = new PatchRequest( '/path/to/script.php' ); self::assertSame( 'PATCH', $request->getRequestMethod() ); } diff --git a/tests/Unit/Requests/PostRequestTest.php b/tests/Unit/Requests/PostRequestTest.php index 088ebe9..61b764d 100644 --- a/tests/Unit/Requests/PostRequestTest.php +++ b/tests/Unit/Requests/PostRequestTest.php @@ -16,7 +16,7 @@ final class PostRequestTest extends TestCase */ public function testRequestMethodIsPost() : void { - $request = new PostRequest( '/path/to/script.php', 'Unit-Test' ); + $request = new PostRequest( '/path/to/script.php' ); self::assertSame( 'POST', $request->getRequestMethod() ); } diff --git a/tests/Unit/Requests/PutRequestTest.php b/tests/Unit/Requests/PutRequestTest.php index 108e510..17f2738 100644 --- a/tests/Unit/Requests/PutRequestTest.php +++ b/tests/Unit/Requests/PutRequestTest.php @@ -16,7 +16,7 @@ final class PutRequestTest extends TestCase */ public function testRequestMethodIsPut() : void { - $request = new PutRequest( '/path/to/script.php', 'Unit-Test' ); + $request = new PutRequest( '/path/to/script.php' ); self::assertSame( 'PUT', $request->getRequestMethod() ); } diff --git a/tests/Unit/Sockets/SocketCollectionTest.php b/tests/Unit/Sockets/SocketCollectionTest.php index 9529586..09ca340 100644 --- a/tests/Unit/Sockets/SocketCollectionTest.php +++ b/tests/Unit/Sockets/SocketCollectionTest.php @@ -246,9 +246,7 @@ public function testSocketWithResponseIsIdle() : void ); $request = new PostRequest( - dirname( __DIR__, 2 ) . '/Integration/Workers/sleepWorker.php', - '' - ); + dirname( __DIR__, 2 ) . '/Integration/Workers/sleepWorker.php' ); $socket->sendRequest( $request ); /** @noinspection UnusedFunctionResultInspection */ @@ -281,7 +279,7 @@ public function testBusySocketIsNotIdle() : void ); $socket->sendRequest( - new PostRequest( '/some/script.php', '' ) + new PostRequest( '/some/script.php' ) ); self::assertNull( $this->collection->getIdleSocket( $connection ) ); @@ -487,7 +485,7 @@ public function testHasBusySockets() : void ); $socket->sendRequest( - new PostRequest( '/some/sctipt.php', '' ) + new PostRequest( '/some/sctipt.php' ) ); self::assertTrue( $this->collection->hasBusySockets() ); diff --git a/tests/Unit/Sockets/SocketTest.php b/tests/Unit/Sockets/SocketTest.php index 737acc3..f67ae27 100644 --- a/tests/Unit/Sockets/SocketTest.php +++ b/tests/Unit/Sockets/SocketTest.php @@ -10,6 +10,7 @@ use hollodotme\FastCGI\Exceptions\TimedoutException; use hollodotme\FastCGI\Exceptions\WriteFailedException; use hollodotme\FastCGI\Interfaces\ProvidesResponseData; +use hollodotme\FastCGI\RequestContents\UrlEncodedFormData; use hollodotme\FastCGI\Requests\PostRequest; use hollodotme\FastCGI\SocketConnections\Defaults; use hollodotme\FastCGI\SocketConnections\NetworkSocket; @@ -79,7 +80,7 @@ public function testCanSendRequestAndFetchResponse() : void $data = ['test-key' => 'unit']; $request = new PostRequest( dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php', - http_build_query( $data ) + new UrlEncodedFormData( $data ) ); $socket->sendRequest( $request ); @@ -108,7 +109,7 @@ public function testCanCollectResource() : void $data = ['test-key' => 'unit']; $request = new PostRequest( dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php', - http_build_query( $data ) + new UrlEncodedFormData( $data ) ); $socket->collectResource( $resources ); @@ -135,7 +136,7 @@ public function testCanNotifyResponseCallback() : void $data = ['test-key' => 'unit']; $request = new PostRequest( dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php', - http_build_query( $data ) + new UrlEncodedFormData( $data ) ); $request->addResponseCallbacks( static function ( ProvidesResponseData $response ) @@ -163,7 +164,7 @@ public function testCanNotifyFailureCallback() : void $data = ['test-key' => 'unit']; $request = new PostRequest( dirname( __DIR__, 2 ) . '/Integration/Workers/worker.php', - http_build_query( $data ) + new UrlEncodedFormData( $data ) ); $request->addFailureCallbacks( static function ( Throwable $throwable ) @@ -284,7 +285,7 @@ public function testIsUsableWhenInInitialState() : void public function testIsNotUsableWhenTimedOut() : void { $socket = $this->getSocket(); - $content = http_build_query( ['sleep' => 1, 'test-key' => 'unit'] ); + $content = new UrlEncodedFormData( ['sleep' => 1, 'test-key' => 'unit'] ); $request = new PostRequest( dirname( __DIR__, 2 ) . '/Integration/Workers/sleepWorker.php', $content ); $socket->sendRequest( $request ); From d6496c48ba1bddd43a08d054a1e98f7e46948237 Mon Sep 17 00:00:00 2001 From: Sander De la Marche Date: Mon, 27 Mar 2023 17:29:41 +0200 Subject: [PATCH 3/8] Fix the AbstractQuery test --- tests/Unit/Requests/AbstractRequestTest.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/tests/Unit/Requests/AbstractRequestTest.php b/tests/Unit/Requests/AbstractRequestTest.php index 9d75c7c..db713a8 100644 --- a/tests/Unit/Requests/AbstractRequestTest.php +++ b/tests/Unit/Requests/AbstractRequestTest.php @@ -2,6 +2,7 @@ namespace hollodotme\FastCGI\Tests\Unit\Requests; +use hollodotme\FastCGI\Interfaces\ComposesRequestContent; use hollodotme\FastCGI\RequestContents\UrlEncodedFormData; use hollodotme\FastCGI\Requests\AbstractRequest; use PHPUnit\Framework\ExpectationFailedException; @@ -41,19 +42,17 @@ public function testCanGetDefaultValues( string $requestMethod ) : void /** * @param string $requestMethod * @param string $scriptFilename - * @param string $content * * @return AbstractRequest */ - private function getRequest( string $requestMethod, string $scriptFilename ) : AbstractRequest + private function getRequest( string $requestMethod, string $scriptFilename, ?ComposesRequestContent $content = null ) : AbstractRequest { - return new class($requestMethod, $scriptFilename) extends AbstractRequest { - /** @var string */ - private $requestMethod; + return new class($requestMethod, $scriptFilename, $content) extends AbstractRequest { + private string $requestMethod; - public function __construct( string $requestMethod, string $scriptFilename ) + public function __construct( string $requestMethod, string $scriptFilename, ?ComposesRequestContent $content = null ) { - parent::__construct( $scriptFilename ); + parent::__construct( $scriptFilename, $content ); $this->requestMethod = $requestMethod; } @@ -97,7 +96,7 @@ public function requestMethodProvider() : array */ public function testCanGetParametersArray( string $requestMethod ) : void { - $request = $this->getRequest( $requestMethod, '/path/to/script.php', 'Unit-Test' ); + $request = $this->getRequest( $requestMethod, '/path/to/script.php', new UrlEncodedFormData(['test' => 'unit']) ); $request->setCustomVar( 'UNIT', 'Test' ); $request->setRequestUri( '/unit/test/' ); @@ -142,7 +141,7 @@ public function testContentLengthChangesWithContent() : void */ public function testCanOverwriteVars() : void { - $request = $this->getRequest( 'POST', '/path/to/script.php', 'Unit-Test' ); + $request = $this->getRequest( 'POST', '/path/to/script.php', new UrlEncodedFormData(['test' => 'unit']) ); $request->setRemoteAddress( '10.100.10.1' ); $request->setRemotePort( 8599 ); $request->setServerSoftware( 'unit/test' ); @@ -185,7 +184,7 @@ public function testCanOverwriteVars() : void */ public function testCanResetCustomVars() : void { - $request = $this->getRequest( 'POST', '/path/to/script.php', 'Unit-Test' ); + $request = $this->getRequest( 'POST', '/path/to/script.php', new UrlEncodedFormData(['test' => 'unit']) ); $request->setCustomVar( 'UNIT', 'Test' ); self::assertSame( ['UNIT' => 'Test'], $request->getCustomVars() ); From 02ff204237a09bf7608d5f7e6f64fb56a07ecdf3 Mon Sep 17 00:00:00 2001 From: Sander De la Marche Date: Mon, 27 Mar 2023 18:11:18 +0200 Subject: [PATCH 4/8] Make sure the content is always set, even if null --- src/Requests/AbstractRequest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Requests/AbstractRequest.php b/src/Requests/AbstractRequest.php index 01523c3..e345a35 100644 --- a/src/Requests/AbstractRequest.php +++ b/src/Requests/AbstractRequest.php @@ -54,9 +54,10 @@ abstract class AbstractRequest implements ProvidesRequestData public function __construct( string $scriptFilename, ?ComposesRequestContent $content = null ) { $this->scriptFilename = $scriptFilename; + $this->content = $content; if (null !== $content) { - $this->setContent( $content ); + $this->contentLength = strlen( $content->getContent() ); $this->setContentType( $content->getContentType() ); } } @@ -148,7 +149,7 @@ public function getContent() : ?ComposesRequestContent public function setContent( ComposesRequestContent $content ) : void { - $this->content = $content; + $this->content = $content; $this->contentLength = strlen( $content->getContent() ); } From aa4980d2e26a00219a0f956d71a5c1c0be43c21e Mon Sep 17 00:00:00 2001 From: Sander De la Marche Date: Mon, 27 Mar 2023 18:11:30 +0200 Subject: [PATCH 5/8] Adjust the content lenght test to factor in the 'test' key of the array --- tests/Integration/NetworkSocket/NetworkSocketTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/NetworkSocket/NetworkSocketTest.php b/tests/Integration/NetworkSocket/NetworkSocketTest.php index cfabc49..b613496 100644 --- a/tests/Integration/NetworkSocket/NetworkSocketTest.php +++ b/tests/Integration/NetworkSocket/NetworkSocketTest.php @@ -423,7 +423,7 @@ public function testCanGetLengthOfSentContent( int $length ) : void $request->setContentType( '*/*' ); $result = $this->client->sendRequest( $this->connection, $request ); - self::assertEquals( $length, $result->getBody() ); + self::assertEquals( $length + 5, $result->getBody() ); } /** From 2129c09e23f9e0177f5a0214735060fe2f343d85 Mon Sep 17 00:00:00 2001 From: Sander De la Marche Date: Tue, 28 Mar 2023 15:20:38 +0200 Subject: [PATCH 6/8] Remove the multi-request stream_select call from Client This method had a bug where, as long as no error was thrown, or timeout reached, it would report that all sockets inside a collection had responses even if some of them did not. Checking each socket individually in a loop is a performance hit, but the only way to report accuratly if a socket has a response ready. --- src/Client.php | 17 +---------------- src/Sockets/SocketCollection.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/Client.php b/src/Client.php index c9002a3..fbc8bd5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -191,22 +191,7 @@ public function hasResponse( int $socketId ) : bool */ public function getSocketIdsHavingResponse() : array { - if ( $this->sockets->isEmpty() ) - { - return []; - } - - $reads = $this->sockets->collectResources(); - $writes = $excepts = null; - - $result = @stream_select( $reads, $writes, $excepts, 0, Socket::STREAM_SELECT_USEC ); - - if ( false === $result || 0 === count( $reads ) ) - { - return []; - } - - return $this->sockets->getSocketIdsByResources( $reads ); + return $this->sockets->hasResponses(); } /** diff --git a/src/Sockets/SocketCollection.php b/src/Sockets/SocketCollection.php index e9b5af3..46db35c 100644 --- a/src/Sockets/SocketCollection.php +++ b/src/Sockets/SocketCollection.php @@ -199,4 +199,23 @@ public function isEmpty() : bool { return [] === $this->sockets; } + + /** @return array */ + public function hasResponses() : array + { + if ( $this->isEmpty() ) + { + return []; + } + + foreach ( $this->sockets as $socket ) + { + if ( $socket->hasResponse() ) + { + $socketIds[] = $socket->getId(); + } + } + + return $socketIds ?? []; + } } \ No newline at end of file From 8f256cd4131313dc51c42636a4a00f0f3f9de771 Mon Sep 17 00:00:00 2001 From: Sander De la Marche Date: Tue, 28 Mar 2023 15:49:05 +0200 Subject: [PATCH 7/8] Add a new property + setter/getter for response polling timeout to requests This will be passed to the socket as soon as the request is sent. The polling timeout will then be used by the hasResponse method, which uses stream_select --- src/Interfaces/ProvidesRequestData.php | 2 ++ src/Requests/AbstractRequest.php | 12 ++++++++++++ src/Sockets/Socket.php | 5 ++++- .../UnixDomainSocket/UnixDomainSocketTest.php | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Interfaces/ProvidesRequestData.php b/src/Interfaces/ProvidesRequestData.php index 71bdc58..27a14a0 100644 --- a/src/Interfaces/ProvidesRequestData.php +++ b/src/Interfaces/ProvidesRequestData.php @@ -34,6 +34,8 @@ public function getContentLength() : int; public function getContent() : ?ComposesRequestContent; + public function getPollingTimeout() : ?int; + /** * @return array */ diff --git a/src/Requests/AbstractRequest.php b/src/Requests/AbstractRequest.php index e345a35..f136826 100644 --- a/src/Requests/AbstractRequest.php +++ b/src/Requests/AbstractRequest.php @@ -35,6 +35,8 @@ abstract class AbstractRequest implements ProvidesRequestData private int $contentLength = 0; + private ?int $pollingTimeout = null; + private ?ComposesRequestContent $content; /** @var array */ @@ -198,6 +200,16 @@ public function getContentLength() : int return $this->contentLength; } + public function getPollingTimeout(): ?int + { + return $this->pollingTimeout; + } + + public function setPollingTimeout(int $pollingTimeout): void + { + $this->pollingTimeout = $pollingTimeout; + } + /** * @return array */ diff --git a/src/Sockets/Socket.php b/src/Sockets/Socket.php index 7198a15..535e9f2 100644 --- a/src/Sockets/Socket.php +++ b/src/Sockets/Socket.php @@ -100,6 +100,8 @@ final class Socket private int $status; + private int $pollingTimeout; + /** * @param SocketId $socketId * @param ConfiguresSocketConnection $connection @@ -145,7 +147,7 @@ public function hasResponse() : bool $reads = [$this->resource]; $writes = $excepts = null; - return (bool)stream_select( $reads, $writes, $excepts, 0, self::STREAM_SELECT_USEC ); + return (bool)stream_select( $reads, $writes, $excepts, 0, $this->pollingTimeout ); } /** @@ -161,6 +163,7 @@ public function sendRequest( ProvidesRequestData $request ) : void $this->response = null; + $this->pollingTimeout = $request->getPollingTimeout() ?? self::STREAM_SELECT_USEC; $this->responseCallbacks = $request->getResponseCallbacks(); $this->failureCallbacks = $request->getFailureCallbacks(); $this->passThroughCallbacks = $request->getPassThroughCallbacks(); diff --git a/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php b/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php index 995523f..36955ea 100644 --- a/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php +++ b/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php @@ -704,4 +704,22 @@ public function testSuccessiveRequestsShouldUseSameSocket() : void self::assertSame( $firstSocket, $lastSocket ); self::assertCount( 1, $sockets->getValue( $this->client ) ); } + + /** + * @throws Exception + * @throws ConnectException + * @throws TimedoutException + * @throws WriteFailedException + */ + public function testSetCustomPollingTimeout() : void + { + $content = new UrlEncodedFormData( ['test-key' => 'unit'] ); + $request = new PostRequest( $this->getWorkerPath( 'worker.php' ), $content ); + $request->setPollingTimeout(500000); + + $socketId = $this->client->sendAsyncRequest( $this->connection, $request ); + + + self::assertEquals( $socketId, 500000 ); + } } From 299c440d238acfcacff8abe8911743fd4710486e Mon Sep 17 00:00:00 2001 From: Sander De la Marche Date: Tue, 28 Mar 2023 15:50:25 +0200 Subject: [PATCH 8/8] Revert the incomplete test --- .../UnixDomainSocket/UnixDomainSocketTest.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php b/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php index 36955ea..995523f 100644 --- a/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php +++ b/tests/Integration/UnixDomainSocket/UnixDomainSocketTest.php @@ -704,22 +704,4 @@ public function testSuccessiveRequestsShouldUseSameSocket() : void self::assertSame( $firstSocket, $lastSocket ); self::assertCount( 1, $sockets->getValue( $this->client ) ); } - - /** - * @throws Exception - * @throws ConnectException - * @throws TimedoutException - * @throws WriteFailedException - */ - public function testSetCustomPollingTimeout() : void - { - $content = new UrlEncodedFormData( ['test-key' => 'unit'] ); - $request = new PostRequest( $this->getWorkerPath( 'worker.php' ), $content ); - $request->setPollingTimeout(500000); - - $socketId = $this->client->sendAsyncRequest( $this->connection, $request ); - - - self::assertEquals( $socketId, 500000 ); - } }