Lock example

$container = new ContainerBuilder();
    $container->set('current_user', $account);
    \Drupal::setContainer($container);

    $view_ui = new ViewUI($storage);

    // A view_ui without a lock object is not locked.     $this->assertFalse($view_ui->isLocked());

    // Set the lock object with a different owner than the mocked account above.     $lock = new Lock(2, (int) $_SERVER['REQUEST_TIME']);
    $view_ui->setLock($lock);
    $this->assertTrue($view_ui->isLocked());

    // Set a different lock object with the same object as the mocked account.     $lock = new Lock(1, (int) $_SERVER['REQUEST_TIME']);
    $view_ui->setLock($lock);
    $this->assertFalse($view_ui->isLocked());

    $view_ui->unsetLock(NULL);
    $this->assertFalse($view_ui->isLocked());
  }

  
$cache->get('cache', fn () => [$now, 0], \INF);

        $this->assertTrue($checkpoint->acquire($startedAt = $now->modify('1 min')));
        $this->assertEquals($now$checkpoint->time());
        $this->assertEquals(0, $checkpoint->index());
        $this->assertEquals([$now, 0, $startedAt]$cache->get('cache', fn () => []));
    }

    public function testWithLockInitStateOnFirstAcquiring()
    {
        $lock = new Lock(new Key('lock')new InMemoryStore());
        $checkpoint = new Checkpoint('dummy', $lock);
        $now = new \DateTimeImmutable('2020-02-20 20:20:20Z');

        $this->assertTrue($checkpoint->acquire($now));
        $this->assertEquals($now$checkpoint->time());
        $this->assertEquals($now$checkpoint->from());
        $this->assertEquals(-1, $checkpoint->index());
        $this->assertTrue($lock->isAcquired());
    }

    public function testWithLockLoadStateOnAcquiring()
    {

  public function getMetadata($key) {
    $key = $this->createkey($key);
    // Fetch the key/value pair and its metadata.     $object = $this->storage->get($key);
    if ($object) {
      // Don't keep the data itself in memory.       unset($object->data);
      return new Lock($object->owner, $object->updated);
    }
  }

  /** * Deletes data from the store for a given key and releases the lock on it. * * @param string $key * The key of the data to delete. * * @return bool * TRUE if the object was deleted or does not exist, FALSE if it exists but * is not owned by $this->owner. * * @throws \Drupal\Core\TempStore\TempStoreException * Thrown when a lock for the backend storage could not be acquired. */
#[AsSchedule('dummy')] class DummySchedule implements ScheduleProviderInterface
{
    public static array $recurringMessages;

    public function getSchedule(): Schedule
    {
        return (new Schedule())
            ->add(...self::$recurringMessages)
            ->stateful(new ArrayAdapter())
            ->lock(new Lock(new Key('dummy')new InMemoryStore()))
        ;
    }
}

  public function getMetadata($key) {
    // Fetch the key/value pair and its metadata.     $object = $this->storage->get($key);
    if ($object) {
      // Don't keep the data itself in memory.       unset($object->data);
      return new Lock($object->owner, $object->updated);
    }
  }

  /** * Deletes data from the store for a given key and releases the lock on it. * * @param string $key * The key of the data to delete. * * @throws \Drupal\Core\TempStore\TempStoreException * Thrown when a lock for the backend storage could not be acquired. */
/** * Creates a lock from the given key. * * @param Key $key The key containing the lock's state * @param float|null $ttl Maximum expected lock duration in seconds * @param bool $autoRelease Whether to automatically release the lock or not when the lock instance is destroyed * * @return SharedLockInterface */
    public function createLockFromKey(Key $key, ?float $ttl = 300.0, bool $autoRelease = true): LockInterface
    {
        $lock = new Lock($key$this->store, $ttl$autoRelease);
        if ($this->logger) {
            $lock->setLogger($this->logger);
        }

        return $lock;
    }
}
use Symfony\Component\Lock\Store\InMemoryStore;

/** * @author Jérémy Derussé <jeremy@derusse.com> */
class LockTest extends TestCase
{
    public function testAcquireNoBlocking()
    {
        $key = new Key(uniqid(__METHOD__, true));
        $store = $this->createMock(PersistingStoreInterface::class);
        $lock = new Lock($key$store);

        $store
            ->expects($this->once())
            ->method('save');
        $store
            ->method('exists')
            ->willReturnOnConsecutiveCalls(true, false);

        $this->assertTrue($lock->acquire(false));
    }

    
Home | Imprint | This part of the site doesn't use cookies.