Skip to content

Commit 443c662

Browse files
authored
Psr transport network exception test (#1635)
* add test for NetworkException in PsrTransport * remove unneeded exception expectation
1 parent 5ed1ec0 commit 443c662

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

tests/Unit/SDK/Common/Export/Http/PsrTransportTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
use function gzdecode;
1212
use function gzencode;
1313
use InvalidArgumentException;
14+
use Nyholm\Psr7\Request;
1415
use Nyholm\Psr7\Response;
1516
use OpenTelemetry\SDK\Common\Export\Http\PsrTransport;
1617
use OpenTelemetry\SDK\Common\Export\Http\PsrTransportFactory;
1718
use PHPUnit\Framework\Attributes\CoversClass;
1819
use PHPUnit\Framework\MockObject\MockObject;
1920
use PHPUnit\Framework\TestCase;
2021
use Psr\Http\Client\ClientInterface;
22+
use Psr\Http\Client\NetworkExceptionInterface;
2123
use Psr\Http\Message\RequestInterface;
2224
use Psr\Http\Message\ResponseInterface;
2325

@@ -152,6 +154,28 @@ public function test_send_number_of_retries(): void
152154
$response->await();
153155
}
154156

157+
public function test_send_retry_network_exception_returns_error(): void
158+
{
159+
$e = new class('network error') extends \Exception implements NetworkExceptionInterface {
160+
public function getRequest(): RequestInterface
161+
{
162+
return new Request('GET', 'http://localhost');
163+
}
164+
};
165+
$this->client->expects($this->exactly(4))->method('sendRequest')->willThrowException($e);
166+
$transport = $this->factory->create('http://localhost', 'text/plain', [], null, 10., 100, 3);
167+
168+
$response = $transport->send('');
169+
170+
try {
171+
$response->await();
172+
} catch (Exception $e) {
173+
$this->assertStringContainsString('retry limit', $e->getMessage());
174+
$this->assertNotNull($e->getPrevious());
175+
$this->assertSame('network error', $e->getPrevious()->getMessage());
176+
}
177+
}
178+
155179
public function test_send_exception_returns_error(): void
156180
{
157181
$this->client->expects($this->once())->method('sendRequest')->willThrowException(new Exception());

0 commit comments

Comments
 (0)