diff --git a/src/BusProNet/ApiClient.php b/src/BusProNet/ApiClient.php index 6604d04..5107a61 100644 --- a/src/BusProNet/ApiClient.php +++ b/src/BusProNet/ApiClient.php @@ -844,6 +844,7 @@ class ApiClient $this->logger->error('Connection retry timeout exceeded', [ 'elapsed_time' => microtime(true) - $this->operationStartTime, 'total_timeout' => $this->config['total_timeout'], + 'port' => $this->selectedPort, ]); throw new TimeoutException('Connection timeout exceeded during retries'); } @@ -852,6 +853,7 @@ class ApiClient 'error_message' => $errStr, 'error_number' => $errNo, 'attempt' => $tries, + 'port' => $this->selectedPort, ]); ++$tries; sleep(1); @@ -865,6 +867,7 @@ class ApiClient 'error_message' => $errStr, 'error_number' => $errNo, 'elapsed_time' => microtime(true) - $this->operationStartTime, + 'port' => $this->selectedPort, ]); throw new ApiClientException('Unable to open socket'); } @@ -883,6 +886,7 @@ class ApiClient if (microtime(true) - $this->operationStartTime > $this->config['total_timeout']) { $this->logger->error('Total timeout exceeded before send', [ 'elapsed_time' => microtime(true) - $this->operationStartTime, + 'port' => $this->selectedPort, ]); throw new TimeoutException('Total operation timeout exceeded before send'); } @@ -913,6 +917,7 @@ class ApiClient 'total_timeout' => $totalTimeout, 'bytes_received' => strlen($response), 'read_attempts' => $readAttempts, + 'port' => $this->selectedPort, ]); throw new TimeoutException('Total operation timeout exceeded while receiving data'); } @@ -930,6 +935,7 @@ class ApiClient 'elapsed_time' => $elapsedTime, 'bytes_received' => 0, 'read_attempts' => $readAttempts, + 'port' => $this->selectedPort, ]); throw new ImmediateConnectionCloseException('Server closed connection immediately - server may be busy'); } @@ -938,6 +944,7 @@ class ApiClient 'elapsed_time' => $elapsedTime, 'bytes_received' => strlen($response), 'read_attempts' => $readAttempts, + 'port' => $this->selectedPort, ]); throw new TimeoutException('Stream timeout while reading from socket'); } @@ -950,6 +957,7 @@ class ApiClient $this->logger->warning('Server closed connection without sending data', [ 'elapsed_time' => $elapsedTime, 'read_attempts' => $readAttempts, + 'port' => $this->selectedPort, ]); throw new ImmediateConnectionCloseException('Server closed connection without sending data'); } diff --git a/tests/BusProNet/ApiClientReceiveTest.php b/tests/BusProNet/ApiClientReceiveTest.php index ae5bdea..8213b56 100644 --- a/tests/BusProNet/ApiClientReceiveTest.php +++ b/tests/BusProNet/ApiClientReceiveTest.php @@ -18,14 +18,17 @@ use Symfony\Component\Serializer\SerializerInterface; class ApiClientReceiveTest extends TestCase { private ApiClient $apiClient; + private LoggerInterface $logger; protected function setUp(): void { + $this->logger = $this->createMock(LoggerInterface::class); + $this->apiClient = new ApiClient( $this->createMock(SerializerInterface::class), $this->createMock(ApiResponseParser::class), $this->createMock(FilesystemOperator::class), - $this->createMock(LoggerInterface::class), + $this->logger, $this->createMock(BookingDataProcessor::class), new RequestStack(), new RequestIdGenerator(), @@ -45,6 +48,16 @@ class ApiClientReceiveTest extends TestCase rewind($stream); $this->setOperationStartTime(); + $this->setSelectedPort(9000); + + $this->logger->expects($this->once()) + ->method('warning') + ->with( + 'Server closed connection without sending data', + $this->callback(static function (array $context): bool { + return 9000 === $context['port']; + }), + ); $this->expectException(ImmediateConnectionCloseException::class); $this->expectExceptionMessage('Server closed connection without sending data'); @@ -77,4 +90,10 @@ class ApiClientReceiveTest extends TestCase $property = new \ReflectionProperty(ApiClient::class, 'operationStartTime'); $property->setValue($this->apiClient, microtime(true)); } + + private function setSelectedPort(int $port): void + { + $property = new \ReflectionProperty(ApiClient::class, 'selectedPort'); + $property->setValue($this->apiClient, $port); + } }