]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix BackgroundJob tests
authorMarcel Klehr <mklehr@gmx.net>
Thu, 20 Apr 2023 13:29:47 +0000 (15:29 +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
lib/public/BackgroundJob/Job.php
tests/lib/BackgroundJob/JobListTest.php
tests/lib/BackgroundJob/JobTest.php

index d00d219541ea2ed657553a4dd896be16fd393e47..638919d4c392724709e4577ea195ccfe19c95adf 100644 (file)
@@ -396,7 +396,8 @@ class JobList implements IJobList {
                }
 
                try {
-                       return $query->executeQuery()->rowCount() > 0;
+                       $result = $query->executeQuery();
+                       return count($result->fetchAll()) > 0;
                } catch (Exception $e) {
                        return false;
                }
index c1e129f12212ef20914240890b0a82b3387c166e..455fb3d42e78d8c5d5ebb7fba13818bb95a38d7e 100644 (file)
@@ -44,6 +44,7 @@ abstract class Job implements IJob, IParallelAwareJob {
        protected $argument;
        protected ITimeFactory $time;
        protected bool $allowParallelRuns = true;
+       private ?ILogger $logger = null;
 
        /**
         * @since 15.0.0
@@ -62,6 +63,7 @@ abstract class Job implements IJob, IParallelAwareJob {
         * @since 15.0.0
         */
        public function execute(IJobList $jobList, ILogger $logger = null) {
+               $this->logger = $logger;
                $this->start($jobList);
        }
 
@@ -71,7 +73,7 @@ abstract class Job implements IJob, IParallelAwareJob {
         */
        public function start(IJobList $jobList): void {
                $jobList->setLastRun($this);
-               $logger = \OCP\Server::get(LoggerInterface::class);
+               $logger = $this->logger ?? \OCP\Server::get(LoggerInterface::class);
 
                if (!$this->getAllowParallelRuns() && $jobList->hasReservedJob(get_class($this))) {
                        $logger->debug('Skipping ' . get_class($this) . ' job with ID ' . $this->getId() . ' because another job with the same class is already running', ['app' => 'cron']);
@@ -86,7 +88,7 @@ abstract class Job implements IJob, IParallelAwareJob {
 
                        $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
                        $jobList->setExecutionTime($this, $timeTaken);
-               } catch (\Exception $e) {
+               } catch (\Throwable $e) {
                        if ($logger) {
                                $logger->error('Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')', [
                                        'app' => 'core',
index b370a233075ee3cc45a82e080e27b40c12e9ec8d..78565c11780a042776d492a1af9bb03f78e018a1 100644 (file)
@@ -32,6 +32,7 @@ class JobListTest extends TestCase {
 
        /** @var \OCP\AppFramework\Utility\ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
        protected $timeFactory;
+       private bool $ran = false;
 
        protected function setUp(): void {
                parent::setUp();
@@ -247,7 +248,10 @@ class JobListTest extends TestCase {
 
        public function testHasReservedJobs() {
                $this->clearJobsList();
-               $job = new TestJob();
+               $job = new TestJob($this->timeFactory, $this, function () {
+                       $this->assertTrue($this->instance->hasReservedJob());
+                       $this->assertTrue($this->instance->hasReservedJob(TestJob::class));
+               });
                $this->instance->add($job);
 
                $this->assertFalse($this->instance->hasReservedJob());
@@ -255,7 +259,10 @@ class JobListTest extends TestCase {
 
                $job->start($this->instance);
 
-               $this->assertTrue($this->instance->hasReservedJob());
-               $this->assertTrue($this->instance->hasReservedJob(TestJob::class));
+               $this->assertTrue($this->ran);
+       }
+
+       public function markRun() {
+               $this->ran = true;
        }
 }
index 37ced6e475732d2783e7deb0e0137a399fa82269..b44324711b865464e1f5db1fe8291576ace6dce6 100644 (file)
@@ -33,8 +33,7 @@ class JobTest extends \Test\TestCase {
                        ->disableOriginalConstructor()
                        ->getMock();
                $logger->expects($this->once())
-                       ->method('logException')
-                       ->with($e);
+                       ->method('error');
 
                $this->assertCount(1, $jobList->getAll());
                $job->execute($jobList, $logger);
@@ -54,8 +53,7 @@ class JobTest extends \Test\TestCase {
                        ->disableOriginalConstructor()
                        ->getMock();
                $logger->expects($this->once())
-                       ->method('logException')
-                       ->with($this->isInstanceOf(\Throwable::class));
+                       ->method('error');
 
                $this->assertCount(1, $jobList->getAll());
                $job->execute($jobList, $logger);
@@ -65,7 +63,8 @@ class JobTest extends \Test\TestCase {
 
        public function testDisallowParallelRunsWithNoOtherJobs() {
                $jobList = new DummyJobList();
-               $job = new TestJob($this->timeFactory, $this);
+               $job = new TestJob($this->timeFactory, $this, function() {
+               });
                $job->setAllowParallelRuns(false);
                $jobList->add($job);
 
@@ -77,7 +76,8 @@ class JobTest extends \Test\TestCase {
 
        public function testAllowParallelRunsWithNoOtherJobs() {
                $jobList = new DummyJobList();
-               $job = new TestJob($this->timeFactory, $this);
+               $job = new TestJob($this->timeFactory, $this, function() {
+               });
                $job->setAllowParallelRuns(true);
                $jobList->add($job);
 
@@ -89,7 +89,8 @@ class JobTest extends \Test\TestCase {
 
        public function testAllowParallelRunsWithOtherJobs() {
                $jobList = new DummyJobList();
-               $job = new TestJob($this->timeFactory, $this);
+               $job = new TestJob($this->timeFactory, $this, function() {
+               });
                $job->setAllowParallelRuns(true);
                $jobList->add($job);
 
@@ -101,7 +102,8 @@ class JobTest extends \Test\TestCase {
 
        public function testDisallowParallelRunsWithOtherJobs() {
                $jobList = new DummyJobList();
-               $job = new TestJob($this->timeFactory, $this);
+               $job = new TestJob($this->timeFactory, $this, function() {
+               });
                $job->setAllowParallelRuns(false);
                $jobList->add($job);