diff options
-rw-r--r-- | lib/private/BackgroundJob/JobList.php | 2 | ||||
-rw-r--r-- | tests/lib/BackgroundJob/DummyJobList.php | 13 | ||||
-rw-r--r-- | tests/lib/BackgroundJob/JobListTest.php | 13 | ||||
-rw-r--r-- | tests/lib/BackgroundJob/JobTest.php | 76 | ||||
-rw-r--r-- | tests/lib/BackgroundJob/TestJob.php | 7 |
5 files changed, 106 insertions, 5 deletions
diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php index 6761aa282d1..707a8c79904 100644 --- a/lib/private/BackgroundJob/JobList.php +++ b/lib/private/BackgroundJob/JobList.php @@ -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') diff --git a/tests/lib/BackgroundJob/DummyJobList.php b/tests/lib/BackgroundJob/DummyJobList.php index be48259789a..70039382864 100644 --- a/tests/lib/BackgroundJob/DummyJobList.php +++ b/tests/lib/BackgroundJob/DummyJobList.php @@ -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 { } diff --git a/tests/lib/BackgroundJob/JobListTest.php b/tests/lib/BackgroundJob/JobListTest.php index ea02e1cd8b9..d42c2521761 100644 --- a/tests/lib/BackgroundJob/JobListTest.php +++ b/tests/lib/BackgroundJob/JobListTest.php @@ -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)); + } } 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; } diff --git a/tests/lib/BackgroundJob/TestJob.php b/tests/lib/BackgroundJob/TestJob.php index e15c7e86c99..cc7a4651c4b 100644 --- a/tests/lib/BackgroundJob/TestJob.php +++ b/tests/lib/BackgroundJob/TestJob.php @@ -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; } |