|
2 | 2 |
|
3 | 3 | namespace Illuminate\Tests\Integration\Database; |
4 | 4 |
|
| 5 | +use Illuminate\Cache\DatabaseLock; |
| 6 | +use Illuminate\Database\Connection; |
| 7 | +use Illuminate\Database\Query\Builder; |
| 8 | +use Illuminate\Database\QueryException; |
5 | 9 | use Illuminate\Support\Facades\Cache; |
6 | 10 | use Illuminate\Support\Facades\DB; |
| 11 | +use Mockery as m; |
7 | 12 | use Orchestra\Testbench\Attributes\WithMigration; |
| 13 | +use PDOException; |
| 14 | +use PHPUnit\Framework\Attributes\TestWith; |
8 | 15 |
|
9 | 16 | #[WithMigration('cache')] |
10 | 17 | class DatabaseLockTest extends DatabaseTestCase |
@@ -68,4 +75,36 @@ public function testOtherOwnerDoesNotOwnLockAfterRestore() |
68 | 75 | $this->assertTrue($secondLock->isOwnedBy($firstLock->owner())); |
69 | 76 | $this->assertFalse($secondLock->isOwnedByCurrentProcess()); |
70 | 77 | } |
| 78 | + |
| 79 | + #[TestWith(['Deadlock found when trying to get lock', 1213, true])] |
| 80 | + #[TestWith(['Table does not exist', 1146, false])] |
| 81 | + public function testIgnoresConcurrencyException(string $message, int $code, bool $hasConcurrenyError) |
| 82 | + { |
| 83 | + $connection = m::mock(Connection::class); |
| 84 | + $insertBuilder = m::mock(Builder::class); |
| 85 | + $deleteBuilder = m::mock(Builder::class); |
| 86 | + |
| 87 | + $insertBuilder->shouldReceive('insert')->once()->andReturn(true); |
| 88 | + |
| 89 | + $deleteBuilder->shouldReceive('where')->with('expiration', '<=', m::any())->once()->andReturnSelf(); |
| 90 | + $deleteBuilder->shouldReceive('delete')->once()->andThrow( |
| 91 | + new QueryException( |
| 92 | + 'mysql', |
| 93 | + 'delete from cache_locks where expiration <= ?', |
| 94 | + [], |
| 95 | + new PDOException($message, $code) |
| 96 | + ) |
| 97 | + ); |
| 98 | + |
| 99 | + $connection->shouldReceive('table')->with('cache_locks')->andReturn($insertBuilder, $deleteBuilder); |
| 100 | + |
| 101 | + $lock = new DatabaseLock($connection, 'cache_locks', 'foo', 0, lottery: [1, 1]); |
| 102 | + |
| 103 | + if ($hasConcurrenyError) { |
| 104 | + $this->assertTrue($lock->acquire()); |
| 105 | + } else { |
| 106 | + $this->expectException(QueryException::class); |
| 107 | + $this->assertFalse($lock->acquire()); |
| 108 | + } |
| 109 | + } |
71 | 110 | } |
0 commit comments