From e26a0960b5994c58c284eb027460beac91e48e3a Mon Sep 17 00:00:00 2001 From: elwafa Date: Sun, 26 Jan 2025 22:13:36 +0200 Subject: [PATCH 1/5] update engine release --- src/Platform.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Platform.php b/src/Platform.php index a5d08a1..46641b1 100644 --- a/src/Platform.php +++ b/src/Platform.php @@ -6,7 +6,7 @@ class Platform { private const BINARY_NAME = 'volt-test'; - private const CURRENT_VERSION = 'v0.0.1-beta'; + private const CURRENT_VERSION = 'v0.0.1'; private const BASE_DOWNLOAD_URL = 'https://github.com/volt-test/binaries/releases/download'; private const SUPPORTED_PLATFORMS = [ 'linux-amd64' => 'volt-test-linux-amd64', From d045d74cd620fb1f18756c35a121837a054aff29 Mon Sep 17 00:00:00 2001 From: elwafa Date: Sun, 26 Jan 2025 22:59:03 +0200 Subject: [PATCH 2/5] instead of throw exception when engine fail display the error --- src/ProcessManager.php | 38 +++++++++++++++++++++--------- tests/Units/ProcessManagerTest.php | 31 ------------------------ 2 files changed, 27 insertions(+), 42 deletions(-) diff --git a/src/ProcessManager.php b/src/ProcessManager.php index 31a1ed8..679f1db 100644 --- a/src/ProcessManager.php +++ b/src/ProcessManager.php @@ -7,13 +7,11 @@ class ProcessManager { private string $binaryPath; - private $currentProcess = null; public function __construct(string $binaryPath) { $this->binaryPath = $binaryPath; - // Enable async signals if (function_exists('pcntl_async_signals')) { pcntl_async_signals(true); pcntl_signal(SIGINT, [$this, 'handleSignal']); @@ -35,7 +33,7 @@ public function execute(array $config, bool $streamOutput): string [$success, $process, $pipes] = $this->openProcess(); $this->currentProcess = $process; - if (! $success || ! is_array($pipes)) { + if (!$success || !is_array($pipes)) { throw new RuntimeException('Failed to start process of volt test'); } @@ -44,7 +42,14 @@ public function execute(array $config, bool $streamOutput): string fclose($pipes[0]); $output = $this->handleProcess($pipes, $streamOutput); - } finally { + + // Store stderr content before closing + $stderrContent = ''; + if (isset($pipes[2]) && is_resource($pipes[2])) { + rewind($pipes[2]); + $stderrContent = stream_get_contents($pipes[2]); + } + // Clean up pipes foreach ($pipes as $pipe) { if (is_resource($pipe)) { @@ -56,12 +61,23 @@ public function execute(array $config, bool $streamOutput): string $exitCode = $this->closeProcess($process); $this->currentProcess = null; if ($exitCode !== 0) { - throw new RuntimeException('Process failed with exit code ' . $exitCode); + echo "\nError: " . trim($stderrContent) . "\n"; + return ''; } } - } - return $output; + return $output; + } finally { + foreach ($pipes as $pipe) { + if (is_resource($pipe)) { + fclose($pipe); + } + } + if (is_resource($process)) { + $this->closeProcess($process); + $this->currentProcess = null; + } + } } protected function openProcess(): array @@ -80,7 +96,7 @@ protected function openProcess(): array ['bypass_shell' => true] ); - if (! is_resource($process)) { + if (!is_resource($process)) { return [false, null, []]; } @@ -112,7 +128,6 @@ private function handleProcess(array $pipes, bool $streamOutput): string if (feof($pipe)) { fclose($pipe); unset($pipes[$type]); - continue; } } @@ -140,7 +155,7 @@ protected function writeInput($pipe, string $input): void protected function closeProcess($process): int { - if (! is_resource($process)) { + if (!is_resource($process)) { return -1; } @@ -149,6 +164,7 @@ protected function closeProcess($process): int proc_terminate($process); } + return proc_close($process); } -} +} \ No newline at end of file diff --git a/tests/Units/ProcessManagerTest.php b/tests/Units/ProcessManagerTest.php index 38bf3b1..9e5a989 100644 --- a/tests/Units/ProcessManagerTest.php +++ b/tests/Units/ProcessManagerTest.php @@ -63,37 +63,6 @@ public static function outputProvider(): array ]; } - #[DataProvider('errorScenarioProvider')] - public function testErrorScenarios( - bool $failStart, - int $exitCode, - string $errorOutput, - string $expectedMessage - ): void { - $this->processManager->setFailProcessStart($failStart); - $this->processManager->setMockExitCode($exitCode); - $this->processManager->setMockStderr($errorOutput); - - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage($expectedMessage); - - $this->processManager->execute(['test' => true], false); - } - - public static function errorScenarioProvider(): array - { - return [ - 'process start failure' => [ - true, 0, '', 'Failed to start process', - ], - 'non-zero exit code' => [ - false, 1, 'Error occurred', 'Process failed with exit code 1', - ], - 'permission error' => [ - false, 126, 'Permission denied', 'Process failed with exit code 126', - ], - ]; - } public function testLongRunningProcess(): void { From c2710ff61967a815277dbd57906ac0ea676b582f Mon Sep 17 00:00:00 2001 From: elwafa Date: Sun, 26 Jan 2025 23:04:01 +0200 Subject: [PATCH 3/5] ci --- src/Configuration.php | 1 + src/Platform.php | 16 +++++++++------- src/ProcessManager.php | 11 +++++++---- src/Request.php | 1 + tests/Units/ProcessManagerTest.php | 1 - 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index cd1681c..9e5dffd 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -88,6 +88,7 @@ public function setTarget(string $idleTimeout = '30s'): self throw new VoltTestException('Invalid idle timeout format. Use [s|m|h]'); } $this->target['idle_timeout'] = $idleTimeout; + return $this; } diff --git a/src/Platform.php b/src/Platform.php index 46641b1..bc148eb 100644 --- a/src/Platform.php +++ b/src/Platform.php @@ -99,7 +99,7 @@ public static function installBinary($testing = false): void { $platform = self::detectPlatform($testing); - if (!array_key_exists($platform, self::SUPPORTED_PLATFORMS)) { + if (! array_key_exists($platform, self::SUPPORTED_PLATFORMS)) { throw new \RuntimeException("Platform $platform is not supported"); } @@ -113,8 +113,8 @@ public static function installBinary($testing = false): void $targetFile .= '.exe'; } - if (!is_dir($binDir)) { - if (!mkdir($binDir, 0755, true)) { + if (! is_dir($binDir)) { + if (! mkdir($binDir, 0755, true)) { throw new \RuntimeException("Failed to create directory: $binDir"); } } @@ -135,6 +135,7 @@ public static function installBinary($testing = false): void $fp = fopen($tempFile, 'w'); if ($fp === false) { curl_close($ch); + throw new \RuntimeException("Failed to open temporary file for writing"); } @@ -145,7 +146,7 @@ public static function installBinary($testing = false): void CURLOPT_FAILONERROR => true, CURLOPT_TIMEOUT => 300, CURLOPT_CONNECTTIMEOUT => 30, - CURLOPT_HTTPHEADER => ['User-Agent: volt-test-php-sdk'] + CURLOPT_HTTPHEADER => ['User-Agent: volt-test-php-sdk'], ]); if (curl_exec($ch) === false) { @@ -160,15 +161,15 @@ public static function installBinary($testing = false): void curl_close($ch); fclose($fp); - if (!file_exists($tempFile) || filesize($tempFile) === 0) { + if (! file_exists($tempFile) || filesize($tempFile) === 0) { throw new \RuntimeException("Downloaded file is empty or missing"); } - if (!rename($tempFile, $targetFile)) { + if (! rename($tempFile, $targetFile)) { throw new \RuntimeException("Failed to move downloaded binary to: $targetFile"); } - if (!chmod($targetFile, 0755)) { + if (! chmod($targetFile, 0755)) { throw new \RuntimeException("Failed to set executable permissions on binary"); } @@ -179,6 +180,7 @@ public static function installBinary($testing = false): void if (file_exists($tempFile)) { unlink($tempFile); } + throw $e; } } diff --git a/src/ProcessManager.php b/src/ProcessManager.php index 679f1db..9877eaa 100644 --- a/src/ProcessManager.php +++ b/src/ProcessManager.php @@ -7,6 +7,7 @@ class ProcessManager { private string $binaryPath; + private $currentProcess = null; public function __construct(string $binaryPath) @@ -33,7 +34,7 @@ public function execute(array $config, bool $streamOutput): string [$success, $process, $pipes] = $this->openProcess(); $this->currentProcess = $process; - if (!$success || !is_array($pipes)) { + if (! $success || ! is_array($pipes)) { throw new RuntimeException('Failed to start process of volt test'); } @@ -62,6 +63,7 @@ public function execute(array $config, bool $streamOutput): string $this->currentProcess = null; if ($exitCode !== 0) { echo "\nError: " . trim($stderrContent) . "\n"; + return ''; } } @@ -96,7 +98,7 @@ protected function openProcess(): array ['bypass_shell' => true] ); - if (!is_resource($process)) { + if (! is_resource($process)) { return [false, null, []]; } @@ -128,6 +130,7 @@ private function handleProcess(array $pipes, bool $streamOutput): string if (feof($pipe)) { fclose($pipe); unset($pipes[$type]); + continue; } } @@ -155,7 +158,7 @@ protected function writeInput($pipe, string $input): void protected function closeProcess($process): int { - if (!is_resource($process)) { + if (! is_resource($process)) { return -1; } @@ -167,4 +170,4 @@ protected function closeProcess($process): int return proc_close($process); } -} \ No newline at end of file +} diff --git a/src/Request.php b/src/Request.php index 816fafd..c284a5b 100644 --- a/src/Request.php +++ b/src/Request.php @@ -109,6 +109,7 @@ public function toArray(): array if (count($this->headers) > 0) { $array['header'] = $this->headers; } + return $array; } diff --git a/tests/Units/ProcessManagerTest.php b/tests/Units/ProcessManagerTest.php index 9e5a989..e741d86 100644 --- a/tests/Units/ProcessManagerTest.php +++ b/tests/Units/ProcessManagerTest.php @@ -63,7 +63,6 @@ public static function outputProvider(): array ]; } - public function testLongRunningProcess(): void { $longOutput = str_repeat("Output line\n", 100); From 88a5c757922778ed75e3058490b4be46371e3a2d Mon Sep 17 00:00:00 2001 From: elwafa Date: Sun, 26 Jan 2025 23:05:49 +0200 Subject: [PATCH 4/5] update composer version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d3c0c83..e2ea830 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "volt-test/php-sdk", "description": "Volt Test PHP SDK - A performance testing tool for PHP applications", "type": "library", - "version": "0.1.0", + "version": "0.1.1", "keywords": [ "volt-test", "php-sdk", From 993c656ac4db3fd89bea29ce0aa3f74ac86ca7c9 Mon Sep 17 00:00:00 2001 From: elwafa Date: Sun, 26 Jan 2025 23:10:14 +0200 Subject: [PATCH 5/5] Change name of const enine version --- src/Platform.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Platform.php b/src/Platform.php index bc148eb..a2bb622 100644 --- a/src/Platform.php +++ b/src/Platform.php @@ -6,7 +6,7 @@ class Platform { private const BINARY_NAME = 'volt-test'; - private const CURRENT_VERSION = 'v0.0.1'; + private const ENGINE_CURRENT_VERSION = 'v0.0.1'; private const BASE_DOWNLOAD_URL = 'https://github.com/volt-test/binaries/releases/download'; private const SUPPORTED_PLATFORMS = [ 'linux-amd64' => 'volt-test-linux-amd64', @@ -67,7 +67,7 @@ private static function getBinaryDir(): string private static function getCurrentVersion(): string { - return self::CURRENT_VERSION; + return self::ENGINE_CURRENT_VERSION; } private static function detectPlatform($testing = false): string