Skip to content

Commit

Permalink
[10.x] Allow custom mutex names for isolated commands (#47814)
Browse files Browse the repository at this point in the history
* added the ability to specify the custom identifier of commands to the commandMutexName function in the CacheCommandMutex class

* developed unit tests for the CommandMutexName function

* fixed styleci errors

* - used DIRECTORY_SEPARATOR const

* formatting

* update test

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
rybakihor and taylorotwell authored Jul 27, 2023
1 parent 45b61f5 commit df0563a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Console/CacheCommandMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,18 @@ public function forget($command)
}

/**
* Get the isolatable command mutex name.
*
* @param \Illuminate\Console\Command $command
* @return string
*/
protected function commandMutexName($command)
{
return 'framework'.DIRECTORY_SEPARATOR.'command-'.$command->getName();
$baseName = 'framework'.DIRECTORY_SEPARATOR.'command-'.$command->getName();

return method_exists($command, 'isolatableId')
? $baseName.'-'.$command->isolatableId()
: $baseName;
}

/**
Expand Down
50 changes: 50 additions & 0 deletions tests/Console/CacheCommandMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,54 @@ private function acquireLockExpectations(MockInterface $lock, bool $acquiresSucc
->once()
->andReturns($acquiresSuccessfully);
}

public function testCommandMutexNameWithoutIsolatedMutexNameMethod()
{
$this->mockUsingCacheStore();

$this->cacheRepository->shouldReceive('getStore')
->with('test')
->andReturn($this->cacheRepository);

$this->cacheRepository->shouldReceive('add')
->once()
->withArgs(function ($key) {
$this->assertEquals('framework'.DIRECTORY_SEPARATOR.'command-command-name', $key);

return true;
})
->andReturn(true);

$this->mutex->create($this->command);
}

public function testCommandMutexNameWithIsolatedMutexNameMethod()
{
$command = new class extends Command
{
protected $name = 'command-name';

public function isolatableId()
{
return 'isolated';
}
};

$this->mockUsingCacheStore();

$this->cacheRepository->shouldReceive('getStore')
->with('test')
->andReturn($this->cacheRepository);

$this->cacheRepository->shouldReceive('add')
->once()
->withArgs(function ($key) {
$this->assertEquals('framework'.DIRECTORY_SEPARATOR.'command-command-name-isolated', $key);

return true;
})
->andReturn(true);

$this->mutex->create($command);
}
}

0 comments on commit df0563a

Please sign in to comment.