NoLockTest.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Illuminate\Tests\Integration\Cache;
  3. use Illuminate\Support\Carbon;
  4. use Illuminate\Support\Facades\Cache;
  5. use Orchestra\Testbench\TestCase;
  6. class NoLockTest extends TestCase
  7. {
  8. /**
  9. * Define environment setup.
  10. *
  11. * @param \Illuminate\Foundation\Application $app
  12. * @return void
  13. */
  14. protected function getEnvironmentSetUp($app)
  15. {
  16. $app['config']->set('cache.default', 'null');
  17. $app['config']->set('cache.stores', [
  18. 'null' => [
  19. 'driver' => 'null',
  20. ],
  21. ]);
  22. }
  23. public function testLocksCanAlwaysBeAcquiredAndReleased()
  24. {
  25. Cache::lock('foo')->forceRelease();
  26. $lock = Cache::lock('foo', 10);
  27. $this->assertTrue($lock->get());
  28. $this->assertTrue(Cache::lock('foo', 10)->get());
  29. $this->assertTrue($lock->release());
  30. $this->assertTrue($lock->release());
  31. }
  32. public function testLocksCanBlockForSeconds()
  33. {
  34. Carbon::setTestNow();
  35. Cache::lock('foo')->forceRelease();
  36. $this->assertSame('taylor', Cache::lock('foo', 10)->block(1, function () {
  37. return 'taylor';
  38. }));
  39. Cache::lock('foo')->forceRelease();
  40. $this->assertTrue(Cache::lock('foo', 10)->block(1));
  41. }
  42. }