diff --git a/tests/Lib/RetryableConnection.php b/tests/Lib/RetryableConnection.php new file mode 100644 index 0000000..0b7ab27 --- /dev/null +++ b/tests/Lib/RetryableConnection.php @@ -0,0 +1,13 @@ +retry($callback); + } +} diff --git a/tests/RetryConnectionTest.php b/tests/RetryConnectionTest.php new file mode 100644 index 0000000..5c248b1 --- /dev/null +++ b/tests/RetryConnectionTest.php @@ -0,0 +1,40 @@ +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); + } +}