Skip to content

Commit

Permalink
fix carbon usage and use correct lock store
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaitholabi committed Jul 3, 2023
1 parent 8f07e42 commit bea99c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
16 changes: 10 additions & 6 deletions src/Illuminate/Console/CacheCommandMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Carbon\CarbonInterval;
use Illuminate\Contracts\Cache\Factory as Cache;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Support\InteractsWithTime;

class CacheCommandMutex implements CommandMutex
{
use InteractsWithTime;

/**
* The cache factory implementation.
*
Expand Down Expand Up @@ -42,12 +45,13 @@ public function create($command)
{
$store = $this->cache->store($this->store);

/** @var \DateTimeInterface|\DateInterval $expiresAt */
$expiresAt = method_exists($command, 'isolationLockExpiresAt')
? $command->isolationLockExpiresAt()
: CarbonInterval::hour();

if ($store instanceof LockProvider) {
return $store->lock($this->commandMutexName($command), $expiresAt)->get();
if ($store->getStore() instanceof LockProvider) {
return $store->getStore()->lock($this->commandMutexName($command), $this->secondsUntil($expiresAt))->get();
}

return $store->add($this->commandMutexName($command), true, $expiresAt);
Expand All @@ -66,8 +70,8 @@ public function exists($command)
{
$store = $this->cache->store($this->store);

if ($store instanceof LockProvider) {
$lock = $store->lock($this->commandMutexName($command));
if ($store->getStore() instanceof LockProvider) {
$lock = $store->getStore()->lock($this->commandMutexName($command));
$acquired = $lock->get();
$lock->release();

Expand All @@ -87,8 +91,8 @@ public function forget($command)
{
$store = $this->cache->store($this->store);

if ($store instanceof LockProvider) {
return $store->lock($this->commandMutexName($command))->release();
if ($store->getStore() instanceof LockProvider) {
return $store->getStore()->lock($this->commandMutexName($command))->forceRelease();
}

return $this->cache->store($this->store)->forget($this->commandMutexName($command));
Expand Down
12 changes: 8 additions & 4 deletions tests/Console/CacheCommandMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CacheCommandMutexTest extends TestCase
protected function setUp(): void
{
$this->cacheFactory = m::mock(Factory::class);
$this->cacheRepository = m::mock(Repository::class);
$this->mutex = new CacheCommandMutex($this->cacheFactory);
$this->command = new class extends Command
{
Expand Down Expand Up @@ -116,7 +117,9 @@ public function testCannotCreateMutexIfAlreadyExistWithLockProvider()
public function testCanCreateMutexWithCustomConnectionWithLockProvider()
{
$lock = m::mock(LockProvider::class);
$this->cacheFactory->expects('store')->once()->with('test')->andReturns($lock);
$this->cacheFactory->expects('store')->once()->with('test')->andReturn($this->cacheRepository);
$this->cacheRepository->expects('getStore')->andReturn($lock);

$this->acquireLockExpectations($lock, true);
$this->mutex->useStore('test');

Expand All @@ -128,14 +131,15 @@ public function testCanCreateMutexWithCustomConnectionWithLockProvider()
*/
private function mockUsingCacheStore(): void
{
$this->cacheRepository = m::mock(Repository::class);
$this->cacheFactory->expects('store')->once()->andReturn($this->cacheRepository);
$this->cacheRepository->expects('getStore')->andReturn(null);
}

private function mockUsingLockProvider(): m\MockInterface
{
$lock = m::mock(LockProvider::class);
$this->cacheFactory->expects('store')->once()->andReturns($lock);
$this->cacheFactory->expects('store')->once()->andReturn($this->cacheRepository);
$this->cacheRepository->expects('getStore')->andReturn($lock);

return $lock;
}
Expand All @@ -144,7 +148,7 @@ private function acquireLockExpectations(MockInterface $lock, bool $acquiresSucc
{
$lock->expects('lock')
->once()
->with(m::type('string'), m::type(CarbonInterval::class))
->with(m::type('string'), m::type('int'))
->andReturns($lock);

$lock->expects('get')
Expand Down

0 comments on commit bea99c9

Please sign in to comment.