DatabaseLockTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Schema;
  7. class DatabaseLockTest extends DatabaseTestCase
  8. {
  9. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  10. {
  11. Schema::create('cache_locks', function (Blueprint $table) {
  12. $table->string('key')->primary();
  13. $table->string('owner');
  14. $table->integer('expiration');
  15. });
  16. }
  17. public function testLockCanHaveASeparateConnection()
  18. {
  19. $this->app['config']->set('cache.stores.database.lock_connection', 'test');
  20. $this->app['config']->set('database.connections.test', $this->app['config']->get('database.connections.mysql'));
  21. $this->assertSame('test', Cache::driver('database')->lock('foo')->getConnectionName());
  22. }
  23. public function testLockCanBeAcquired()
  24. {
  25. $lock = Cache::driver('database')->lock('foo');
  26. $this->assertTrue($lock->get());
  27. $otherLock = Cache::driver('database')->lock('foo');
  28. $this->assertFalse($otherLock->get());
  29. $lock->release();
  30. $otherLock = Cache::driver('database')->lock('foo');
  31. $this->assertTrue($otherLock->get());
  32. $otherLock->release();
  33. }
  34. public function testLockCanBeForceReleased()
  35. {
  36. $lock = Cache::driver('database')->lock('foo');
  37. $this->assertTrue($lock->get());
  38. $otherLock = Cache::driver('database')->lock('foo');
  39. $otherLock->forceRelease();
  40. $this->assertTrue($otherLock->get());
  41. $otherLock->release();
  42. }
  43. public function testExpiredLockCanBeRetrieved()
  44. {
  45. $lock = Cache::driver('database')->lock('foo');
  46. $this->assertTrue($lock->get());
  47. DB::table('cache_locks')->update(['expiration' => now()->subDays(1)->getTimestamp()]);
  48. $otherLock = Cache::driver('database')->lock('foo');
  49. $this->assertTrue($otherLock->get());
  50. $otherLock->release();
  51. }
  52. }