Skip to content

Commit

Permalink
Do not require BlockingStoreInterface in preventOverlapping() (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloKowalczyk authored Nov 1, 2022
1 parent 9148a14 commit 206942a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
19 changes: 7 additions & 12 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 @@ -166,7 +165,7 @@ class Event implements PingableInterface
* The symfony lock factory that is used to acquire locks. If the value is null, but preventOverlapping = true
* crunz falls back to filesystem locks.
*
* @var Factory|LockFactory|null
* @var LockFactory|null
*/
private $lockFactory;
/** @var string[] */
Expand Down Expand Up @@ -715,28 +714,24 @@ 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"
);
}

$lockStore = $store ?: $this->createDefaultLockStore();
$this->preventOverlapping = true;
$this->lockFactory = \class_exists(Factory::class)
? new Factory($lockStore)
: new LockFactory($lockStore)
;
$this->lockFactory = new LockFactory($lockStore);

// Skip the event if it's locked (processing)
$this->skip(function () {
Expand Down
20 changes: 17 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\PersistingStoreInterface;
use Symfony\Component\Lock\Store\PdoStore;
use Symfony\Component\Lock\Store\SemaphoreStore;

final class EventTest extends UnitTestCase
Expand Down Expand Up @@ -412,6 +413,19 @@ 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 PdoStore('');
$event = $this->createEvent();

// Expect
$this->expectNotToPerformAssertions();

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

/** @return iterable<string,array> */
public function deprecatedEveryProvider(): iterable
{
Expand Down Expand Up @@ -452,7 +466,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 +476,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 206942a

Please sign in to comment.