Skip to content

Commit

Permalink
Add RetryConnection test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Sep 13, 2023
1 parent 0f19cc1 commit 478b1be
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/Lib/RetryableConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace ipl\Tests\Sql\Lib;

use ipl\Sql\RetryConnection;

class RetryableConnection extends RetryConnection
{
public function execRetry(callable $callback)
{
return $this->retry($callback);
}
}
40 changes: 40 additions & 0 deletions tests/RetryConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace ipl\Tests\Sql;

use Exception;
use ipl\Sql\RetryConnection;

class RetryConnectionTest extends \PHPUnit\Framework\TestCase
{
public function testIsRetryable()
{
$db = $this->getConnection();

$this->assertTrue($db::isRetryable(new Exception('SQLState: Connection refused by the server')));
$this->assertTrue($db::isRetryable(new Exception('SQLState: Error writing data to the connection')));
$this->assertTrue($db::isRetryable(new Exception('SQLState: No such file or directory found')));

$this->assertFalse($db::isRetryable(new Exception('SQLState: Cannot start transaction')));
$this->assertFalse($db::isRetryable(new Exception('Cannot establish the connection to SQL server')));
$this->assertFalse($db::isRetryable(new Exception('Fatal error encountered during command execution')));
}

public function testExecutionRetriesGivesUpAfterMaxRetries()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('SQLSTATE[HY000] [2002] No such file or directory');

$this->getConnection(2)->transaction(function () {

});
}

protected function getConnection(int $retries = 1): RetryConnection
{
return new RetryConnection([
'db' => 'mysql',
'dbname' => 'foo',
], $retries);
}
}

0 comments on commit 478b1be

Please sign in to comment.