View source
<?php
namespace Drupal\Tests\ultimate_cron\Kernel;
use Drupal\Tests\system\Kernel\System\CronQueueTest;
use Drupal\ultimate_cron\CronJobInterface;
use Drupal\ultimate_cron\Entity\CronJob;
class UltimateCronQueueTest extends CronQueueTest {
public static $modules = array(
'ultimate_cron',
);
protected function setUp() {
parent::setUp();
module_load_install('ultimate_cron');
ultimate_cron_install();
$this
->installSchema('ultimate_cron', [
'ultimate_cron_log',
'ultimate_cron_lock',
]);
}
public function testExceptions() {
$queue = $this->container
->get('queue')
->get('cron_queue_test_exception');
$queue
->createItem(array(
$this
->randomMachineName() => $this
->randomMachineName(),
));
$this->cron
->run();
$this
->assertEquals(1, \Drupal::state()
->get('cron_queue_test_exception'));
$this
->assertEquals(1, $queue
->numberOfItems(), 'Failing item still in the queue after throwing an exception.');
$this->connection
->update('queue')
->condition('name', 'cron_queue_test_exception')
->fields([
'expire' => REQUEST_TIME - 1,
])
->execute();
system_cron();
$this->cron
->run();
$this
->assertEquals(2, \Drupal::state()
->get('cron_queue_test_exception'));
$this
->assertEquals(0, $queue
->numberOfItems(), 'Item was processed and removed from the queue.');
$queue = $this->container
->get('queue')
->get('cron_queue_test_broken_queue');
$queue
->createItem('process');
$queue
->createItem('crash');
$queue
->createItem('ignored');
$this->cron
->run();
$this
->assertEquals(2, $queue
->numberOfItems(), 'Failing queue stopped processing at the failing item.');
$item = $queue
->claimItem();
$this
->assertEquals('crash', $item->data, 'Failing item remains in the queue.');
$item = $queue
->claimItem();
$this
->assertEquals('ignored', $item->data, 'Item beyond the failing item remains in the queue.');
$queue = $this->container
->get('queue')
->get('cron_queue_test_requeue_exception');
$queue
->createItem([]);
$this->cron
->run();
$this
->assertEquals(2, \Drupal::state()
->get('cron_queue_test_requeue_exception'));
$this
->assertEquals(0, $queue
->numberOfItems());
}
public function testOverriddenProcessing() {
$job = CronJob::load(CronJobInterface::QUEUE_ID_PREFIX . 'cron_queue_test_broken_queue');
$this
->assertNull($job);
$this
->config('ultimate_cron.settings')
->set('queue.enabled', TRUE)
->save();
\Drupal::service('ultimate_cron.discovery')
->discoverCronJobs();
$job = CronJob::load(CronJobInterface::QUEUE_ID_PREFIX . 'cron_queue_test_broken_queue');
$this
->assertTrue($job instanceof CronJobInterface);
$queue = $this->container
->get('queue')
->get('cron_queue_test_broken_queue');
$queue
->createItem('process');
$queue
->createItem('process');
$queue
->createItem('process');
$this
->assertEquals(3, $queue
->numberOfItems());
$job
->run(t('Test launch'));
$this
->assertEquals(0, $queue
->numberOfItems());
$this
->config('ultimate_cron.settings')
->set('queue.delays.item_delay', 0.5)
->save();
$queue
->createItem('process');
$queue
->createItem('process');
$queue
->createItem('process');
$this
->assertEquals(3, $queue
->numberOfItems());
$before = microtime(TRUE);
$job
->run(t('Test launch'));
$after = microtime(TRUE);
$this
->assertEquals(0, $queue
->numberOfItems());
$this
->assertTrue($after - $before > 1);
$this
->assertTrue($after - $after < 1.5);
}
}