-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |