diff --git a/src/Attribute/HttpTimeout.php b/src/Attribute/HttpTimeout.php new file mode 100644 index 0000000..dbd5e9b --- /dev/null +++ b/src/Attribute/HttpTimeout.php @@ -0,0 +1,13 @@ +httpClient = $this->createHttpClient($this->allowSelfSignedCertificate); + + $httpTimeout = $test->getMethod()->getAttributes(HttpTimeout::class); + + if (!$httpTimeout) { + $httpTimeout = $test->getMethod()->getDeclaringClass()->getAttributes(HttpTimeout::class); + } + + $timeout = 10; + + if ($httpTimeout) { + $timeout = $httpTimeout[0]->newInstance()->timeout; + } + + $this->configureRequest = function (Request $request) use ($timeout) { + $request->setInactivityTimeout($timeout); + $request->setTcpConnectTimeout($timeout); + $request->setTlsHandshakeTimeout($timeout); + $request->setTransferTimeout($timeout); + }; } /** * Allow to test a rejection or a resolution of an async call. */ - final protected function sendRequest(Request $request): HttpResponse + final protected function sendRequest(Request $request): Response { + if ($this->configureRequest !== null) { + $configureRequest = $this->configureRequest; + $configureRequest($request); + } + return $this->httpClient->request($request); } } diff --git a/src/Runner/PoolRunner.php b/src/Runner/PoolRunner.php index 0eb61ce..e00a1fa 100644 --- a/src/Runner/PoolRunner.php +++ b/src/Runner/PoolRunner.php @@ -16,9 +16,6 @@ class PoolRunner { private Semaphore $semaphore; - /** @var object[] */ - private $testCases = []; - /** * @param positive-int $concurrency */ @@ -88,24 +85,18 @@ protected function run(Test $test): void private function getTestCase(Test $test): object { $reflectionClass = $test->getMethod()->getDeclaringClass(); + $testCase = $reflectionClass->newInstance(); - if (!isset($this->testCases[$reflectionClass->getName()])) { - $testCase = $reflectionClass->newInstance(); - - // Find all methods with attribute OnCreate - foreach ($reflectionClass->getMethods() as $reflectionMethod) { - $onCreate = $reflectionMethod->getAttributes(OnCreate::class); + foreach ($reflectionClass->getMethods() as $reflectionMethod) { + $onCreate = $reflectionMethod->getAttributes(OnCreate::class); - if (0 === count($onCreate)) { - continue; - } - - $testCase->{$reflectionMethod->getName()}($test); + if (0 === count($onCreate)) { + continue; } - $this->testCases[$reflectionClass->getName()] = $testCase; + $testCase->{$reflectionMethod->getName()}($test); } - return $this->testCases[$reflectionClass->getName()]; + return $testCase; } } diff --git a/tests/FunctionalHttpTests.php b/tests/FunctionalHttpTests.php index 34acdfc..6e0f30f 100644 --- a/tests/FunctionalHttpTests.php +++ b/tests/FunctionalHttpTests.php @@ -4,6 +4,7 @@ use Amp\Http\HttpResponse; use Asynit\Attribute\Depend; +use Asynit\Attribute\HttpTimeout; use Asynit\Attribute\TestCase; use Asynit\HttpClient\HttpClientWebCaseTrait; @@ -120,6 +121,20 @@ public function test_c_with_d($a, $b, $d) $this->assertSame('d', $d); } + #[HttpTimeout(1)] + public function testTimeout() + { + try { + $this->get($this->createUri('/delay/2')); + $hasException = false; + } catch (\Exception $e) { + $this->assertInstanceOf(\Amp\Http\Client\HttpException::class, $e); + $hasException = true; + } + + $this->assertTrue($hasException); + } + protected function createUri(string $uri): string { return 'http://127.0.0.1:8081'.$uri;