Skip to content

Commit

Permalink
chore: increase test coverage;
Browse files Browse the repository at this point in the history
  • Loading branch information
vitgrams committed Nov 28, 2024
1 parent b0cd6b8 commit 1a303da
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/Drivers/BaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,12 @@ protected function handleFileWithLock(
string $mode,
int $operation,
callable $callback,
int $maxRetries = 20,
int $minWaitTime = 100,
int $maxWaitTime = 1000,
): mixed
{
$handle = fopen($filePath, $mode);

if ($handle === false) {
throw new RuntimeException("Unable to open file: {$filePath}");
}

$retryCounter = 0;

try {
while (!flock($handle, $operation)) {
if ($retryCounter >= $maxRetries) {
throw new RuntimeException("Unable to lock file: {$filePath}");
}

usleep(rand($minWaitTime, $maxWaitTime));

$retryCounter++;
}
$this->acquireLock($handle, $operation);

return $callback($handle);
} finally {
Expand All @@ -131,8 +114,29 @@ protected function readJsonFromStream($handle): ?array
{
$content = stream_get_contents($handle);

return ($content === false)
? null
: json_decode($content, true);
return ($content === false) ? null : json_decode($content, true);
}

/**
* @codeCoverageIgnore
*/
protected function acquireLock(
$handle,
int $operation,
int $maxRetries = 20,
int $minWaitTime = 100,
int $maxWaitTime = 1000,
): void {
$retryCounter = 0;

while (!flock($handle, $operation)) {
if ($retryCounter >= $maxRetries) {
throw new RuntimeException('Unable to lock file');
}

usleep(rand($minWaitTime, $maxWaitTime));

$retryCounter++;
}
}
}

0 comments on commit 1a303da

Please sign in to comment.