Skip to content

Commit

Permalink
Do not require BlockingStoreInterface in "preventOverlapping" method
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloKowalczyk committed Oct 27, 2022
1 parent 9148a14 commit c3bd22d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
12 changes: 5 additions & 7 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
use Crunz\Pinger\PingableTrait;
use Crunz\Process\Process;
use Crunz\Task\TaskException;
use Symfony\Component\Lock\BlockingStoreInterface;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\Factory;
use Symfony\Component\Lock\Lock;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\Store\FlockStore;
use Symfony\Component\Lock\StoreInterface;

class Event implements PingableInterface
{
Expand Down Expand Up @@ -715,19 +714,18 @@ public function user($user)
* By default, the lock is acquired through file system locks. Alternatively, you can pass a symfony lock store
* that will be responsible for the locking.
*
* @param StoreInterface|BlockingStoreInterface $store
* @param PersistingStoreInterface|object $store
*
* @return $this
*/
public function preventOverlapping(object $store = null)
{
if (null !== $store && !($store instanceof BlockingStoreInterface || $store instanceof StoreInterface)) {
$legacyClass = StoreInterface::class;
$newClass = BlockingStoreInterface::class;
if (null !== $store && !($store instanceof PersistingStoreInterface)) {
$expectedClass = PersistingStoreInterface::class;
$actualClass = \get_class($store);

throw new \RuntimeException(
"Instance of '{$newClass}' or '{$legacyClass}' is expected, '{$actualClass}' provided"
"Instance of '{$expectedClass}' is expected, '{$actualClass}' provided"
);
}

Expand Down
40 changes: 37 additions & 3 deletions tests/Unit/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
use Crunz\Tests\TestCase\Faker;
use Crunz\Tests\TestCase\TestClock;
use Crunz\Tests\TestCase\UnitTestCase;
use Symfony\Component\Lock\BlockingStoreInterface;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\Store\SemaphoreStore;

final class EventTest extends UnitTestCase
Expand Down Expand Up @@ -412,6 +413,39 @@ public function test_hourly_at_with_invalid_minute(
$event->hourlyAt($minute);
}

public function test_non_blocking_store_can_be_passed_to_prevent_overlapping(): void
{
// Arrange
$store = new class() implements PersistingStoreInterface {
public function save(Key $key): void
{
throw new \RuntimeException('Not implemented.');
}

public function delete(Key $key): void
{
throw new \RuntimeException('Not implemented.');
}

public function exists(Key $key): bool
{
throw new \RuntimeException('Not implemented.');
}

public function putOffExpiration(Key $key, float $ttl): void
{
throw new \RuntimeException('Not implemented.');
}
};
$event = $this->createEvent();

// Expect
$this->expectNotToPerformAssertions();

// Act
$event->preventOverlapping($store);
}

/** @return iterable<string,array> */
public function deprecatedEveryProvider(): iterable
{
Expand Down Expand Up @@ -452,7 +486,7 @@ public function hourlyAtInvalidProvider(): iterable
];
}

private function assertPreventOverlapping(BlockingStoreInterface $store = null): void
private function assertPreventOverlapping(PersistingStoreInterface $store = null): void
{
$event = $this->createPreventOverlappingEvent($store);
$event2 = $this->createPreventOverlappingEvent($store);
Expand All @@ -462,7 +496,7 @@ private function assertPreventOverlapping(BlockingStoreInterface $store = null):
self::assertFalse($event2->isDue(new \DateTimeZone('UTC')));
}

private function createPreventOverlappingEvent(BlockingStoreInterface $store = null): Event
private function createPreventOverlappingEvent(PersistingStoreInterface $store = null): Event
{
$command = "php -r 'sleep(2);'";

Expand Down

0 comments on commit c3bd22d

Please sign in to comment.