diff options
author | Marcel Klehr <mklehr@gmx.net> | 2023-04-20 13:18:28 +0200 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2023-04-23 12:36:12 +0200 |
commit | 1296f3612e0a4c81edb922b5cd000842807ca899 (patch) | |
tree | aeb21a93f20229acb8290813b27aa8eb7abab432 /tests/lib/BackgroundJob/JobTest.php | |
parent | ef27bd6e550530ccf37705f89d026ba2fb517f1c (diff) | |
download | nextcloud-server-1296f3612e0a4c81edb922b5cd000842807ca899.tar.gz nextcloud-server-1296f3612e0a4c81edb922b5cd000842807ca899.zip |
test(BackgroundJobs): Add tests for allowParallelRuns and hasReservedJobs
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
Diffstat (limited to 'tests/lib/BackgroundJob/JobTest.php')
-rw-r--r-- | tests/lib/BackgroundJob/JobTest.php | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/tests/lib/BackgroundJob/JobTest.php b/tests/lib/BackgroundJob/JobTest.php index b4048aa1c22..26e6b4ed85b 100644 --- a/tests/lib/BackgroundJob/JobTest.php +++ b/tests/lib/BackgroundJob/JobTest.php @@ -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; } |