You are here

public function DatabaseBackendTest::testQueue in Advanced Queue 8

@covers ::deleteQueue @covers ::countJobs @covers ::enqueueJob @covers ::enqueueJobs @covers ::claimJob @covers ::onSuccess @covers ::onFailure @covers ::deleteJob

File

tests/src/Kernel/DatabaseBackendTest.php, line 82

Class

DatabaseBackendTest
@coversDefaultClass \Drupal\advancedqueue\Plugin\AdvancedQueue\Backend\Database @group advancedqueue

Namespace

Drupal\Tests\advancedqueue\Kernel

Code

public function testQueue() {
  $first_job = Job::create('simple', [
    'test' => '1',
  ]);
  $second_job = Job::create('simple', [
    'test' => '2',
  ]);
  $third_job = Job::create('simple', [
    'test' => '3',
  ]);
  $fourth_job = Job::create('simple', [
    'test' => '4',
  ]);
  $this->firstQueue
    ->getBackend()
    ->enqueueJobs([
    $first_job,
    $third_job,
  ]);

  // Confirm that the other needed fields have been populated.
  $this
    ->assertQueuedJob(1, 'first_queue', 0, $first_job);
  $this
    ->assertQueuedJob(2, 'first_queue', 0, $third_job);

  // Confirm that the queue now contains two jobs.
  $counts = $this->firstQueue
    ->getBackend()
    ->countJobs();
  $this
    ->assertEquals([
    Job::STATE_QUEUED => 2,
  ], array_filter($counts));

  // Update the jobs to match how they'll look when claimed.
  $first_job
    ->setState(Job::STATE_PROCESSING);
  $first_job
    ->setExpiresTime(635814000 + 5);
  $third_job
    ->setExpiresTime(635814000 + 5);
  $third_job
    ->setState(Job::STATE_PROCESSING);

  // Confirm that the jobs are returned in the correct order (FIFO).
  $first_claimed_job = $this->firstQueue
    ->getBackend()
    ->claimJob();
  $this
    ->assertEquals($first_job, $first_claimed_job);
  $third_claimed_job = $this->firstQueue
    ->getBackend()
    ->claimJob();
  $this
    ->assertEquals($third_job, $third_claimed_job);
  $this
    ->assertNull($this->firstQueue
    ->getBackend()
    ->claimJob());
  $this->firstQueue
    ->getBackend()
    ->enqueueJobs([
    $first_job,
    $third_job,
  ]);
  $this->secondQueue
    ->getBackend()
    ->enqueueJob($second_job);
  $this->secondQueue
    ->getBackend()
    ->enqueueJob($fourth_job);

  // Confirm that the other needed fields have been populated.
  $this
    ->assertQueuedJob(5, 'second_queue', 0, $second_job);
  $this
    ->assertQueuedJob(6, 'second_queue', 0, $fourth_job);

  // Update the jobs to match how they'll look when claimed.
  $second_job
    ->setState(Job::STATE_PROCESSING);
  $second_job
    ->setExpiresTime(635814000 + 5);
  $fourth_job
    ->setExpiresTime(635814000 + 5);
  $fourth_job
    ->setState(Job::STATE_PROCESSING);

  // Confirm that deleting the job works.
  $this->secondQueue
    ->getBackend()
    ->deleteJob($second_job
    ->getId());
  $fourth_claimed_job = $this->secondQueue
    ->getBackend()
    ->claimJob();
  $this
    ->assertEquals($fourth_job, $fourth_claimed_job);

  // Confirm fail -> retry -> success.
  $fourth_job
    ->setState(Job::STATE_FAILURE);
  $this->secondQueue
    ->getBackend()
    ->onFailure($fourth_job);
  $this
    ->assertEquals(635814000, $fourth_job
    ->getProcessedTime());
  $this
    ->assertEmpty($fourth_job
    ->getExpiresTime());
  $this->secondQueue
    ->getBackend()
    ->retryJob($fourth_job, 9);
  $this
    ->assertEquals(Job::STATE_QUEUED, $fourth_job
    ->getState());
  $this
    ->assertEquals(1, $fourth_job
    ->getNumRetries());
  $this
    ->assertEquals(635814000 + 9, $fourth_job
    ->getAvailableTime());
  $this
    ->assertEmpty($fourth_job
    ->getExpiresTime());
  $this
    ->rewindTime(635814010);
  $fourth_job
    ->setState(Job::STATE_PROCESSING);
  $fourth_job
    ->setExpiresTime(635814010 + 5);
  $fourth_claimed_job = $this->secondQueue
    ->getBackend()
    ->claimJob();
  $this
    ->assertEquals($fourth_job, $fourth_claimed_job);
  $fourth_job
    ->setState(Job::STATE_SUCCESS);
  $this->secondQueue
    ->getBackend()
    ->onSuccess($fourth_job);
  $this
    ->assertEquals(635814010, $fourth_job
    ->getProcessedTime());
  $this
    ->assertEmpty($fourth_job
    ->getExpiresTime());

  // Confirm updated counts.
  $this->secondQueue
    ->getBackend()
    ->enqueueJob($first_job);
  $this->secondQueue
    ->getBackend()
    ->enqueueJob($second_job);
  $counts = $this->secondQueue
    ->getBackend()
    ->countJobs();
  $this
    ->assertEquals([
    Job::STATE_QUEUED => 2,
    Job::STATE_PROCESSING => 0,
    Job::STATE_SUCCESS => 1,
    Job::STATE_FAILURE => 0,
  ], $counts);

  // Confirm that deleting the queue removes the jobs.
  $this->firstQueue
    ->getBackend()
    ->deleteQueue();
  $this
    ->assertNull($this->firstQueue
    ->getBackend()
    ->claimJob());
}