aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/BackgroundJob/JobListTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/BackgroundJob/JobListTest.php')
-rw-r--r--tests/lib/BackgroundJob/JobListTest.php150
1 files changed, 122 insertions, 28 deletions
diff --git a/tests/lib/BackgroundJob/JobListTest.php b/tests/lib/BackgroundJob/JobListTest.php
index ea02e1cd8b9..d816bf707e8 100644
--- a/tests/lib/BackgroundJob/JobListTest.php
+++ b/tests/lib/BackgroundJob/JobListTest.php
@@ -1,17 +1,23 @@
<?php
+
+declare(strict_types=1);
+
/**
- * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
- * This file is licensed under the Affero General Public License version 3 or
- * later.
- * See the COPYING-README file.
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\BackgroundJob;
+use OC\BackgroundJob\JobList;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
+use OCP\IDBConnection;
+use OCP\Server;
+use Psr\Log\LoggerInterface;
use Test\TestCase;
/**
@@ -24,26 +30,28 @@ class JobListTest extends TestCase {
/** @var \OC\BackgroundJob\JobList */
protected $instance;
- /** @var \OCP\IDBConnection */
+ /** @var IDBConnection */
protected $connection;
- /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
+ /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
protected $config;
- /** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
+ /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
protected $timeFactory;
+ private bool $ran = false;
protected function setUp(): void {
parent::setUp();
- $this->connection = \OC::$server->getDatabaseConnection();
+ $this->connection = Server::get(IDBConnection::class);
$this->clearJobsList();
$this->config = $this->createMock(IConfig::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
- $this->instance = new \OC\BackgroundJob\JobList(
+ $this->instance = new JobList(
$this->connection,
$this->config,
- $this->timeFactory
+ $this->timeFactory,
+ Server::get(LoggerInterface::class),
);
}
@@ -68,7 +76,7 @@ class JobListTest extends TestCase {
return $jobs;
}
- public function argumentProvider() {
+ public static function argumentProvider(): array {
return [
[null],
[false],
@@ -82,10 +90,10 @@ class JobListTest extends TestCase {
}
/**
- * @dataProvider argumentProvider
* @param $argument
*/
- public function testAddRemove($argument) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('argumentProvider')]
+ public function testAddRemove($argument): void {
$existingJobs = $this->getAllSorted();
$job = new TestJob();
$this->instance->add($job, $argument);
@@ -104,10 +112,10 @@ class JobListTest extends TestCase {
}
/**
- * @dataProvider argumentProvider
* @param $argument
*/
- public function testRemoveDifferentArgument($argument) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('argumentProvider')]
+ public function testRemoveDifferentArgument($argument): void {
$existingJobs = $this->getAllSorted();
$job = new TestJob();
$this->instance->add($job, $argument);
@@ -125,10 +133,10 @@ class JobListTest extends TestCase {
}
/**
- * @dataProvider argumentProvider
* @param $argument
*/
- public function testHas($argument) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('argumentProvider')]
+ public function testHas($argument): void {
$job = new TestJob();
$this->assertFalse($this->instance->has($job, $argument));
$this->instance->add($job, $argument);
@@ -141,17 +149,21 @@ class JobListTest extends TestCase {
}
/**
- * @dataProvider argumentProvider
* @param $argument
*/
- public function testHasDifferentArgument($argument) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('argumentProvider')]
+ public function testHasDifferentArgument($argument): void {
$job = new TestJob();
$this->instance->add($job, $argument);
$this->assertFalse($this->instance->has($job, 10));
}
- protected function createTempJob($class, $argument, $reservedTime = 0, $lastChecked = 0) {
+ protected function createTempJob($class,
+ $argument,
+ int $reservedTime = 0,
+ int $lastChecked = 0,
+ int $lastRun = 0): int {
if ($lastChecked === 0) {
$lastChecked = time();
}
@@ -161,14 +173,15 @@ class JobListTest extends TestCase {
->values([
'class' => $query->createNamedParameter($class),
'argument' => $query->createNamedParameter($argument),
- 'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
+ 'last_run' => $query->createNamedParameter($lastRun, IQueryBuilder::PARAM_INT),
'last_checked' => $query->createNamedParameter($lastChecked, IQueryBuilder::PARAM_INT),
'reserved_at' => $query->createNamedParameter($reservedTime, IQueryBuilder::PARAM_INT),
]);
- $query->execute();
+ $query->executeStatement();
+ return $query->getLastInsertId();
}
- public function testGetNext() {
+ public function testGetNext(): void {
$job = new TestJob();
$this->createTempJob(get_class($job), 1, 0, 12345);
$this->createTempJob(get_class($job), 2, 0, 12346);
@@ -184,7 +197,7 @@ class JobListTest extends TestCase {
$this->assertEquals($savedJob1, $nextJob);
}
- public function testGetNextSkipReserved() {
+ public function testGetNextSkipReserved(): void {
$job = new TestJob();
$this->createTempJob(get_class($job), 1, 123456789, 12345);
$this->createTempJob(get_class($job), 2, 0, 12346);
@@ -198,7 +211,22 @@ class JobListTest extends TestCase {
$this->assertEquals(2, $nextJob->getArgument());
}
- public function testGetNextSkipNonExisting() {
+ public function testGetNextSkipTimed(): void {
+ $job = new TestTimedJobNew($this->timeFactory);
+ $jobId = $this->createTempJob(get_class($job), 1, 123456789, 12345, 123456789 - 5);
+ $this->timeFactory->expects(self::atLeastOnce())
+ ->method('getTime')
+ ->willReturn(123456789);
+
+ $nextJob = $this->instance->getNext();
+
+ self::assertNull($nextJob);
+ $job = $this->instance->getById($jobId);
+ self::assertInstanceOf(TestTimedJobNew::class, $job);
+ self::assertEquals(123456789 - 5, $job->getLastRun());
+ }
+
+ public function testGetNextSkipNonExisting(): void {
$job = new TestJob();
$this->createTempJob('\OC\Non\Existing\Class', 1, 0, 12345);
$this->createTempJob(get_class($job), 2, 0, 12346);
@@ -213,10 +241,10 @@ class JobListTest extends TestCase {
}
/**
- * @dataProvider argumentProvider
* @param $argument
*/
- public function testGetById($argument) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('argumentProvider')]
+ public function testGetById($argument): void {
$job = new TestJob();
$this->instance->add($job, $argument);
@@ -227,7 +255,7 @@ class JobListTest extends TestCase {
$this->assertEquals($addedJob, $this->instance->getById($addedJob->getId()));
}
- public function testSetLastRun() {
+ public function testSetLastRun(): void {
$job = new TestJob();
$this->instance->add($job);
@@ -244,4 +272,70 @@ class JobListTest extends TestCase {
$this->assertGreaterThanOrEqual($timeStart, $addedJob->getLastRun());
$this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun());
}
+
+ public function testHasReservedJobs(): void {
+ $this->clearJobsList();
+
+ $this->timeFactory->expects($this->atLeastOnce())
+ ->method('getTime')
+ ->willReturn(123456789);
+
+ $job = new TestJob($this->timeFactory, $this, function (): void {
+ });
+
+ $job2 = new TestJob($this->timeFactory, $this, function (): void {
+ });
+
+ $this->instance->add($job, 1);
+ $this->instance->add($job2, 2);
+
+ $this->assertCount(2, iterator_to_array($this->instance->getJobsIterator(null, 10, 0)));
+
+ $this->assertFalse($this->instance->hasReservedJob());
+ $this->assertFalse($this->instance->hasReservedJob(TestJob::class));
+
+ $job = $this->instance->getNext();
+ $this->assertNotNull($job);
+ $this->assertTrue($this->instance->hasReservedJob());
+ $this->assertTrue($this->instance->hasReservedJob(TestJob::class));
+ $job = $this->instance->getNext();
+ $this->assertNotNull($job);
+ $this->assertTrue($this->instance->hasReservedJob());
+ $this->assertTrue($this->instance->hasReservedJob(TestJob::class));
+ }
+
+ public function testHasReservedJobsAndParallelAwareJob(): void {
+ $this->clearJobsList();
+
+ $this->timeFactory->expects($this->atLeastOnce())
+ ->method('getTime')
+ ->willReturnCallback(function () use (&$time) {
+ return time();
+ });
+
+ $job = new TestParallelAwareJob($this->timeFactory, $this, function (): void {
+ });
+
+ $job2 = new TestParallelAwareJob($this->timeFactory, $this, function (): void {
+ });
+
+ $this->instance->add($job, 1);
+ $this->instance->add($job2, 2);
+
+ $this->assertCount(2, iterator_to_array($this->instance->getJobsIterator(null, 10, 0)));
+
+ $this->assertFalse($this->instance->hasReservedJob());
+ $this->assertFalse($this->instance->hasReservedJob(TestParallelAwareJob::class));
+
+ $job = $this->instance->getNext();
+ $this->assertNotNull($job);
+ $this->assertTrue($this->instance->hasReservedJob());
+ $this->assertTrue($this->instance->hasReservedJob(TestParallelAwareJob::class));
+ $job = $this->instance->getNext();
+ $this->assertNull($job); // Job doesn't allow parallel runs
+ }
+
+ public function markRun() {
+ $this->ran = true;
+ }
}