Skip to content
Open
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
10 changes: 6 additions & 4 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class Event implements PingableInterface
* Indicates if the command should not overlap itself.
*/
private bool $preventOverlapping = false;
private int $lockTtl = 30;
/** @var ClockInterface */
private static $clock;
private static ?ClosureSerializerInterface $closureSerializer = null;
Expand Down Expand Up @@ -702,11 +703,14 @@ public function user($user)
* that will be responsible for the locking.
*
* @param PersistingStoreInterface|object $store
* @param int $lockTtl TTL in seconds for the overlap-prevention lock (default: 30)
*
* @return $this
*/
public function preventOverlapping(?object $store = null)
public function preventOverlapping(?object $store = null, int $lockTtl = 30)
{
$this->lockTtl = $lockTtl;

if (null !== $store && !($store instanceof PersistingStoreInterface)) {
$expectedClass = PersistingStoreInterface::class;
$actualClass = $store::class;
Expand Down Expand Up @@ -1101,10 +1105,8 @@ protected function createLockObject()
$this->checkLockFactory();

if (null === $this->lock && null !== $this->lockFactory) {
$ttl = 30;

$this->lock = $this->lockFactory
->createLock($this->lockKey(), $ttl);
->createLock($this->lockKey(), $this->lockTtl);
}

return $this->lock;
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,30 @@ public static function dateFromToProvider(): iterable
];
}

/** @test */
public function prevent_overlapping_default_lock_ttl_is_30(): void
{
$event = $this->createEvent();
$event->preventOverlapping();

$reflection = new \ReflectionProperty(Event::class, 'lockTtl');
$reflection->setAccessible(true);

self::assertSame(30, $reflection->getValue($event));
}

/** @test */
public function prevent_overlapping_accepts_custom_lock_ttl(): void
{
$event = $this->createEvent();
$event->preventOverlapping(null, 300);

$reflection = new \ReflectionProperty(Event::class, 'lockTtl');
$reflection->setAccessible(true);

self::assertSame(300, $reflection->getValue($event));
}

private function assertPreventOverlapping(?PersistingStoreInterface $store = null): void
{
$event = $this->createPreventOverlappingEvent($store);
Expand Down