Skip to content

Commit

Permalink
Adapt tests cases to the new client interface, #45
Browse files Browse the repository at this point in the history
  • Loading branch information
hollodotme committed Jun 22, 2019
1 parent 2cd6069 commit bb90dde
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 105 deletions.
28 changes: 12 additions & 16 deletions tests/Integration/AsyncRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testAsyncRequestsWillRespondToCallbackIfRequestsExceedPhpFpmMaxC

$this->assertTrue( $limit > 5 );

$client = $this->getClientWithNetworkSocket();
$client = new Client();
$results = [];
$expectedResults = range( 0, $limit - 1 );

Expand All @@ -58,7 +58,7 @@ static function ( ProvidesResponseData $response ) use ( &$results )
{
$request->setContent( http_build_query( ['test-key' => $i] ) );

$client->sendAsyncRequest( $request );
$client->sendAsyncRequest( $this->getNetworkSocketConnection(), $request );
}

$client->waitForResponses();
Expand All @@ -78,14 +78,12 @@ private function getMaxChildrenSettingFromNetworkSocket() : int
return (int)$iniSettings['network']['pm.max_children'];
}

private function getClientWithNetworkSocket() : Client
private function getNetworkSocketConnection() : NetworkSocket
{
$networkSocket = new NetworkSocket(
return new NetworkSocket(
$this->getNetworkSocketHost(),
$this->getNetworkSocketPort()
);

return new Client( $networkSocket );
}

/**
Expand All @@ -105,7 +103,7 @@ public function testAsyncRequestsWillRespondToCallbackIfRequestsExceedPhpFpmMaxC

$this->assertTrue( $limit > 5 );

$client = $this->getClientWithUnixDomainSocket();
$client = new Client();
$results = [];
$expectedResults = range( 0, $limit - 1 );

Expand All @@ -121,7 +119,7 @@ static function ( ProvidesResponseData $response ) use ( &$results )
{
$request->setContent( http_build_query( ['test-key' => $i] ) );

$client->sendAsyncRequest( $request );
$client->sendAsyncRequest( $this->getUnixDomainSocketConnection(), $request );
}

$client->waitForResponses();
Expand All @@ -141,11 +139,9 @@ private function getMaxChildrenSettingFromUnixDomainSocket() : int
return (int)$iniSettings['uds']['pm.max_children'];
}

private function getClientWithUnixDomainSocket() : Client
private function getUnixDomainSocketConnection() : UnixDomainSocket
{
$unixDomainSocket = new UnixDomainSocket( $this->getUnixDomainSocket() );

return new Client( $unixDomainSocket );
return new UnixDomainSocket( $this->getUnixDomainSocket() );
}

/**
Expand All @@ -163,7 +159,7 @@ public function testCanReadResponsesOfAsyncRequestsIfRequestsExceedPhpFpmMaxChil

$this->assertTrue( $limit > 5 );

$client = $this->getClientWithNetworkSocket();
$client = new Client();
$results = [];
$expectedResults = range( 0, $limit - 1 );

Expand All @@ -173,7 +169,7 @@ public function testCanReadResponsesOfAsyncRequestsIfRequestsExceedPhpFpmMaxChil
{
$request->setContent( http_build_query( ['test-key' => $i] ) );

$client->sendAsyncRequest( $request );
$client->sendAsyncRequest( $this->getNetworkSocketConnection(), $request );
}

while ( $client->hasUnhandledResponses() )
Expand Down Expand Up @@ -205,7 +201,7 @@ public function testCanReadResponsesOfAsyncRequestsIfRequestsExceedPhpFpmMaxChil

$this->assertTrue( $limit > 5 );

$client = $this->getClientWithUnixDomainSocket();
$client = new Client();
$results = [];
$expectedResults = range( 0, $limit - 1 );

Expand All @@ -221,7 +217,7 @@ static function ( ProvidesResponseData $response ) use ( &$results )
{
$request->setContent( http_build_query( ['test-key' => $i] ) );

$client->sendAsyncRequest( $request );
$client->sendAsyncRequest( $this->getUnixDomainSocketConnection(), $request );
}

while ( $client->hasUnhandledResponses() )
Expand Down
59 changes: 31 additions & 28 deletions tests/Integration/NetworkSocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,22 @@ final class NetworkSocketTest extends TestCase
{
use SocketDataProviding;

/** @var NetworkSocket */
private $connection;

/** @var Client */
private $client;

protected function setUp() : void
{
$connection = new NetworkSocket( $this->getNetworkSocketHost(), $this->getNetworkSocketPort() );
$this->client = new Client( $connection );
$this->connection = new NetworkSocket( $this->getNetworkSocketHost(), $this->getNetworkSocketPort() );
$this->client = new Client();
}

protected function tearDown() : void
{
$this->client = null;
$this->connection = null;
$this->client = null;
}

/**
Expand All @@ -71,7 +75,7 @@ public function testCanSendAsyncRequestAndReceiveRequestId() : void
$content = http_build_query( ['test-key' => 'unit'] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );

$requestId = $this->client->sendAsyncRequest( $request );
$requestId = $this->client->sendAsyncRequest( $this->connection, $request );

$this->assertGreaterThanOrEqual( 1, $requestId );
$this->assertLessThanOrEqual( 65535, $requestId );
Expand All @@ -91,7 +95,7 @@ public function testCanSendAsyncRequestAndReadResponse() : void
$expectedResponse =
"X-Powered-By: PHP/7.1.0\r\nX-Custom: Header\r\nContent-type: text/html; charset=UTF-8\r\n\r\nunit";

$requestId = $this->client->sendAsyncRequest( $request );
$requestId = $this->client->sendAsyncRequest( $this->connection, $request );
$response = $this->client->readResponse( $requestId );

$this->assertEquals( $expectedResponse, $response->getOutput() );
Expand All @@ -114,7 +118,7 @@ public function testCanSendSyncRequestAndReceiveResponse() : void
$expectedResponse =
"X-Powered-By: PHP/7.1.0\r\nX-Custom: Header\r\nContent-type: text/html; charset=UTF-8\r\n\r\nunit";

$response = $this->client->sendRequest( $request );
$response = $this->client->sendRequest( $this->connection, $request );

$this->assertEquals( $expectedResponse, $response->getOutput() );
$this->assertSame( 'unit', $response->getBody() );
Expand Down Expand Up @@ -146,7 +150,7 @@ static function ( ProvidesResponseData $response ) use ( $unitTest )
}
);

$this->client->sendAsyncRequest( $request );
$this->client->sendAsyncRequest( $this->connection, $request );
$this->client->waitForResponses();
}

Expand Down Expand Up @@ -180,7 +184,7 @@ static function ( Throwable $throwable ) use ( $unitTest )
}
);

$this->client->sendAsyncRequest( $request );
$this->client->sendAsyncRequest( $this->connection, $request );
$this->client->waitForResponses();
}

Expand All @@ -197,7 +201,7 @@ public function testCanCheckForRequestIdsHavingResponses() : void
$content = http_build_query( ['test-key' => 'unit'] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );

$requestId = $this->client->sendAsyncRequest( $request );
$requestId = $this->client->sendAsyncRequest( $this->connection, $request );

usleep( 60000 );

Expand All @@ -216,11 +220,11 @@ public function testCanReadResponses() : void
$content = http_build_query( ['test-key' => 'unit'] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );

$requestIdOne = $this->client->sendAsyncRequest( $request );
$requestIdOne = $this->client->sendAsyncRequest( $this->connection, $request );

$request->setContent( http_build_query( ['test-key' => 'test'] ) );

$requestIdTwo = $this->client->sendAsyncRequest( $request );
$requestIdTwo = $this->client->sendAsyncRequest( $this->connection, $request );

usleep( 110000 );

Expand Down Expand Up @@ -257,11 +261,10 @@ public function testReadingSyncResponseCanTimeOut() : void
Defaults::CONNECT_TIMEOUT,
100
);
$client = new Client( $connection );
$content = http_build_query( ['test-key' => 'unit'] );
$request = new PostRequest( __DIR__ . '/Workers/sleepWorker.php', $content );

$response = $client->sendRequest( $request );
$response = $this->client->sendRequest( $connection, $request );

$this->assertSame( 'unit - 0', $response->getBody() );

Expand All @@ -271,7 +274,7 @@ public function testReadingSyncResponseCanTimeOut() : void
$this->expectException( TimedoutException::class );

/** @noinspection UnusedFunctionResultInspection */
$client->sendRequest( $request );
$this->client->sendRequest( $connection, $request );
}

