DatabaseLockBackend example

    $objects = [];
    for ($i = 0; $i <= 3; $i++) {
      $objects[$i] = $this->randomObject();
    }

    // Create a key/value collection.     $database = Database::getConnection();
    // Mock the current user service so that isAnonymous returns FALSE.     $current_user = $this->prophesize(AccountProxyInterface::class);
    $factory = new SharedTempStoreFactory(
      new KeyValueExpirableFactory(\Drupal::getContainer()),
      new DatabaseLockBackend($database),
      $this->container->get('request_stack'),
      $current_user->reveal()
    );
    $collection = $this->randomMachineName();

    // Create two mock users.     for ($i = 0; $i <= 1; $i++) {
      $users[$i] = mt_rand(500, 5000000);

      // Storing the SharedTempStore objects in a class member variable causes a       // fatal exception, because in that situation garbage collection is not

  protected $lock;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $this->lock = new DatabaseLockBackend($this->container->get('database'));
  }

  /** * Tests backend release functionality. */
  public function testBackendLockRelease() {
    $success = $this->lock->acquire('lock_a');
    $this->assertTrue($success, 'Could acquire first lock.');

    // This function is not part of the backend, but the default database     // backend implement it, we can here use it safely.
Home | Imprint | This part of the site doesn't use cookies.