|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Utopia\Database\Adapter\Memory as DatabaseMemory; |
| 7 | +use Utopia\Database\Adapter\Redis as RedisAdapter; |
| 8 | +use Utopia\Database\Exception\Duplicate as DuplicateException; |
| 9 | +use Utopia\Database\Exception\Timeout as TimeoutException; |
| 10 | + |
| 11 | +/** |
| 12 | + * Covers the retry policy of Adapter::withTransaction(): which exceptions abort |
| 13 | + * immediately versus which are retried up to the built-in attempt budget. |
| 14 | + */ |
| 15 | +class TransactionRetryTest extends TestCase |
| 16 | +{ |
| 17 | + private DatabaseMemory $adapter; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + $this->adapter = new DatabaseMemory(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * A statement timeout already spent the full timeout budget on this attempt; |
| 26 | + * retrying re-runs it for another full budget and amplifies lock convoys. |
| 27 | + * It must abort after a single attempt. |
| 28 | + */ |
| 29 | + public function testTimeoutIsNotRetried(): void |
| 30 | + { |
| 31 | + $attempts = 0; |
| 32 | + $thrown = null; |
| 33 | + |
| 34 | + try { |
| 35 | + $this->adapter->withTransaction(function () use (&$attempts) { |
| 36 | + $attempts++; |
| 37 | + throw new TimeoutException('Query timed out'); |
| 38 | + }); |
| 39 | + } catch (TimeoutException $e) { |
| 40 | + $thrown = $e; |
| 41 | + } |
| 42 | + |
| 43 | + $this->assertInstanceOf(TimeoutException::class, $thrown); |
| 44 | + $this->assertSame(1, $attempts); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * A duplicate is deterministic, so it also aborts on the first attempt. |
| 49 | + * Anchors the timeout case against an existing no-retry exception. |
| 50 | + */ |
| 51 | + public function testDuplicateIsNotRetried(): void |
| 52 | + { |
| 53 | + $attempts = 0; |
| 54 | + $thrown = null; |
| 55 | + |
| 56 | + try { |
| 57 | + $this->adapter->withTransaction(function () use (&$attempts) { |
| 58 | + $attempts++; |
| 59 | + throw new DuplicateException('Duplicate'); |
| 60 | + }); |
| 61 | + } catch (DuplicateException $e) { |
| 62 | + $thrown = $e; |
| 63 | + } |
| 64 | + |
| 65 | + $this->assertInstanceOf(DuplicateException::class, $thrown); |
| 66 | + $this->assertSame(1, $attempts); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * A transient/unknown failure is still retried across the full attempt |
| 71 | + * budget (3 attempts: initial + 2 retries) before the error propagates. |
| 72 | + */ |
| 73 | + public function testGenericFailureIsRetried(): void |
| 74 | + { |
| 75 | + $attempts = 0; |
| 76 | + $thrown = null; |
| 77 | + |
| 78 | + try { |
| 79 | + $this->adapter->withTransaction(function () use (&$attempts) { |
| 80 | + $attempts++; |
| 81 | + throw new \RuntimeException('transient'); |
| 82 | + }); |
| 83 | + } catch (\RuntimeException $e) { |
| 84 | + $thrown = $e; |
| 85 | + } |
| 86 | + |
| 87 | + $this->assertInstanceOf(\RuntimeException::class, $thrown); |
| 88 | + $this->assertSame(3, $attempts); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Rollback cleanup can itself fail (adapters throw a DatabaseException from |
| 93 | + * rollbackTransaction()). A non-retriable action must still abort after a |
| 94 | + * single attempt and propagate the original action, not be retried or |
| 95 | + * masked by the rollback error. |
| 96 | + */ |
| 97 | + public function testNonRetriableActionAbortsWhenRollbackFails(): void |
| 98 | + { |
| 99 | + $adapter = new class () extends DatabaseMemory { |
| 100 | + public function rollbackTransaction(): bool |
| 101 | + { |
| 102 | + throw new \RuntimeException('rollback failed'); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + $attempts = 0; |
| 107 | + $thrown = null; |
| 108 | + |
| 109 | + try { |
| 110 | + $adapter->withTransaction(function () use (&$attempts) { |
| 111 | + $attempts++; |
| 112 | + throw new TimeoutException('Query timed out'); |
| 113 | + }); |
| 114 | + } catch (\Throwable $e) { |
| 115 | + $thrown = $e; |
| 116 | + } |
| 117 | + |
| 118 | + $this->assertInstanceOf(TimeoutException::class, $thrown); |
| 119 | + $this->assertSame(1, $attempts); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * A failed rollback in the Redis adapter must reset the depth counter and |
| 124 | + * the journal stack together. Resetting only the counter would strand the |
| 125 | + * parent frames, breaking the count($journalStack) === inTransaction |
| 126 | + * invariant and letting later transactions merge into a stale frame. |
| 127 | + */ |
| 128 | + public function testRedisRollbackFailureClearsJournalStack(): void |
| 129 | + { |
| 130 | + if (!\extension_loaded('redis')) { |
| 131 | + $this->markTestSkipped('redis extension not loaded'); |
| 132 | + } |
| 133 | + |
| 134 | + $adapter = new class (new \Redis()) extends RedisAdapter { |
| 135 | + protected function rollbackJournal(): void |
| 136 | + { |
| 137 | + throw new \RuntimeException('rollback replay failed'); |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + $adapter->startTransaction(); |
| 142 | + $adapter->startTransaction(); |
| 143 | + |
| 144 | + $thrown = null; |
| 145 | + try { |
| 146 | + $adapter->rollbackTransaction(); |
| 147 | + } catch (\Throwable $e) { |
| 148 | + $thrown = $e; |
| 149 | + } |
| 150 | + |
| 151 | + $this->assertInstanceOf(\RuntimeException::class, $thrown); |
| 152 | + |
| 153 | + $inTransaction = new \ReflectionProperty(RedisAdapter::class, 'inTransaction'); |
| 154 | + $inTransaction->setAccessible(true); |
| 155 | + $this->assertSame(0, $inTransaction->getValue($adapter)); |
| 156 | + |
| 157 | + $journalStack = new \ReflectionProperty(RedisAdapter::class, 'journalStack'); |
| 158 | + $journalStack->setAccessible(true); |
| 159 | + $this->assertSame([], $journalStack->getValue($adapter)); |
| 160 | + } |
| 161 | +} |
0 commit comments