/**
Expand All @@ -295,7 +298,7 @@ static function ( ProvidesResponseData $response ) use ( $unitTest )
}
);

$this->client->sendAsyncRequest( $request );
$this->client->sendAsyncRequest( $this->connection, $request );

while ( $this->client->hasUnhandledResponses() )
{
Expand All @@ -314,7 +317,7 @@ public function testCanReadReadyResponses() : void
$content = http_build_query( ['test-key' => 'unit'] );
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );

$this->client->sendAsyncRequest( $request );
$this->client->sendAsyncRequest( $this->connection, $request );

while ( $this->client->hasUnhandledResponses() )
{
Expand Down Expand Up @@ -348,7 +351,7 @@ static function ( ProvidesResponseData $response ) use ( $unitTest )
}
);

$requestId = $this->client->sendAsyncRequest( $request );
$requestId = $this->client->sendAsyncRequest( $this->connection, $request );

$this->client->waitForResponse( $requestId );
}
Expand All @@ -367,7 +370,7 @@ public function testReadResponsesSkipsUnknownRequestIds() : void
$request = new PostRequest( __DIR__ . '/Workers/worker.php', $content );

$requestIds = [];
$requestIds[] = $this->client->sendAsyncRequest( $request );
$requestIds[] = $this->client->sendAsyncRequest( $this->connection, $request );
$requestIds[] = 12345;

sleep( 1 );
Expand Down Expand Up @@ -422,7 +425,7 @@ static function ( $buffer ) use ( $unitTest, $data, &$passCounter )
}
);

$this->client->sendAsyncRequest( $request );
$this->client->sendAsyncRequest( $this->connection, $request );
$this->client->waitForResponses();
}

Expand All @@ -439,7 +442,7 @@ public function testCanGetLengthOfSentContent( int $length ) : void
$content = str_repeat( 'a', $length );
$request = new PostRequest( __DIR__ . '/Workers/lengthWorker.php', $content );
$request->setContentType( '*/*' );
$result = $this->client->sendRequest( $request );
$result = $this->client->sendRequest( $this->connection, $request );

