]> source.dussan.org Git - nextcloud-server.git/commitdiff
test(BackgroundJobs): Add tests for allowParallelRuns and hasReservedJobs
authorMarcel Klehr <mklehr@gmx.net>
Thu, 20 Apr 2023 11:18:28 +0000 (13:18 +0200)
committerMarcel Klehr <mklehr@gmx.net>
Sun, 23 Apr 2023 10:36:12 +0000 (12:36 +0200)
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
lib/private/BackgroundJob/JobList.php
tests/lib/BackgroundJob/DummyJobList.php
tests/lib/BackgroundJob/JobListTest.php
tests/lib/BackgroundJob/JobTest.php
tests/lib/BackgroundJob/TestJob.php

index 6761aa282d155fce4b093b3294e468507db3c2d1..707a8c799045fcdce1296343831fa969437c81d8 100644 (file)
@@ -384,7 +384,7 @@ class JobList implements IJobList {
                $query->executeStatement();
        }
 
-       public function hasReservedJob(?string $className): bool {
+       public function hasReservedJob(?string $className = null): bool {
                $query = $this->connection->getQueryBuilder();
                $query->select('*')
                        ->from('jobs')
index be48259789aa9f392b3d7a111a98fdf3f54db89b..700393828649c13ee06876a9ae188c2a62daa877 100644 (file)
@@ -21,6 +21,11 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
         */
        private array $jobs = [];
 
+       /**
+        * @var bool[]
+        */
+       private array $reserved = [];
+
        private int $last = 0;
 
        public function __construct() {
@@ -135,6 +140,14 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
                $job->setLastRun(time());
        }
 
+       public function hasReservedJob(?string $className = null): bool {
+               return $this->reserved[$className];
+       }
+
+       public function setHasReservedJob(?string $className, bool $hasReserved): void {
+               $this->reserved[$className] = $hasReserved;
+       }
+
        public function setExecutionTime(IJob $job, $timeTaken): void {
        }
 
index ea02e1cd8b97bbaec5c33c8997eddc74e3465f4f..d42c2521761d5f10357555bb59955c3acd47cfe3 100644 (file)
@@ -244,4 +244,17 @@ class JobListTest extends TestCase {
                $this->assertGreaterThanOrEqual($timeStart, $addedJob->getLastRun());
                $this->assertLessThanOrEqual($timeEnd, $addedJob->getLastRun());
        }
+
+       public function testHasReservedJobs() {
+               $job = new TestJob();
+               $this->instance->add($job);
+
+               $this->assertFalse($this->instance->hasReservedJob());
+               $this->assertFalse($this->instance->hasReservedJob(TestJob::class));
+
+               $job->start($this->instance);
+
+               $this->assertTrue($this->instance->hasReservedJob());
+               $this->assertTrue($this->instance->hasReservedJob(TestJob::class));
+       }
 }
index b4048aa1c2257ee0c702ae5322e192279364ce98..26e6b4ed85b7c31b6a5b227d5109ab5e52735746 100644 (file)
@@ -8,20 +8,23 @@
 
 namespace Test\BackgroundJob;
 
+use OCP\AppFramework\Utility\ITimeFactory;
 use OCP\ILogger;
 
 class JobTest extends \Test\TestCase {
        private $run = false;
+       private ITimeFactory $timeFactory;
 
        protected function setUp(): void {
                parent::setUp();
                $this->run = false;
+               $this->timeFactory = \OC::$server->get(ITimeFactory::class);
        }
 
        public function testRemoveAfterException() {
                $jobList = new DummyJobList();
                $e = new \Exception();
-               $job = new TestJob($this, function () use ($e) {
+               $job = new TestJob($this->timeFactory, $this, function () use ($e) {
                        throw $e;
                });
                $jobList->add($job);
@@ -41,7 +44,7 @@ class JobTest extends \Test\TestCase {
 
        public function testRemoveAfterError() {
                $jobList = new DummyJobList();
-               $job = new TestJob($this, function () {
+               $job = new TestJob($this->timeFactory, $this, function () {
                        $test = null;
                        $test->someMethod();
                });
@@ -60,6 +63,75 @@ class JobTest extends \Test\TestCase {
                $this->assertCount(1, $jobList->getAll());
        }
 
+       public function testRemoveAfterError() {
+               $jobList = new DummyJobList();
+               $job = new TestJob($this->timeFactory, $this, function () {
+                       $test = null;
+                       $test->someMethod();
+               });
+               $jobList->add($job);
+
+               $logger = $this->getMockBuilder(ILogger::class)
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $logger->expects($this->once())
+                       ->method('logException')
+                       ->with($this->isInstanceOf(\Throwable::class));
+
+               $this->assertCount(1, $jobList->getAll());
+               $job->execute($jobList, $logger);
+               $this->assertTrue($this->run);
+               $this->assertCount(1, $jobList->getAll());
+       }
+
+       public function testDisallowParallelRunsWithNoOtherJobs() {
+               $jobList = new DummyJobList();
+               $job = new TestJob($this->timeFactory, $this);
+               $job->setAllowParallelRuns(false);
+               $jobList->add($job);
+
+               $jobList->setHasReservedJob(null, false);
+               $jobList->setHasReservedJob(TestJob::class, false);
+               $job->start($jobList);
+               $this->assertTrue($this->run);
+       }
+
+       public function testAllowParallelRunsWithNoOtherJobs() {
+               $jobList = new DummyJobList();
+               $job = new TestJob($this->timeFactory, $this);
+               $job->setAllowParallelRuns(true);
+               $jobList->add($job);
+
+               $jobList->setHasReservedJob(null, false);
+               $jobList->setHasReservedJob(TestJob::class, false);
+               $job->start($jobList);
+               $this->assertTrue($this->run);
+       }
+
+       public function testAllowParallelRunsWithOtherJobs() {
+               $jobList = new DummyJobList();
+               $job = new TestJob($this->timeFactory, $this);
+               $job->setAllowParallelRuns(true);
+               $jobList->add($job);
+
+               $jobList->setHasReservedJob(null, true);
+               $jobList->setHasReservedJob(TestJob::class, true);
+               $job->start($jobList);
+               $this->assertTrue($this->run);
+       }
+
+       public function testDisallowParallelRunsWithOtherJobs() {
+               $jobList = new DummyJobList();
+               $job = new TestJob($this->timeFactory, $this);
+               $job->setAllowParallelRuns(false);
+               $jobList->add($job);
+
+               $jobList->setHasReservedJob(null, true);
+               $jobList->setHasReservedJob(TestJob::class, true);
+               $job->start($jobList);
+               $this->assertFalse($this->run);
+       }
+
        public function markRun() {
                $this->run = true;
        }
index e15c7e86c998e6d648a5667374f9bc309c22193a..cc7a4651c4b8fe5628f4f640b8d7a1ff4ec7a128 100644 (file)
@@ -8,7 +8,9 @@
 
 namespace Test\BackgroundJob;
 
-class TestJob extends \OC\BackgroundJob\Job {
+use OCP\AppFramework\Utility\ITimeFactory;
+
+class TestJob extends \OCP\BackgroundJob\Job {
        private $testCase;
 
        /**
@@ -20,7 +22,8 @@ class TestJob extends \OC\BackgroundJob\Job {
         * @param JobTest $testCase
         * @param callable $callback
         */
-       public function __construct($testCase = null, $callback = null) {
+       public function __construct(ITimeFactory $time = null, $testCase = null, $callback = null) {
+               parent::__construct($time ?? \OC::$server->get(ITimeFactory::class));
                $this->testCase = $testCase;
                $this->callback = $callback;
        }