Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/testo.php export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.php_cs.dist.php export-ignore
/.php-cs-fixer.dist.php export-ignore
/psalm.xml export-ignore
/psalm.xml.dist export-ignore
/UPGRADING.md export-ignore
17 changes: 17 additions & 0 deletions .github/workflows/cs-fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---

on: # yamllint disable-line rule:truthy
push:
branches:
- '*'

name: Fix Code Style

jobs:
cs-fix:
permissions:
contents: write
uses: spiral/gh-actions/.github/workflows/cs-fix.yml@master
with:
php: >-
['8.4']
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ docs
vendor
node_modules
.php-cs-fixer.cache
runtime
13 changes: 13 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

require_once 'vendor/autoload.php';

return \Spiral\CodeStyle\Builder::create()
->include(__DIR__ . '/src')
->include(__DIR__ . '/tests')
->include(__FILE__)
->cache('./runtime/php-cs-fixer.cache')
->allowRisky(false)
->build();
73 changes: 0 additions & 73 deletions .styleci.yml

This file was deleted.

3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
},
"require-dev": {
"mockery/mockery": "^1.6",
"spiral/code-style": "^2.3.1",
"testo/bridge-mockery": "^0.1.2",
"testo/testo": "^0.10.55",
"vimeo/psalm": "^6 || ^7"
Expand All @@ -67,6 +68,8 @@
},
"scripts": {
"test": "vendor/bin/testo",
"cs:diff": "php-cs-fixer fix --dry-run -v --diff",
"cs:fix": "php-cs-fixer fix -v",
"psalm": "vendor/bin/psalm --config=psalm.xml ./src --show-info=true"
},
"config": {
Expand Down
29 changes: 13 additions & 16 deletions src/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace RoadRunner\Lock;

use DateInterval;
use RoadRunner\Lock\DTO\V1BETA1\{
Request, Response
};
Expand All @@ -31,8 +30,8 @@ public function __construct(
*
* @param non-empty-string $resource The name of the resource to be locked.
* @param non-empty-string|null $id The lock ID. If not specified, a random UUID will be generated.
* @param int|float|DateInterval $ttl The time-to-live of the lock, in seconds. Defaults to 0 (forever).
* @param int|float|DateInterval $waitTTL How long to wait for the lock to become free before giving up, in seconds.
* @param int|float|\DateInterval $ttl The time-to-live of the lock, in seconds. Defaults to 0 (forever).
* @param int|float|\DateInterval $waitTTL How long to wait for the lock to become free before giving up, in seconds.
* Defaults to 0. With 0 the call is effectively non-blocking: the RoadRunner
* server caps the acquire window at defaultImmediateTimeout (1ms), so false
* is returned almost immediately when the resource is already locked. A
Expand All @@ -45,8 +44,8 @@ public function __construct(
public function lock(
string $resource,
?string $id = null,
int|float|DateInterval $ttl = 0,
int|float|DateInterval $waitTTL = 0,
int|float|\DateInterval $ttl = 0,
int|float|\DateInterval $waitTTL = 0,
): false|string {
$request = new Request();
$request->setResource($resource);
Expand All @@ -68,8 +67,8 @@ public function lock(
*
* @param non-empty-string $resource The name of the resource to be locked.
* @param non-empty-string|null $id The lock ID. If not specified, a random UUID will be generated.
* @param int|float|DateInterval $ttl The time-to-live of the lock, in seconds. Defaults to 0 (forever).
* @param int|float|DateInterval $waitTTL How long to wait for the lock to become free before giving up, in seconds.
* @param int|float|\DateInterval $ttl The time-to-live of the lock, in seconds. Defaults to 0 (forever).
* @param int|float|\DateInterval $waitTTL How long to wait for the lock to become free before giving up, in seconds.
* Defaults to 0. With 0 the call is effectively non-blocking: the RoadRunner
* server caps the acquire window at defaultImmediateTimeout (1ms), so false
* is returned almost immediately when the resource is already locked. A
Expand All @@ -82,8 +81,8 @@ public function lock(
public function lockRead(
string $resource,
?string $id = null,
int|float|DateInterval $ttl = 0,
int|float|DateInterval $waitTTL = 0,
int|float|\DateInterval $ttl = 0,
int|float|\DateInterval $waitTTL = 0,
): false|string {
$request = new Request();
$request->setResource($resource);
Expand Down Expand Up @@ -162,12 +161,12 @@ public function exists(string $resource, ?string $id = null): bool
*
* @param string $resource The name of the resource to update the TTL for.
* @param string $id Lock ID from lock or lockRead method.
* @param int|float|DateInterval $ttl The new TTL in seconds.
* @param int|float|\DateInterval $ttl The new TTL in seconds.
* @return bool Returns true on success and false on failure.
*
* @throws \InvalidArgumentException If ttl is negative.
*/
public function updateTTL(string $resource, string $id, int|float|DateInterval $ttl): bool
public function updateTTL(string $resource, string $id, int|float|\DateInterval $ttl): bool
{
$request = new Request();
$request->setResource($resource);
Expand All @@ -179,10 +178,10 @@ public function updateTTL(string $resource, string $id, int|float|DateInterval $
return $response->getOk();
}

private function convertTimeToMicroseconds(int|float|DateInterval $ttl): int
private function convertTimeToMicroseconds(int|float|\DateInterval $ttl): int
{
if ($ttl instanceof DateInterval) {
return (int) \round((int)$ttl->format('%s') * 1_000_000);
if ($ttl instanceof \DateInterval) {
return (int) \round((int) $ttl->format('%s') * 1_000_000);
}

\assert($ttl >= 0, 'TTL must be positive');
Expand All @@ -194,8 +193,6 @@ private function convertTimeToMicroseconds(int|float|DateInterval $ttl): int
* Make an RPC call to the RoadRunner server.
*
* @param non-empty-string $method
* @param Request $request
* @return Response
*/
private function call(string $method, Request $request): Response
{
Expand Down
2 changes: 1 addition & 1 deletion src/LockIdGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ interface LockIdGeneratorInterface
* @return non-empty-string
*/
public function generate(): string;
}
}
5 changes: 2 additions & 3 deletions src/UuidLockIdGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ final class UuidLockIdGenerator implements LockIdGeneratorInterface
{
public function __construct(
private readonly UuidFactoryInterface $factory = new UuidFactory(),
) {
}
) {}

public function generate(): string
{
return $this->factory->uuid4()->toString();
}
}
}
126 changes: 63 additions & 63 deletions tests/src/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,58 @@ final class LockTest
private LockIdGeneratorInterface|MockInterface $idGenerator;
private Lock $lock;

#[BeforeTest]
protected function setUp(): void
public static function lockTypeDataProvider(): \Generator
{
$this->rpc = m::mock(RPCInterface::class);
foreach (self::lockDataProvider() as $name => $data) {
foreach ([true, false] as $result) {
foreach (['id1', null] as $id) {
yield 'lock: ' . $name . ' | ' . $id . ' | ' . \var_export($result, true) => [
'lock',
'lock.Lock',
...$data,
$result,
$id,
];

$this->rpc->shouldReceive('withCodec')
->with(m::type(ProtobufCodec::class))
->once()
->andReturnSelf();
yield 'read-lock: ' . $name . ' | ' . $id . ' | ' . \var_export($result, true) => [
'lockRead',
'lock.LockRead',
...$data,
$result,
$id,
];
}
}
}
}

$this->idGenerator = m::mock(LockIdGeneratorInterface::class);
$this->lock = new Lock($this->rpc, $this->idGenerator);
public static function updateTTLDataProvider(): \Traversable
{
foreach (self::lockDataProvider() as $name => $data) {
foreach ([true, false] as $result) {
yield $name . ' | ' . \var_export($result, true) => [$data[0], $data[1], $result];
}
}
}

public static function lockDataProvider(): \Generator
{
yield 'int' => [10, 10_000_000, 8, 8_000_000,];

yield 'float' => [0.000_01, 10, 0.000_004, 4,];

yield 'date-interval' => [
new \DateInterval('PT10S'),
10_000_000,
new \DateInterval('PT9S'),
9_000_000,
];
}

public static function resultDataProvider(): \Traversable
{
yield [true];
yield [false];
}

#[DataProvider('lockTypeDataProvider')]
Expand Down Expand Up @@ -159,60 +199,6 @@ public function testExistsWithId(bool $result): void
Assert::same($this->lock->exists('resource', 'some-id'), $result);
}

public static function lockTypeDataProvider(): \Generator
{
foreach (self::lockDataProvider() as $name => $data) {
foreach ([true, false] as $result) {
foreach (['id1', null] as $id) {
yield 'lock: ' . $name . ' | ' .$id. ' | ' . \var_export($result, true) => [
'lock',
'lock.Lock',
...$data,
$result,
$id
];

yield 'read-lock: ' . $name . ' | ' .$id. ' | ' . \var_export($result, true) => [
'lockRead',
'lock.LockRead',
...$data,
$result,
$id
];
}
}
}
}

public static function updateTTLDataProvider(): \Traversable
{
foreach (self::lockDataProvider() as $name => $data) {
foreach ([true, false] as $result) {
yield $name . ' | ' . \var_export($result, true) => [$data[0], $data[1], $result];
}
}
}

public static function lockDataProvider(): \Generator
{
yield 'int' => [10, 10_000_000, 8, 8_000_000,];

yield 'float' => [0.000_01, 10, 0.000_004, 4,];

yield 'date-interval' => [
new \DateInterval('PT10S'),
10_000_000,
new \DateInterval('PT9S'),
9_000_000,
];
}

public static function resultDataProvider(): \Traversable
{
yield [true];
yield [false];
}

#[ExpectException(\LogicException::class)]
public function testLockNegativeTtl(): void
{
Expand Down Expand Up @@ -242,4 +228,18 @@ public function testUpdateNegativeWaitTtl(): void
{
$this->lock->updateTTL('resource', 'uuid', -300);
}

#[BeforeTest]
protected function setUp(): void
{
$this->rpc = m::mock(RPCInterface::class);

$this->rpc->shouldReceive('withCodec')
->with(m::type(ProtobufCodec::class))
->once()
->andReturnSelf();

$this->idGenerator = m::mock(LockIdGeneratorInterface::class);
$this->lock = new Lock($this->rpc, $this->idGenerator);
}
}
Loading