-
Notifications
You must be signed in to change notification settings - Fork 56
Exclude timeout exception #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,161 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Unit; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Utopia\Database\Adapter\Memory as DatabaseMemory; | ||
| use Utopia\Database\Adapter\Redis as RedisAdapter; | ||
| use Utopia\Database\Exception\Duplicate as DuplicateException; | ||
| use Utopia\Database\Exception\Timeout as TimeoutException; | ||
|
|
||
| /** | ||
| * Covers the retry policy of Adapter::withTransaction(): which exceptions abort | ||
| * immediately versus which are retried up to the built-in attempt budget. | ||
| */ | ||
| class TransactionRetryTest extends TestCase | ||
| { | ||
| private DatabaseMemory $adapter; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->adapter = new DatabaseMemory(); | ||
| } | ||
|
|
||
| /** | ||
| * A statement timeout already spent the full timeout budget on this attempt; | ||
| * retrying re-runs it for another full budget and amplifies lock convoys. | ||
| * It must abort after a single attempt. | ||
| */ | ||
| public function testTimeoutIsNotRetried(): void | ||
| { | ||
| $attempts = 0; | ||
| $thrown = null; | ||
|
|
||
| try { | ||
| $this->adapter->withTransaction(function () use (&$attempts) { | ||
| $attempts++; | ||
| throw new TimeoutException('Query timed out'); | ||
| }); | ||
| } catch (TimeoutException $e) { | ||
| $thrown = $e; | ||
| } | ||
|
|
||
| $this->assertInstanceOf(TimeoutException::class, $thrown); | ||
| $this->assertSame(1, $attempts); | ||
| } | ||
|
|
||
| /** | ||
| * A duplicate is deterministic, so it also aborts on the first attempt. | ||
| * Anchors the timeout case against an existing no-retry exception. | ||
| */ | ||
| public function testDuplicateIsNotRetried(): void | ||
| { | ||
| $attempts = 0; | ||
| $thrown = null; | ||
|
|
||
| try { | ||
| $this->adapter->withTransaction(function () use (&$attempts) { | ||
| $attempts++; | ||
| throw new DuplicateException('Duplicate'); | ||
| }); | ||
| } catch (DuplicateException $e) { | ||
| $thrown = $e; | ||
| } | ||
|
|
||
| $this->assertInstanceOf(DuplicateException::class, $thrown); | ||
| $this->assertSame(1, $attempts); | ||
| } | ||
|
|
||
| /** | ||
| * A transient/unknown failure is still retried across the full attempt | ||
| * budget (3 attempts: initial + 2 retries) before the error propagates. | ||
| */ | ||
| public function testGenericFailureIsRetried(): void | ||
| { | ||
| $attempts = 0; | ||
| $thrown = null; | ||
|
|
||
| try { | ||
| $this->adapter->withTransaction(function () use (&$attempts) { | ||
| $attempts++; | ||
| throw new \RuntimeException('transient'); | ||
| }); | ||
| } catch (\RuntimeException $e) { | ||
| $thrown = $e; | ||
| } | ||
|
|
||
| $this->assertInstanceOf(\RuntimeException::class, $thrown); | ||
| $this->assertSame(3, $attempts); | ||
| } | ||
|
|
||
| /** | ||
| * Rollback cleanup can itself fail (adapters throw a DatabaseException from | ||
| * rollbackTransaction()). A non-retriable action must still abort after a | ||
| * single attempt and propagate the original action, not be retried or | ||
| * masked by the rollback error. | ||
| */ | ||
| public function testNonRetriableActionAbortsWhenRollbackFails(): void | ||
| { | ||
| $adapter = new class () extends DatabaseMemory { | ||
| public function rollbackTransaction(): bool | ||
| { | ||
| throw new \RuntimeException('rollback failed'); | ||
| } | ||
| }; | ||
|
|
||
| $attempts = 0; | ||
| $thrown = null; | ||
|
|
||
| try { | ||
| $adapter->withTransaction(function () use (&$attempts) { | ||
| $attempts++; | ||
| throw new TimeoutException('Query timed out'); | ||
| }); | ||
| } catch (\Throwable $e) { | ||
| $thrown = $e; | ||
| } | ||
|
|
||
| $this->assertInstanceOf(TimeoutException::class, $thrown); | ||
| $this->assertSame(1, $attempts); | ||
| } | ||
|
|
||
| /** | ||
| * A failed rollback in the Redis adapter must reset the depth counter and | ||
| * the journal stack together. Resetting only the counter would strand the | ||
| * parent frames, breaking the count($journalStack) === inTransaction | ||
| * invariant and letting later transactions merge into a stale frame. | ||
| */ | ||
| public function testRedisRollbackFailureClearsJournalStack(): void | ||
| { | ||
| if (!\extension_loaded('redis')) { | ||
| $this->markTestSkipped('redis extension not loaded'); | ||
| } | ||
|
|
||
| $adapter = new class (new \Redis()) extends RedisAdapter { | ||
| protected function rollbackJournal(): void | ||
| { | ||
| throw new \RuntimeException('rollback replay failed'); | ||
| } | ||
| }; | ||
|
|
||
| $adapter->startTransaction(); | ||
| $adapter->startTransaction(); | ||
|
|
||
| $thrown = null; | ||
| try { | ||
| $adapter->rollbackTransaction(); | ||
| } catch (\Throwable $e) { | ||
| $thrown = $e; | ||
| } | ||
|
|
||
| $this->assertInstanceOf(\RuntimeException::class, $thrown); | ||
|
|
||
| $inTransaction = new \ReflectionProperty(RedisAdapter::class, 'inTransaction'); | ||
| $inTransaction->setAccessible(true); | ||
| $this->assertSame(0, $inTransaction->getValue($adapter)); | ||
|
|
||
| $journalStack = new \ReflectionProperty(RedisAdapter::class, 'journalStack'); | ||
| $journalStack->setAccessible(true); | ||
| $this->assertSame([], $journalStack->getValue($adapter)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.