From 478b1be57645bd2518587f68fda8ffa3b21b7707 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 13 Sep 2023 10:14:55 +0200 Subject: [PATCH] Add `RetryConnection` test cases --- tests/Lib/RetryableConnection.php | 13 ++++++++++ tests/RetryConnectionTest.php | 40 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/Lib/RetryableConnection.php create mode 100644 tests/RetryConnectionTest.php 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); + } +}