$this->assertEquals( $length, $result->getBody() );
}
Expand Down Expand Up @@ -497,7 +500,7 @@ public function testRequestingAnUnknownScriptPathThrowsException( string $script
{
$request = new GetRequest( $scriptFilename, '' );

$response = $this->client->sendRequest( $request );
$response = $this->client->sendRequest( $this->connection, $request );

$this->assertSame( '404 Not Found', $response->getHeaderLine( 'Status' ) );
$this->assertSame( "File not found.\n", $response->getBody() );
Expand Down Expand Up @@ -529,7 +532,7 @@ public function testNotAllowedFileNameExtensionRespondsWithAccessDeniedHeader()
{
$request = new GetRequest( __DIR__ . '/Workers/worker.php7', '' );

$response = $this->client->sendRequest( $request );
$response = $this->client->sendRequest( $this->connection, $request );

$this->assertSame( '403 Forbidden', $response->getHeaderLine( 'Status' ) );
$this->assertRegExp(
Expand All @@ -554,7 +557,7 @@ public function testUnaccessibleScriptRespondsWithAccessDeniedHeader() : void
$this->makeFileUnaccessible( $scriptPath );

$request = new GetRequest( $scriptPath, '' );
$response = $this->client->sendRequest( $request );
$response = $this->client->sendRequest( $this->connection, $request );

$this->assertSame( '403 Forbidden', $response->getHeaderLine( 'Status' ) );
$this->assertRegExp(
Expand Down Expand Up @@ -601,7 +604,7 @@ static function (
}
);

$requestId = $this->client->sendAsyncRequest( $request );
$requestId = $this->client->sendAsyncRequest( $this->connection, $request );
$this->client->handleResponse( $requestId );

$this->expectOutputRegex( $expectecOutputRegExp );
Expand All @@ -625,7 +628,7 @@ static function ( ProvidesResponseData $response ) use ( $unitTest )
}
);

$requestId = $this->client->sendAsyncRequest( $request );
$requestId = $this->client->sendAsyncRequest( $this->connection, $request );
$this->client->handleResponse( $requestId );
}

Expand All @@ -640,7 +643,7 @@ static function ( ProvidesResponseData $response ) use ( $unitTest )
public function testCanGetErrorOutputFromWorkerUsingErrorLog() : void
{
$request = new GetRequest( __DIR__ . '/Workers/errorLogWorker.php', '' );
$response = $this->client->sendRequest( $request );
$response = $this->client->sendRequest( $this->connection, $request );

$expectedError = "#^PHP message: ERROR1\n\n?"
. "PHP message: ERROR2\n\n?"
Expand All @@ -663,12 +666,12 @@ public function testSuccessiveRequestsShouldUseSameSocket() : void
{
$request = new GetRequest( __DIR__ . '/Workers/sleepWorker.php', '' );

$requestId = $this->client->sendRequest( $request )->getRequestId();
$requestId = $this->client->sendRequest( $this->connection, $request )->getRequestId();

$requestIds = [];
for ( $i = 0; $i < 5; $i++ )
{
$requestIds[] = $this->client->sendRequest( $request )->getRequestId();
$requestIds[] = $this->client->sendRequest( $this->connection, $request )->getRequestId();
}

$expectedRequestIds = [$requestId, $requestId, $requestId, $requestId, $requestId];
Expand Down
Loading

0 comments on commit bb90dde

Please sign in to comment.