aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/BackgroundJob
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/BackgroundJob')
-rw-r--r--tests/lib/BackgroundJob/DummyJobList.php32
-rw-r--r--tests/lib/BackgroundJob/JobListTest.php86
-rw-r--r--tests/lib/BackgroundJob/JobTest.php12
-rw-r--r--tests/lib/BackgroundJob/QueuedJobTest.php6
-rw-r--r--tests/lib/BackgroundJob/TestJob.php16
-rw-r--r--tests/lib/BackgroundJob/TestParallelAwareJob.php16
-rw-r--r--tests/lib/BackgroundJob/TestTimedJobNew.php26
-rw-r--r--tests/lib/BackgroundJob/TimedJobTest.php24
8 files changed, 144 insertions, 74 deletions
diff --git a/tests/lib/BackgroundJob/DummyJobList.php b/tests/lib/BackgroundJob/DummyJobList.php
index fc80786d28b..717db52715f 100644
--- a/tests/lib/BackgroundJob/DummyJobList.php
+++ b/tests/lib/BackgroundJob/DummyJobList.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -7,14 +8,17 @@
namespace Test\BackgroundJob;
+use OC\BackgroundJob\JobList;
use OCP\BackgroundJob\IJob;
+use OCP\BackgroundJob\Job;
+use OCP\Server;
/**
* Class DummyJobList
*
* in memory job list for testing purposes
*/
-class DummyJobList extends \OC\BackgroundJob\JobList {
+class DummyJobList extends JobList {
/**
* @var IJob[]
*/
@@ -26,6 +30,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
private array $reserved = [];
private int $last = 0;
+ private int $lastId = 0;
public function __construct() {
}
@@ -37,9 +42,11 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
public function add($job, $argument = null, ?int $firstCheck = null): void {
if (is_string($job)) {
/** @var IJob $job */
- $job = \OCP\Server::get($job);
+ $job = Server::get($job);
}
$job->setArgument($argument);
+ $job->setId($this->lastId);
+ $this->lastId++;
if (!$this->has($job, null)) {
$this->jobs[] = $job;
}
@@ -54,9 +61,20 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
* @param mixed $argument
*/
public function remove($job, $argument = null): void {
- $index = array_search($job, $this->jobs);
- if ($index !== false) {
- unset($this->jobs[$index]);
+ foreach ($this->jobs as $index => $listJob) {
+ if (get_class($job) === get_class($listJob) && $job->getArgument() == $listJob->getArgument()) {
+ unset($this->jobs[$index]);
+ return;
+ }
+ }
+ }
+
+ public function removeById(int $id): void {
+ foreach ($this->jobs as $index => $listJob) {
+ if ($listJob->getId() === $id) {
+ unset($this->jobs[$index]);
+ return;
+ }
}
}
@@ -115,7 +133,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
/**
* set the job that was last ran
*
- * @param \OCP\BackgroundJob\Job $job
+ * @param Job $job
*/
public function setLastJob(IJob $job): void {
$i = array_search($job, $this->jobs);
@@ -126,7 +144,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
}
}
- public function getById(int $id): IJob {
+ public function getById(int $id): ?IJob {
foreach ($this->jobs as $job) {
if ($job->getId() === $id) {
return $job;
diff --git a/tests/lib/BackgroundJob/JobListTest.php b/tests/lib/BackgroundJob/JobListTest.php
index 510d307eda8..d816bf707e8 100644
--- a/tests/lib/BackgroundJob/JobListTest.php
+++ b/tests/lib/BackgroundJob/JobListTest.php
@@ -1,4 +1,7 @@
<?php
+
+declare(strict_types=1);
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -7,10 +10,13 @@
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,28 +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,
- \OC::$server->get(LoggerInterface::class),
+ Server::get(LoggerInterface::class),
);
}
@@ -70,7 +76,7 @@ class JobListTest extends TestCase {
return $jobs;
}
- public function argumentProvider() {
+ public static function argumentProvider(): array {
return [
[null],
[false],
@@ -84,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);
@@ -106,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);
@@ -127,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);
@@ -143,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();
}
@@ -163,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);
@@ -186,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);
@@ -200,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);
@@ -215,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);
@@ -229,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);
@@ -247,17 +273,17 @@ class JobListTest extends TestCase {
$this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun());
}
- public function testHasReservedJobs() {
+ public function testHasReservedJobs(): void {
$this->clearJobsList();
$this->timeFactory->expects($this->atLeastOnce())
->method('getTime')
->willReturn(123456789);
- $job = new TestJob($this->timeFactory, $this, function () {
+ $job = new TestJob($this->timeFactory, $this, function (): void {
});
- $job2 = new TestJob($this->timeFactory, $this, function () {
+ $job2 = new TestJob($this->timeFactory, $this, function (): void {
});
$this->instance->add($job, 1);
@@ -278,7 +304,7 @@ class JobListTest extends TestCase {
$this->assertTrue($this->instance->hasReservedJob(TestJob::class));
}
- public function testHasReservedJobsAndParallelAwareJob() {
+ public function testHasReservedJobsAndParallelAwareJob(): void {
$this->clearJobsList();
$this->timeFactory->expects($this->atLeastOnce())
@@ -287,10 +313,10 @@ class JobListTest extends TestCase {
return time();
});
- $job = new TestParallelAwareJob($this->timeFactory, $this, function () {
+ $job = new TestParallelAwareJob($this->timeFactory, $this, function (): void {
});
- $job2 = new TestParallelAwareJob($this->timeFactory, $this, function () {
+ $job2 = new TestParallelAwareJob($this->timeFactory, $this, function (): void {
});
$this->instance->add($job, 1);
diff --git a/tests/lib/BackgroundJob/JobTest.php b/tests/lib/BackgroundJob/JobTest.php
index fa89a06c8f5..b67059f0380 100644
--- a/tests/lib/BackgroundJob/JobTest.php
+++ b/tests/lib/BackgroundJob/JobTest.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -8,6 +9,7 @@
namespace Test\BackgroundJob;
use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\Server;
use Psr\Log\LoggerInterface;
class JobTest extends \Test\TestCase {
@@ -18,16 +20,16 @@ class JobTest extends \Test\TestCase {
protected function setUp(): void {
parent::setUp();
$this->run = false;
- $this->timeFactory = \OCP\Server::get(ITimeFactory::class);
+ $this->timeFactory = Server::get(ITimeFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);
\OC::$server->registerService(LoggerInterface::class, fn ($c) => $this->logger);
}
- public function testRemoveAfterException() {
+ public function testRemoveAfterException(): void {
$jobList = new DummyJobList();
$e = new \Exception();
- $job = new TestJob($this->timeFactory, $this, function () use ($e) {
+ $job = new TestJob($this->timeFactory, $this, function () use ($e): void {
throw $e;
});
$jobList->add($job);
@@ -41,9 +43,9 @@ class JobTest extends \Test\TestCase {
$this->assertCount(1, $jobList->getAll());
}
- public function testRemoveAfterError() {
+ public function testRemoveAfterError(): void {
$jobList = new DummyJobList();
- $job = new TestJob($this->timeFactory, $this, function () {
+ $job = new TestJob($this->timeFactory, $this, function (): void {
$test = null;
$test->someMethod();
});
diff --git a/tests/lib/BackgroundJob/QueuedJobTest.php b/tests/lib/BackgroundJob/QueuedJobTest.php
index ab4bacb201e..1c0946ff2f2 100644
--- a/tests/lib/BackgroundJob/QueuedJobTest.php
+++ b/tests/lib/BackgroundJob/QueuedJobTest.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -9,6 +10,7 @@ namespace Test\BackgroundJob;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
+use OCP\Server;
class TestQueuedJobNew extends QueuedJob {
public bool $ran = false;
@@ -27,8 +29,8 @@ class QueuedJobTest extends \Test\TestCase {
$this->jobList = new DummyJobList();
}
- public function testJobShouldBeRemovedNew() {
- $job = new TestQueuedJobNew(\OCP\Server::get(ITimeFactory::class));
+ public function testJobShouldBeRemovedNew(): void {
+ $job = new TestQueuedJobNew(Server::get(ITimeFactory::class));
$job->setId(42);
$this->jobList->add($job);
diff --git a/tests/lib/BackgroundJob/TestJob.php b/tests/lib/BackgroundJob/TestJob.php
index ea0634e63d9..ac18530ac7f 100644
--- a/tests/lib/BackgroundJob/TestJob.php
+++ b/tests/lib/BackgroundJob/TestJob.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -8,10 +9,10 @@
namespace Test\BackgroundJob;
use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\BackgroundJob\Job;
+use OCP\Server;
-class TestJob extends \OCP\BackgroundJob\Job {
- private $testCase;
-
+class TestJob extends Job {
/**
* @var callable $callback
*/
@@ -21,9 +22,12 @@ class TestJob extends \OCP\BackgroundJob\Job {
* @param JobTest $testCase
* @param callable $callback
*/
- public function __construct(?ITimeFactory $time = null, $testCase = null, $callback = null) {
- parent::__construct($time ?? \OCP\Server::get(ITimeFactory::class));
- $this->testCase = $testCase;
+ public function __construct(
+ ?ITimeFactory $time = null,
+ private $testCase = null,
+ $callback = null,
+ ) {
+ parent::__construct($time ?? Server::get(ITimeFactory::class));
$this->callback = $callback;
}
diff --git a/tests/lib/BackgroundJob/TestParallelAwareJob.php b/tests/lib/BackgroundJob/TestParallelAwareJob.php
index f7a6cf5fa79..6efb1a1fd8d 100644
--- a/tests/lib/BackgroundJob/TestParallelAwareJob.php
+++ b/tests/lib/BackgroundJob/TestParallelAwareJob.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -7,10 +8,10 @@
namespace Test\BackgroundJob;
use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\BackgroundJob\Job;
+use OCP\Server;
-class TestParallelAwareJob extends \OCP\BackgroundJob\Job {
- private $testCase;
-
+class TestParallelAwareJob extends Job {
/**
* @var callable $callback
*/
@@ -20,10 +21,13 @@ class TestParallelAwareJob extends \OCP\BackgroundJob\Job {
* @param JobTest $testCase
* @param callable $callback
*/
- public function __construct(?ITimeFactory $time = null, $testCase = null, $callback = null) {
- parent::__construct($time ?? \OC::$server->get(ITimeFactory::class));
+ public function __construct(
+ ?ITimeFactory $time = null,
+ private $testCase = null,
+ $callback = null,
+ ) {
+ parent::__construct($time ?? Server::get(ITimeFactory::class));
$this->setAllowParallelRuns(false);
- $this->testCase = $testCase;
$this->callback = $callback;
}
diff --git a/tests/lib/BackgroundJob/TestTimedJobNew.php b/tests/lib/BackgroundJob/TestTimedJobNew.php
new file mode 100644
index 00000000000..a40c4033655
--- /dev/null
+++ b/tests/lib/BackgroundJob/TestTimedJobNew.php
@@ -0,0 +1,26 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+namespace Test\BackgroundJob;
+
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\BackgroundJob\TimedJob;
+
+class TestTimedJobNew extends TimedJob {
+ public bool $ran = false;
+
+ public function __construct(ITimeFactory $timeFactory) {
+ parent::__construct($timeFactory);
+ $this->setInterval(10);
+ }
+
+ public function run($argument) {
+ $this->ran = true;
+ }
+}
diff --git a/tests/lib/BackgroundJob/TimedJobTest.php b/tests/lib/BackgroundJob/TimedJobTest.php
index 0ae245d1ff8..d56240eb75e 100644
--- a/tests/lib/BackgroundJob/TimedJobTest.php
+++ b/tests/lib/BackgroundJob/TimedJobTest.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -8,20 +9,7 @@
namespace Test\BackgroundJob;
use OCP\AppFramework\Utility\ITimeFactory;
-use OCP\BackgroundJob\TimedJob;
-
-class TestTimedJobNew extends TimedJob {
- public bool $ran = false;
-
- public function __construct(ITimeFactory $timeFactory) {
- parent::__construct($timeFactory);
- $this->setInterval(10);
- }
-
- public function run($argument) {
- $this->ran = true;
- }
-}
+use OCP\Server;
class TimedJobTest extends \Test\TestCase {
private DummyJobList $jobList;
@@ -31,10 +19,10 @@ class TimedJobTest extends \Test\TestCase {
parent::setUp();
$this->jobList = new DummyJobList();
- $this->time = \OCP\Server::get(ITimeFactory::class);
+ $this->time = Server::get(ITimeFactory::class);
}
- public function testShouldRunAfterIntervalNew() {
+ public function testShouldRunAfterIntervalNew(): void {
$job = new TestTimedJobNew($this->time);
$job->setId(42);
$this->jobList->add($job);
@@ -44,7 +32,7 @@ class TimedJobTest extends \Test\TestCase {
$this->assertTrue($job->ran);
}
- public function testShouldNotRunWithinIntervalNew() {
+ public function testShouldNotRunWithinIntervalNew(): void {
$job = new TestTimedJobNew($this->time);
$job->setId(42);
$this->jobList->add($job);
@@ -54,7 +42,7 @@ class TimedJobTest extends \Test\TestCase {
$this->assertFalse($job->ran);
}
- public function testShouldNotTwiceNew() {
+ public function testShouldNotTwiceNew(): void {
$job = new TestTimedJobNew($this->time);
$job->setId(42);
$this->jobList->add($job);